PDA

View Full Version : [ubuntu] shell script to convert .avi to .mp3 in batch



qmqmqm
August 28th, 2011, 07:54 PM
Hi

I have written a shell script to try to convert a bunch of avi files in a directory to mp3.

However the script stops after the first file.

I can't figure out what was wrong with the script. When I comment out the line that does the conversion, it prints the file line by line just fine.

Can anyone please advise?

Thanks in advance!

The script:

while read line
do
echo $line
mplayer -vo null -ao pcm:file="$line".wav "$line"
done </tmp/tmp.txt


/tmp/tmp.txt:

a b c.avi
d e f.avi
x y z.avi
... ...

papibe
August 28th, 2011, 10:02 PM
It just stops after the first? No errors? Is that the actual content of the list?

Regards.

erind
August 28th, 2011, 10:02 PM
As it is, the mplayer reading from stdin will consume all the arguments ( $line ) at once. Adding < /dev/null will redirect its reading and fix it.


while read line
do
echo $line
mplayer -vo null -ao pcm:file="$line".wav "$line" < /dev/null
done < /tmp/tmp.txt

qmqmqm
September 6th, 2011, 12:12 AM
As it is, the mplayer reading from stdin will consume all the arguments ( $line ) at once. Adding < /dev/null will redirect its reading and fix it.


while read line
do
echo $line
mplayer -vo null -ao pcm:file="$line".wav "$line" < /dev/null
done < /tmp/tmp.txt



Thanks Erind!

$line should contain only 1 line, not the entire file, because it is inside of a while loop... I do not see how mplayer is able to read the entire file at once.

Could you (or anyone else) please explain?

Thanks in advance!

sisco311
September 6th, 2011, 12:27 AM
Thanks Erind!

$line should contain only 1 line, not the entire file, because it is inside of a while loop... I do not see how mplayer is able to read the entire file at once.

Could you (or anyone else) please explain?

Thanks in advance!

By default, both the read built-in (from while read) and mplayer read from stdin (in a script running on a terminal, the Standard Input is how bash sees the characters you type on your keyboard.)

with < /tmp/tmp.txt you redirect the file to stdin, hence read will read the first line, then mplayer (it's greedier than read) will read the rest of the file.

with < /dev/null you tell to mplayer to read from /dev/null instead from stdin.

You could also tell read to read from FD6 and redirect the file to FD6:

while read -r -u6 line
do
mplayer ...
done 6< /tmp/tmp.txt

In this case mplayer will still accept input from the keyboard, or more precisely from the Standard Input (a.k.a stdin or FD0)

For details, check out: http://mywiki.wooledge.org/BashGuide/InputAndOutput

qmqmqm
September 17th, 2011, 08:36 PM
Thanks so much Sisco311 !

It's interesting how shell script works differently than you would think sometimes.