LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script ffmpeg (https://www.linuxquestions.org/questions/programming-9/shell-script-ffmpeg-899783/)

davidjmcc86 08-27-2011 07:33 AM

Shell script ffmpeg
 
Hello,

I have converted my CD collection to .flac

Now I would like to play my music on my ipod and a DSi, so I need to convert my files to .aac

I have written this script

Code:

!/bin/bash
# title holds name of song
for title in *
ffmpeg -i $title $title.aac
done

I get the following error

Code:

$ ./MusicConvert.sh
./MusicConvert.sh: line 4: syntax error near unexpected token `ffmpeg'
./MusicConvert.sh: line 4: `ffmpeg -i $title $title.aac'

I think I may also need to find a way to ensure the correct suffux is on the file. I'm afraid if it does work I will end up with file.flac.aac

Any advice is much appreciated.
Dave

dive 08-27-2011 07:41 AM

You missed the 'do'
Also you probably want to use basename:

Code:

!/bin/bash
# title holds name of song
for title in *.flac
do
    ffmpeg -i $title $(basename $title .flac).aac
done


davidjmcc86 08-27-2011 07:53 AM

Thanks alot for your help. You can tell I don't write scripts often.

Have a great weekend!
Dave

David the H. 08-27-2011 11:33 AM

A few more points.

1) The shabang starts with a "#!".

http://mywiki.wooledge.org/BashGuide...uments#Scripts

2) Always quote variables, to avoid problems with word splitting.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/Quotes

3) Use globbing rules to ensure that you're grabbing only the files you want. For complex cases, use find.

http://mywiki.wooledge.org/glob
http://mywiki.wooledge.org/UsingFind

4) basename is unnecessary when you have parameter expansion. You may want to collect and use both the path and the filename, to make your script more flexible.

http://mywiki.wooledge.org/BashFAQ/073

5) Consistent indentation and similar formatting, among other good practices, helps make scripts easier to read and debug. I think for/while/until loops are more readable when the "do" keyword is also on the same line, for example.

http://mywiki.wooledge.org/BashGuide/Practices

Code:

#!/bin/bash

for title in ./*.flac; do

        path="${title%/*}"
        name="${title##*/}"
        ffmpeg -i "$title" "$path/${name%.*}.aac"

done


davidjmcc86 08-28-2011 06:27 AM

David the H, thanks for your help and the website. I will incorporate all into my next script.

R/
Dave


All times are GMT -5. The time now is 05:41 AM.