HI All,
A little post for a cool Linux bash script I created with some help from the net that will convert all the .flac files in a directory and all sub-directories to .mp3 format. This script can easily be modified to convert other audio files to any other format that FFMPEG supports and also convert between all the different video files FFMPEG supports.
*** Stupid WordPress messes up all the quotes ” please make sure your final script has only regular quotes throughout and not forward and backward quotes. Also the single quotes are mess up too… Make sure they are regular sigle quotes ‘. Sorry for the inconvenience. ***
First install ffmpeg:
Go here for how-to install ffmpeg with mp3 and other file support…
Otherwise a regular sudo apt-get install ffmpeg will install a limited version of ffmpeg
Now onto the script…
Use nano to create the script
$ nano flac2mp3
Copy and paste the following script:
#!/bin/bash
# convert .flac to .mp3 recursively
# By Glen Hewlett
#####################################
if [ -z $1 ];then echo Give target directory; exit 0;fi
find “$1″ -depth -name ‘*’ | while read file ; do
directory=$(dirname “$file”)
oldfilename=$(basename “$file”)
newfilename=$(basename “${file%.[Ff][Ll][Aa][Cc]}”)
if [ "$oldfilename" != "$newfilename" ]; then
ffmpeg -i “$directory/$oldfilename” -ab 320k “$directory/$newfilename.mp3″ </dev/null
rm “$directory/$oldfilename”
fi
done
CTRL-X and save the output Then make it executable
$ chmod +x flac2mp3
Use the command flac2mp3 with the directory name you want it to look for files to convert. Example: convert all the files in the current directory and all subdirectories.
$./flac2mp3 ./
Example: convert all the files in the “/temp” directory and all subdirectories.
$./flac2mp3 /temp
You can remove the “#” at the beginning of the line rm “$directory/$oldfilename” and the script will delete the original file. Be careful to make sure you still have a copy of the original files if you do remove the “#”.
If you want this script to convert all the .wav files to .aac you would change the line:
newfilename=$(basename “${file%.[Ff][Ll][Aa][Cc]}”)
to
newfilename=$(basename “${file%.[Ww][Aa][Vv]}”)
Also modify the ffmpeg output line from:
ffmpeg -i “$directory/$oldfilename” -ab 320k “$directory/$newfilename.mp3″ </dev/null
to
ffmpeg -i “$directory/$oldfilename” -ab 320k “$directory/$newfilename.aac” </dev/null
Of course you can always tweak the ffmpeg command line to a lower bitrate if you want depending on your needs (-ab 320k). If you do plan on using this for video conversion then you will have to play with the ffmpeg settings to get the desired output. There’s tons of info all over the net for using ffmpeg to convert audio and video formats. Just search for ffmpeg and the format you want to end up with and you’ll surely find tons of example ffmpeg commands.
To get ffmpeg to list all the file types it can convert use the command:
$ffmpeg -formats
Have fun… Glen.
Ady Ngom
December 20, 2011 at 6:09 pm
Very neat and helpful my friend maybe an improvement would be to capture all possible formats in an array and pass two parameters to the function specifying the conversion type, thus making it a universal conversion tool. Thanks again for posting this.
jcampos
February 2, 2012 at 10:34 am
Thank you very much, you script guided to me and now I will be able to convert recursively my video files.