Earlier today I got a massive SCORM object that contained lots of mp3 files. They were all high quality files, so I wanted to cut them down in size for web use. For this purpose I am using ffmpeg which you can easily get for Windows, Linux or Mac. I tried the following on a Mac, but it should also work on Linux (paths permitting) and on Windows if you are using something like cygwin.
I highly recommend you get familiar with ffmpeg as it is amazingly useful. Just check out the man pages; swf to avi, avi to something that works on ipods, all a breeze!
find . -type f -name *.mp3 -exec ffmpeg -i {} -ac 1 -ab 32k {}32kb_m.mp3 \;
Now, as you can see, I have created a new file for each original file. Each new file’s name is appended with 32kb_m.mp3 which tells me information about how the new mp3 file is encoded and gives hints to ffmpeg as to the files format (mp3 and encoder). Let me explain a little about the command. With find
we are saying find stuff in the current directory which are files and end in “.mp3″ and when you find them execute ffmpeg. The {} bit is expanded with whatever file find
found. With the ffmpeg
bit we are saying our lord of ffmpeg please take as input this file fromfind
‘s output and set the number of audio channels to 1 and the bitrate to 32kbp/s and then output the file using the settings to a file whose filename is the same as our input file but has 32kb_m.mp3 added to end.
Now we have created our new files and checked everything is ok (you should have those audios backed up somewhere too) we want to move all these new files to the locations of the originals. Our command moves/renames the files, removing part of the filename, recursively. Here it is:
find . -type f -name *32kb_m.mp3 | sed 's/\(.*\)32kb_m.mp3/mv "\132kb_m.mp3" "\1"/' | sh
So, again we use find! Here we are saying find, in the current directory, all files with file names ending in “32kb_m.mp3″ . Here we are only, hopefully, pulling out our newly created files. Next we pipe all this data to sed
. We tell sed
: hey Mr. Sed, look for any set of characters that are followed with “32kb_m.mp3″ and replace the entire string (including 32kb_m.mp3) with “mv [the bit matched before 32kb_m.mp3]32kb_m.mp3 [the bit matched before 32kb_m.mp3]“. Very simple, I hope! We are taking the big list from the output of find and using sed to transform the output into lots of mv
commands. If we pipe these commands to sh
then all those commands get executed. Hey presto, we’re done!
How much space did you save?
All my mp3s were in ‘audio’ which I copied to ‘audio_32kb_m’. I then entered audio_32kb_m by cding into it and executing the commands above. Below is the output from du
which tells me the total size of my audio directories. Size reduction is around 75%.
snapple:/tmp tariq$ du -hs audio* 33M audio 8.5M audio_32kb_m
Leave a Reply