Reduce mp3 file size for app and web use
November 14, 2013 Leave a comment
Typical 2 minutes piece of music is roughly 2-4MB in common mp3 format. This is size for usual quality that is stereo audio 256 kbps and 44.1 kHz sample rate. Tough the file size is not that large, it will slow down loading HTML5 game or mobile app, especially on wireless networks.
Web HTML5 or mobile game applications do not need nearly as high quality and the size can be reduced to ~400-700kB without too much audible quality penalty. Difference can be noticed if you listen for it, but it’s unlikely that users do notice anything, as they have nothing to compare it against. This also depends lot of the music, sharp high frequency sounds tend to distort first.
MP3/WAV to reduced size MP3
Easy way to convert mp3s is lame. A flexible command line mp3 decoder and encoder.
Here we use 1:45 minutes 1.7MB music piece Lake Hylia from Zelda Reorchestrated. I don’t own any rights to this music and it’s used purely for demonstration purposes. I selected this piece because it’s melodic and instrumental like most game music is.
Key to reducing file size is lower sample and bitrate, and most importantly converting file to mono. Example below is how to do this with lame 3.99.5 on OS/X. I installed lame using Macports.
$ lame -hv -mm --resample 22.05 "Zelda Reorchestrated - Lake Hylia.original.mp3" -B 32 "Zelda Reorchestrated - Lake Hylia.small.mp3" LAME 3.99.5 64bits (http://lame.sf.net) Autoconverting from stereo to mono. Setting encoding to mono mode. Resampling: input 44.1 kHz output 22.05 kHz polyphase lowpass filter disabled Encoding Zelda Reorchestrated - Lake Hylia.mp3 to Zelda Reorchestrated - Lake Hylia.small.mp3 Encoding as 22.05 kHz single-ch MPEG-2 Layer III VBR(q=4) Frame | CPU time/estim | REAL time/estim | play/CPU | ETA 3996/3996 (100%)| 0:01/ 0:01| 0:01/ 0:01| 82.375x| 0:00 8 [ 36] ** 16 [ 24] * 24 [ 4] * 32 [3932] ************************************************************************************************************************************** ------------------------------------------------------------------------------------------------------------------------------------------------- kbps mono % long switch short % 31.7 100.0 99.9 0.1 0.1 Writing LAME Tag...done ReplayGain: +3.7dB
This converts the input mp3 (lame also accepts .wav files) to 22.05kHz 64kbs monoaural mp3. The output file size is 412kB, resulting to nearly 75% savings. You can reduce file further by using even lower bitrate, (experiment -B 24 or -B 16), but you may start hearing distortions.
$ du -hs Zelda\ Reorchestrated\ -\ Lake\ Hylia.* 1.6M Zelda Reorchestrated - Lake Hylia.original.mp3 404K Zelda Reorchestrated - Lake Hylia.small.mp3
Test play the files here and listen for any differences.
Original: Zelda Reorchestrated – Lake Hylia.original.mp3
Reduced: Zelda Reorchestrated – Lake Hylia.small.mp3
MP3/WAV to reduced OGG Vorbis
This is main for Firefox as it still supports only Ogg Vorbis. You can convert mp3/wav file to reduced mono ogg vorbis with ffmpeg tool.
This example uses ffmpeg 2.1, and like lame I installed is on OS/X using Macports.
$ ffmpeg -i "Zelda Reorchestrated - Lake Hylia.original.mp3" -strict experimental -acodec libvorbis -ab 32k -ar 22050 -ac 1 "Zelda Reorchestrated - Lake Hylia.small.ogg" ffmpeg version 2.1 Copyright (c) 2000-2013 the FFmpeg developers built on Oct 30 2013 23:24:10 with Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-libtheora --enable-libschroedinger --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libspeex --enable-libass --enable-libbluray --enable-gnutls --enable-libfreetype --disable-outdev=xv --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --arch=x86_64 --enable-yasm --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid --enable-nonfree --enable-libfaac libavutil 52. 48.100 / 52. 48.100 libavcodec 55. 39.100 / 55. 39.100 libavformat 55. 19.104 / 55. 19.104 libavdevice 55. 5.100 / 55. 5.100 libavfilter 3. 90.100 / 3. 90.100 libavresample 1. 1. 0 / 1. 1. 0 libswscale 2. 5.101 / 2. 5.101 libswresample 0. 17.104 / 0. 17.104 libpostproc 52. 3.100 / 52. 3.100 Input #0, mp3, from 'Zelda Reorchestrated - Lake Hylia.original.mp3': Metadata: title : Lake Hylia artist : Zelda Reorchestrated album : Twilight Princess track : 11 Duration: 00:01:44.39, start: 0.000000, bitrate: 128 kb/s Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s Output #0, ogg, to 'Zelda Reorchestrated - Lake Hylia.small.ogg': Metadata: title : Lake Hylia artist : Zelda Reorchestrated album : Twilight Princess track : 11 encoder : Lavf55.19.104 Stream #0:0: Audio: vorbis (libvorbis), 22050 Hz, mono, fltp, 32 kb/s Metadata: title : Lake Hylia artist : Zelda Reorchestrated album : Twilight Princess TRACKNUMBER : 11 encoder : Lavf55.19.104 Stream mapping: Stream #0:0 -> #0:0 (mp3 -> libvorbis) Press [q] to stop, [?] for help size= 398kB time=00:01:44.38 bitrate= 31.2kbits/s video:0kB audio:387kB subtitle:0 global headers:3kB muxing overhead 1.866299%
Resulting OGG file size and quality is comparable to reduced mp3.
$ du -hs Zelda\ Reorchestrated\ -\ Lake\ Hylia.*p3 400K Zelda Reorchestrated - Lake Hylia.small.ogg 1.6M Zelda Reorchestrated - Lake Hylia.original.mp3
Compare here:
Original: Zelda Reorchestrated – Lake Hylia.original.mp3
Reduced: Zelda Reorchestrated – Lake Hylia.small.ogg