You are not logged in.

#1 2010-05-12 21:44:40

CaptainKirk
Member
Registered: 2009-06-07
Posts: 393

Small Bash Script Problem

I have this lovely little script that I whipped up:

#!/bin/bash
OIFS=$IFS
for i in $( ls *.flv ); do
  set -- "$i"
  IFS="."
  declare -a Array=($*)
  if  [ ! -e ${Array[0]}.mp3 ]; then
    ffmpeg -i ${Array[0]}.flv -vn -acodec copy -ar 44100 -ac 2 -ab 1048576 -f mp3 ${Array[0]}.mp3
  fi
done
IFS=$OIFS

which looks for any .flv file that has no matching .mp3 file and creates that .mp3 file. It works fine but not for a .flv file with a space in the name.

I tried


    ffmpeg -i \"${Array[0]}.flv\" -vn -acodec copy -ar 44100 -ac 2 -ab 1048576 -f mp3 \"${Array[0]}.mp3\"

but that doesn't seem to work.

Can anyone tell me how to get such files to process?

Offline

#2 2010-05-12 22:00:19

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Small Bash Script Problem

No need to declare an array. Use Bash string manipulation.

for i in *.flv; do
  ffmpeg -i "${i%.*}.flv" -vn -acodec copy -ar 44100 -ac 2 -ab 1048576 -f mp3 "${i%.*}.mp3"
done

Last edited by falconindy (2010-05-12 22:03:32)

Offline

#3 2010-05-12 22:02:12

Andrwe
Member
From: Leipzig/Germany
Registered: 2009-06-17
Posts: 322
Website

Re: Small Bash Script Problem

Try this one:

if  [ ! -e "${Array[0]}.mp3" ]; then
   ffmpeg -i "${Array[0]}.flv" -vn -acodec copy -ar 44100 -ac 2 -ab 1048576 -f mp3 "${Array[0]}.mp3"

Website: andrwe.org

Offline

#4 2010-05-12 22:23:25

CaptainKirk
Member
Registered: 2009-06-07
Posts: 393

Re: Small Bash Script Problem

Andrwe, I had tried that before also. The " characters don't come through in the final command so it doesn't help.

falconindy, you're a bit of a genius, eh? Took me a half hour to figure out that syntax and you replaced my code with a few lines. Well you skipped the IF. Here is the code that works, using your trick:

#!/bin/bash
for i in *.flv; do
if  [ ! -e "${i%.*}.mp3" ]; then
  ffmpeg -i "${i%.*}.flv" -vn -acodec copy -ar 44100 -ac 2 -ab 1048576 -f mp3 "${i%.*}.mp3"
fi
done

Thank you. That's pretty cool. smile

Offline

#5 2010-05-12 22:58:07

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Small Bash Script Problem

Aha. I've had a long day -- I think I glanced at the if/then and didn't think it was necessary. Now that I look at it again, you're right to leave it in.

You should probably assign the base filename to a variable rather than calling the string manip 3 times.

Just an FYI: quotes in shell commands are for functional purposes only -- the actual quotes won't (and shouldn't) appear in the parsed command. If they did, the program executed would be looking for an argument that had a literal quote in it. Simply put:

$ touch "foo bar"
$ ls "foo bar"
foo bar
$ ls \"foo bar\"
ls: cannot access "foo: No such file or directory
ls: cannot access bar": No such file or directory

Last edited by falconindy (2010-05-12 22:59:24)

Offline

#6 2010-05-12 23:13:05

CaptainKirk
Member
Registered: 2009-06-07
Posts: 393

Re: Small Bash Script Problem

Thanks. smile

Offline

#7 2010-05-13 03:35:42

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Small Bash Script Problem

#!/bin/bash
for i in *.flv; do
    [[ ! -e ${i%.*}.mp3 ]] && ffmpeg -i "${i%.*}.flv" -vn -acodec copy -ar 44100 -ac 2 -ab 1048576 -f mp3 "${i%.*}.mp3"
done

Offline

Board footer

Powered by FluxBB