RSS

Convert all files recursively from one format to another (ie .flac to .mp3) using Linux

11 Sep

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.

Advertisement
 
2 Comments

Posted by on September 11, 2011 in Uncategorized

 

2 Responses to Convert all files recursively from one format to another (ie .flac to .mp3) using Linux

  1. 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.

     
  2. 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.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.