Wednesday, April 18, 2012

Mass Music Conversion and Compression


Scenario:
You have 9GB free storage on your Android smart phone (or 2GB storage on your iPhone), and you have 14GB of music that you want to put on it. What do you do? You have probably like 3 options:
  1. Buy a micro SD card (Android only) - ~$10-20 depending on storage size
  2. Only load some of your music onto your device - The cost of only having some of your music (not to mention the agony of picking the right music)
  3. Compressing music, reducing sound quality but allowing you to keep all your music - The cost of running a script
Now for some, option 3 sounds absolutely ridiculous, because who want's to listen to poor-quality music? My argument  is that it's actually not that bad, and you can barely tell the difference depending on the compression level.

Lets see some before and after stats:
Before:
14.8GB
~128kb/s bitrate

After:
6.6GB
~64kb/s bitrate

Warning, if you are going to try this, try it with one song first and see if the quality is good enough for you (I tried many variations in order to reach my optimal compression quality).

How To:

1.
Forget MP3's. Yes, that's right, MP3 is TERRIBLE! (also there's licencing stuff with the codecs that gave me issues). On Ubuntu it may work out of the box, but for legal reasons Debian doesn't have the codec in its stable repositories. Luckily, I use Debian and did some research into MP3 alternative. OGG Vorbis is where it's at! "Ogg Vorbis is much better than MP3 at very low bitrates (64kbps, etc)."(wiki vs.) Alright, great now we have our audio encoding figured out.

2.
FFMPEG is your friend, play with it. Seriusly, give it a whirl and see what you can do to your audio. Here is what I used for my music:
ffmpeg -i "$input" -aq 15 -acodec vorbis -strict experimental "$output"

3.
Finally, here is the script I came up with to convert all of my music at once:


#!/bin/bash
dir="/home/user/Music/shrunken"
#the find command has a bug where it doesn't actually exclude the shrunken directory (but im sure you can fix it)
find . -path "/home/user/Music/shrunken/*" -prune -o -name *.mp3 -print| while read f
do
input=${f:2}
fileLoc=${f:1}
output="$dir${fileLoc%.*}.ogg"
extension=${output%%.*}
echo $input
folder=$(dirname "$extension")
#mkdir with the "-p" option tells it to auto-create the sub-directories required
mkdir -p "$folder"
#the "echo" is a neat trick that defaults all of the questions ffmpeg might ask about replacing files to "no"
echo "n"|ffmpeg -i "$input" -aq 15 -acodec vorbis -strict experimental "$output"
done
Yes, there are bugs. Yes, it's not perfect. But your smart, you can handle it. Good Luck!

(Side note: My music player on android is jukefox - great piece of software right there)

0 comments:

Post a Comment