run an audio file by bash script [ SOLVED ]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ckonn
Level 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

run an audio file by bash script [ SOLVED ]

Post by ckonn »

hello,

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

thank you in advance
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: run an audio file by bash script

Post by xenopeek »

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 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

Re: run an audio file by bash script

Post by ckonn »

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?
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: run an audio file by bash script

Post by xenopeek »

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 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

Re: run an audio file by bash script

Post by ckonn »

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
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: run an audio file by bash script

Post by xenopeek »

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 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

Re: run an audio file by bash script

Post by ckonn »

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.
Locked

Return to “Scripts & Bash”