FFMPEG: Convert m4a files to mp3 without significant loss of information / quality. Automated bitrate

47

20

I have a load of audio files (about 1000) which I want to convert from m4a to mp3 so I can use play them on a CD player which has a USB port.

I tried doing something simple like: ffmpeg -i FILE.m4a FILE.mp3 but this seems to reduce the bitrate to a very low value, which isn't what I want.

Similarly I don't want to convert using a constant bitrate, such as 320k, because some of the files I am converting are 320k m4a's and some are as low quality as 96k m4a's.

It seems to make no sense to force 320k, since some files will become many times larger than they need be. Similarly it makes no sense to destroy all my 320k files by converting them to something much lower than 96k. (At the moment, the files are being converted to about 50k.)

Does anyone know how I can do this? What I really want to do is tell ffmpeg to convert all m4a files in a directory into mp3's while retaining the current audio quality as best it can. (Of course there is likely to be some extra losses from converting from lossy to lossy file formats.)

Thanks for your help. If this isn't possible, is there some sort of script which might detect the required quality as it converts files individually?

PS: I am working on an intel Mac, but also have a Ubuntu box.

user3728501

Posted 2014-01-20T20:23:40.367

Reputation: 1 090

Answers

53

VBR

You can use the -qscale:a option in ffmpeg to create a variable bitrate (VBR) MP3 output. From FFmpeg Wiki: Encoding VBR (Variable Bit Rate) mp3 audio:

Control quality with -qscale:a (or the alias -q:a). Values are encoder specific, so for libmp3lame the range is 0-9 where a lower value is a higher quality. 0-3 will normally produce transparent results, 4 (default) should be close to perceptual transparency, and 6 usually produces an "acceptable" quality. The option -qscale:a is mapped to the -V option in the standalone lame command-line interface tool.

You'll have to experiment to see what value is acceptable for you. Also see Hydrogen Audio: Recommended LAME Encoder Settings.

Encoding multiple files

You can use a Bash "for loop" to encode all files in a directory:

$ mkdir newfiles
$ for f in *.m4a; do ffmpeg -i "$f" -codec:v copy -codec:a libmp3lame -q:a 2 newfiles/"${f%.m4a}.mp3"; done

Get ffmpeg

It's recommended to use a recent build of ffmpeg if possible. See the FFmpeg Download page for links to builds for OS X, Linux, and Windows. You can also follow a guide to compile FFmpeg.

llogan

Posted 2014-01-20T20:23:40.367

Reputation: 31 929

1Okay thanks, the commands worked but I don't think it is doing what I wanted. I've set -q:a 0, which is doing what I expected for higher quality m4a's (producing large files with higher vbr rates) but lower quality files I think are still being converted into large files, with vbr's of about 250-260 kb/s. I would have expected the files which are about 96k in m4a format to be converted to a similar bitrate with the lame encoder in vbr mode. I'm assuming I haven't quite understood how the vbr encoding mode works? – user3728501 – 2014-01-21T22:42:29.157

23

Use FFmpeg command line version.

MP3 is officially discontinued (from 2017) and obsolete codec. World has already switched to AAC (more efficient & quality codec), and everyone should use this, instead of mp3:

ffmpeg -i filenameee.m4a -acodec copy output.aac

Update: Unfortunately, some people still request for MP3, and okey... here it is:

ffmpeg -i filenameee.m4a -acodec libmp3lame -ab 256k output.mp3

T.Todua

Posted 2014-01-20T20:23:40.367

Reputation: 2 436

4Since the question is about quality, maybe it will be beneficial if convert to 256k instead of 128. – VessoVit – 2015-09-21T12:31:37.160

1@VessoVit he has now. – barlop – 2019-12-30T00:08:53.097

You write "MP3 is officially discontinued (from 2017), obsolete and dead codec" bur the link you give says "Some weeks ago, we updated our website with information about the end of the mp3 licensing program by Technicolor and Fraunhofer. So, does this mean that mp3 is really dead now, as we have read often in the last few days? Of course not! mp3 is a phenomenon, which changed our way of consuming music forever, and is very much alive in 2017.

The licensing program coming to an end is due to the fact that the last patent included in the program expired. In no way does that mean that the – barlop – 2019-12-30T00:11:15.690

(cntd) "usage permit ends. The only ones deciding on the “death” of mp3 will be the users, who might switch to more modern audio formats at some point, such as AAC, which is included in almost every smartphone today." <-- end of quote from your link. – barlop – 2019-12-30T00:11:19.967

3

This worked for me:

ffmpeg -i testing.m4v -b:a 192K -vn testing.mp3

user205987

Posted 2014-01-20T20:23:40.367

Reputation: 171

1

Using existing answers as a basis I wrote a bash script which should do exactly what the question asks on an Ubuntu machine (tested on 16.04). You'll need to install avprobe which is in the libav-tools package. You can install avprobe with the following command

sudo apt-get install libav-tools

NOTE: It would be wise to make a backup of any folder in which you run this script as well as make your best effort to read and understand what it is doing before running.

I have not tested very rigorously so YMMV.

#!/bin/bash

mkdir "mp3s"
for f in *.m4a;
do
    bitrate=$(avprobe "${f}" 2> >(grep bitrate) | sed 's/^.*bitrate://g' | sed 's/[^0-9]*//g')
    bitrate="${bitrate}K"
    new_filename=$(echo "${f}" | sed 's/.m4a$/.mp3/g')
    ffmpeg -y -i "${f}" -acodec libmp3lame -ab "${bitrate}" "mp3s/${new_filename}"
done

jbowman

Posted 2014-01-20T20:23:40.367

Reputation: 111

1

avprobe for windows http://builds.libav.org/windows/release-gpl/ can also use mediainfo to show bitrate.. The second answer shows this is the kind of line you want.. like what you did but this shows it more raw ffmpeg -i inputfile.m4a -acodec libmp3lame -ab 256k outputfile.mp3 and as you have, one can apply that to multiple files.

– barlop – 2019-12-30T00:34:33.023

0

If you work at 48 KhZ and at least 24-bit in the input file (from a file editor i.ex.) -q:a 0 will reproduce the 20 kHz at no so large file as flac... But low quality audio unless you reduce the noise from high frequencies (hiss) by dithering or nose reduction audio editors, created in conversions, that will be reproduced, then will use data file as a inaudible HF, but expensive noise.

Kaiser Lima

Posted 2014-01-20T20:23:40.367

Reputation: 9