How do I programmatically convert mp3 to an itunes-playable aac/m4a file?
Asked Answered
P

7

5

I've been looking for a way to convert an mp3 to aac programmatically or via the command line with no luck. Ideally, I'd have a snippet of code that I could call from my rails app that converts an mp3 to an aac. I installed ffmpeg and libfaac and was able to create an aac file with the following command:

ffmpeg -i test.mp3 -acodec libfaac -ab 163840 dest.aac

When i change the output file's name to dest.m4a, it doesn't play in iTunes.

Thanks!

Primeval answered 16/9, 2008 at 7:35 Comment(0)
E
9

FFmpeg provides AAC encoding facilities if you've compiled them in. If you are using Windows you can grab full binaries from here

ffmpeg -i source.mp3 -acodec libfaac -ab 128k dest.aac

I'm not sure how you would call this from ruby.

Also, be sure to set the bitrate appropriately.

Emotion answered 16/9, 2008 at 7:42 Comment(5)
Hmm, I installed ffmpeg and when i tried the command you mentioned, the following warnings/errors came up: WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s Unknown encoder 'aac' I downloaded the source using git. Do I need to download any other codecs?Mesdames
You need an ffmpeg compiled with libfaac, and its acodec libfaac, not aac. -ab 128k is the correct value, as you stated, not -ab 128.Hosmer
Thanks for correcting me Dark. And this is why we test before releasing it to the client :PEmotion
This is also why people looking for help with ffmpeg should drop by #ffmpeg to get real-time support :)Hosmer
True that. Definitely beats some guy BSing off the top of his head :o)Emotion
I
2

in ffmpeg 0.5 or later use ffmpeg -i source.mp3 target.m4a

for better results to transfer metadata and to override default bitrate ffmpeg applies

ffmpeg -i "input.mp3" -ab 256k -map_meta_data input.mp3:output.m4a output.m4a

best do not convert as ipod plays mp3 fine (I know there is such answer but my low standing does not allow voting)

Insufficiency answered 16/9, 2008 at 7:35 Comment(0)
H
2

There are only three free AAC encoders that I know of that are available through a commandline interface:

  1. FAAC (LPGL), which is honestly pretty bad (the quality is going to be significantly worse than LAME at the same bitrate). Its fine though if you're willing to go for higher bitrates (>>128kbps) and need AAC for compatibility, not quality reasons. The most common way to use FAAC is through ffmpeg, as libfaac.

  2. Nero AAC, the commandline encoder for which is available for free under Windows and Linux, but only for noncommercial use (and is correspondingly closed-source).

  3. ffmpeg's AAC encoder, which is still under development and while I believe it does technically work, it is not at all stable or good or even fast, since its still in the initial stages. Its also not available in trunk, as far as I know.

(Edit: Seems iTunes might have one too, I suspect its terms of use are similar to Nero's. AFAIK its quality is comparable.)

Hosmer answered 16/9, 2008 at 7:39 Comment(0)
A
2

I realize I'm late to this party, but I'm questioning the premise of this question. Why do you even want to convert an MP3 to an "itunes playable" format? iTunes already handles MP3s natively.

It seems like you are doing an unnecessary conversion, and since you are converting from one lossy format to another, you are losing some quality in the process.

Audriaaudrie answered 30/9, 2008 at 19:38 Comment(1)
itunes will download songs as mp3, but if you're already downloaded them as m4a, you have to convert them. Itunes will also do that, but if you have hundreds of songs, it's annoying to right click on each one and convert. Not all "mobile digital music" (for lack of a better term) players, play m4a. which is why you'd want to convert in the first place.Rai
T
1

After installing the converting app on the linux/window machine you're running your Rails application on, use the "system()" command in Ruby to invoke the converting application on the system. system("command_here");

Thissa answered 16/9, 2008 at 7:40 Comment(0)
S
0

Actually, syntax is ffmpeg -i input.mp3 -c:a aac -strict -2 -b:a 256k output.m4a; more correct if one is emulating "correct" bitrate. cf.:link for a compilation scheme. (rpmfusion package works fine too:

configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-frei0r --enable-gnutls --enable-libass --enable-libcdio --enable-libcelt --enable-libdc1394 --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect

Swithbert answered 16/9, 2008 at 7:35 Comment(0)
L
0

I've had good luck using mplayer (which I believe uses ffmpeg...) and lame. To the point that I've wrapped it up in a script:

#!/bin/sh

TARGET=$1

BASE=`basename "${TARGET}"`
echo TARGET: "${TARGET}"
echo BASE:   "${BASE}" .m4a

# Warning! Race condition vulnerability here! Should use a mktemp
# variant or something...
mkfifo encode
mplayer -quiet -ao pcm -aofile encode "${TARGET}" &
lame --silent encode "${BASE}".mp3
rm encode

Sorry for the security issues, I banged this out on the train one day...

My mplayer and lame come from fink

Lobe answered 16/9, 2008 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.