6

I have this bash script for converting .mp4 video to .mp3 audio. It runs, but does the loop only once, though there are more mp4 files in /home/myname/mp4dir.

The script converts the first .mp4 file it finds to .mp3, unless there is already an .mp3 file. This should be done in a loop, but after the first call to ffmpeg the script stops.

Why?

#!/bin/bash
find /home/myname/mp4dir -name "*.mp4" | while read f
do
        fOut=`echo $f | sed s/mp4/mp3/`
        echo "convert $f => $fOut"
        if ! test -f $fOut
        then
                ffmpeg -i $f -vn -f mp3 $fOut
        fi
done
13
  • Does it work if you replace the loop body with echo $f? Commented Sep 22, 2012 at 12:16
  • Yes, all .mp4 files are listed. The script stops after the first ffmpeg execution. If I comment this out and replace it by an echo, it runs for all .mp4 files. Commented Sep 22, 2012 at 12:25
  • What do you see when you run the script with bash -x youscript.sh? Does the ffmpeg command complete? Commented Sep 22, 2012 at 12:27
  • With bash -x youscript.sh the loop executes more than once; the output of several ffmpeg calls shows. What's the difference? Commented Sep 22, 2012 at 12:30
  • Edit: Sorry, my fault: The behaviour is the same as with direct call to the script: It executes only once. Commented Sep 22, 2012 at 12:44

3 Answers 3

15

The answer is to add -nostdin to ffmpeg (wouldn't answer to an old question like this, but still tops on related Google searches).

Sign up to request clarification or add additional context in comments.

Comments

4

try this in terminal

create bash nohup.sh and paste in this

#!/bin/sh
while true
do
 /var/www/bash/Someother-bash-script-goes-here.sh
 sleep 60
done

in terminal type where nohup.sh is located

nohup sh nohup.sh >/dev/null 2>&1 &

nohup, will execute every 60 seconds, you can grep service check like this

 #!/bin/sh
SERVICE='ffmpeg'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    echo "$SERVICE service running, will not bother ffmpeg yet"
else
    echo "$SERVICE is not running"
    echo "$SERVICE is not running!" | /var/www/bash/start_service.sh
fi

Comments

3

From the comments we got that there actually are input files you are looping over (of what kind soever) and that ffmpeg at least starts once. Then, you are stuck.

Add

&> ffmpeg.outerr < /dev/null 

to your ffmpeg command and monitor the file ffmpeg.outerr while your "loop", i.e. ffmpeg, executes. It will tell you what the problem is. Either you can solve this problem yourself, or you come back to stackoverflow with this ffmpeg-specific problem using the tag ffmpeg.

2 Comments

The < /dev/null does the trick. Only I don't know why. The log file does not show any errors, just the regular output of ffmpeg. But with the input redirection the loop runs as intended.
The ffmpeg solution is to add the -nostdin flag to the ffmpeg command

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.