run an audio file by bash script [ SOLVED ]

Forum rules
Before you post please read how to get help
Post Reply
ckonn
Level 2
Level 2
Posts: 58
Joined: Wed Oct 01, 2014 7:03 pm

run an audio file by bash script [ SOLVED ]

Post by ckonn » Wed Apr 19, 2017 1:03 pm

hello,

could someone help me to run a .mp3 audio file by some bash script?

thank you in advance
Last edited by ckonn on Tue Apr 25, 2017 8:11 am, edited 1 time in total.
linux mint 17.3 mate edition

User avatar
xenopeek
Level 24
Level 24
Posts: 22082
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: run an audio file by bash script

Post by xenopeek » Wed Apr 19, 2017 3:17 pm

Install mpg123 and then you can play an mp3 file with mpg123 [i]/path/to/file.mp3[/i]. See mpg123 --help for summary of options or man mpg123 for detailed description of options.
Image

ckonn
Level 2
Level 2
Posts: 58
Joined: Wed Oct 01, 2014 7:03 pm

Re: run an audio file by bash script

Post by ckonn » Thu Apr 20, 2017 11:48 am

I have install mpg123 and it works.

but my wish was that I write just one script that execute the following tasks:

1 run a given audio file in background
2 after 5 seconds greets me with: "Hello, Tony!"

I came to the conclusion that for this purpose the script should look like this:

#!/bin/bash

mpg123 /home/music/lenny1.mp3

sleep 5

echo "Hello, Tony!"


the problem here is that if I run exactly such file the audio lenny.mp3 will start and the bash will execute the next command just when the audio comes to end. if the audio file has a duration of 15:20 m I should wait for 15 min. to see the greeting.

I would like to write such a script that brings the os to run lenny.mp3 as a single separate process running in a background and then ( after 5 seconds ) I want that bash gives me the greeting "Hello, Tony!" without having to wait 15 min.

is it possible?
linux mint 17.3 mate edition

User avatar
xenopeek
Level 24
Level 24
Posts: 22082
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: run an audio file by bash script

Post by xenopeek » Thu Apr 20, 2017 12:33 pm

Yes, add a & at the end of the mpg123 command to run it in the background. As such:
mpg123 /home/music/lenny1.mp3 &

You may also want to use -q (I think) option of mpg123 to make it not output text on the terminal. Or else replace the command with this to make it not output text (&>/dev/null means send both stdout and stderr to the everlasting bit bucket):
mpg123 /home/music/lenny1.mp3 &>/dev/null &
Image

ckonn
Level 2
Level 2
Posts: 58
Joined: Wed Oct 01, 2014 7:03 pm

Re: run an audio file by bash script

Post by ckonn » Thu Apr 20, 2017 1:25 pm

I have added & to the line mpg123 /home/music/lenny1.mp3 and now it works really gut!

just ... one little detail. ( hopefully I am not too impudent )

is it possible to write such script that will make things so that bash will not show in the command line the following message:

High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
version 1.16.0; written and copyright by Michael Hipp and others
free software (LGPL) without any warranty but with best wishes

Directory: /home/music/lenny1.mp3
Playing MPEG stream 1 of 1: lynar.mp3 ...

MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereo
linux mint 17.3 mate edition

User avatar
xenopeek
Level 24
Level 24
Posts: 22082
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: run an audio file by bash script

Post by xenopeek » Thu Apr 20, 2017 2:02 pm

I already answered that :wink:
xenopeek wrote:You may also want to use -q (I think) option of mpg123 to make it not output text on the terminal. Or else replace the command with this to make it not output text (&>/dev/null means send both stdout and stderr to the everlasting bit bucket):
mpg123 /home/music/lenny1.mp3 &>/dev/null &
Image

ckonn
Level 2
Level 2
Posts: 58
Joined: Wed Oct 01, 2014 7:03 pm

Re: run an audio file by bash script

Post by ckonn » Fri Apr 21, 2017 12:33 am

so.

mpg123 --quiet --scale 1000 --loop -1 /home/music/lennny1.mp3 &

sleep 6

spd-say hi,,tony

echo "hi, tony!"

and one almost have the feeling he just created his own homemade A. I. !

I am not sure that this is possible with windows. that's why linux is cool! not bad.
linux mint 17.3 mate edition

Post Reply

Return to “Scripts & Bash”