27

We want to convert 320kbps mp3 file to 128kbps mp3 so currently we are using below ffmpeg command but its not working.

ffmpeg -i input.mp3 -codec:a libmp3lame -qscale:a 5 output.mp3

Result:-the output bitrate same as input mp3.

And we are following the FFmpeg Encoding guideline for that here is the link :- https://trac.ffmpeg.org/wiki/Encode/MP3

so please suggest any solution.

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Do you have a link or name to the (Android) FFmpeg build you're using? – VC.One Mar 28 '17 at 05:09
  • After years of FFmpeg usage I arrogantly didn't need to check how to set audio bitrate - I can just tell you... After reading your link now I see you were looking at **Variable BitRate** (VBR) settings where `-qscale:a 5` does aim for average bitrate of around 130kbps. Anycase your question sounds like really you want a **Constant BitRate** (CBR) of 128kbps. PS: I wanted to know your FFmpeg build because you can't input 320kbps with setting `-qscale:a 5` and get output of exact same 320kbps. What's wrong with it? I want to check... – VC.One Mar 28 '17 at 18:15
  • 1
    This appears to be unrelated to Android, and about ffmpeg in general. Suggest removing `android` tag and updating title. – Jake Dec 22 '21 at 00:26

3 Answers3

35

I tried your shown command (tested on Windows / commandline) :

ffmpeg -i input.mp3 -codec:a libmp3lame -qscale:a 5 output.mp3

Result : It works for me. However the -qscale:a 5 makes FFmpeg decide on an average bitrate for you. With one (320k) MP3 file I got it giving a close convert of 134kbps. This is expected since :

lame option   Average kbit/s  Bitrate range kbit/s    ffmpeg option
   -V 5             130           120-150                -q:a 5

Solution :
Instead of making the internal mp3 frames hold different bitrates (that vary to acommodate the "current" perceived audio, eg: think "silent" parts using smaller rate of bits/bytes compared to "busy" audio parts), so just set a constant bitrate of 128kbps as you need.

I would just set it to constant 128kbps manually and explicitly with :

ffmpeg -i input.mp3 -codec:a libmp3lame -b:a 128k output.mp3
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Thanks for replying but we have tried your given command but in the output log audio bitrates is 320 kbps after conversion.so and I have tried using android FFmpegAndroid library.so its not working for us. –  Mar 29 '17 at 12:16
  • Below is the log.but the bitrates of mp3 is 320Kbps Output #0, mp3, to '/storage/AUD_1490697495080.mp3': Metadata: TIT2 :Jab Tak TALB :MS Dhoni TPE1 :Armaan TSSE :Lavf56.4.101 Stream #0:1: Audio:mp3,44100 Hz,stereo,320 kb/s frame=1 fps=0.0 q=0.0 Lsize=1470kB time=00:00:15.00 bitrate=802.2kbits/s –  Mar 29 '17 at 12:25
  • 1
    So output file `AUD_1490697495080.mp3` is a **different** name to the input file name, right? I mean afterwards you have two separate files that are both 320k, is that correct? I'm trying to visualize your issue and if YES about two files @ 320k then your result could only happen if used `-codec:a copy` instead of `-codec:a libmp3lame`... This is why I need to know your exact FFmpeg build (**got a link**?) to test exact same thing because maybe you have a buggy version.... – VC.One Apr 02 '17 at 05:24
  • 2
    for me 20MB mp3 converted to 3.6 MB file. thanks alot – saber tabatabaee yazdi May 13 '20 at 19:14
  • Error: "Could not find tag for codec h264 in stream #0, codec not currently supported in container". Clearly we need some way to tell ffmpeg that it's audio only. Any ideas? – Jake Dec 22 '21 at 00:03
  • 1
    `-map 0:0` seems to fix it. Why is that not the default? – Jake Dec 22 '21 at 00:13
  • @Jake How to reproduce that error? MP3 is not a container so it can't have been from `-i input.mp3`. – VC.One Dec 22 '21 at 11:58
  • @VC.One it was a `.m4a` file. – Jake Dec 23 '21 at 18:33
  • I can't reproduce your error message. This works okay `ffmpeg -i test.m4a -c:a libmp3lame -qscale:a 5 test.mp3`. I don't know why H264 video codec was mentioned by FFmpeg if you used an audio-only file (and not a renamed MP4)... – VC.One Dec 23 '21 at 19:40
  • `ffmpeg -i input.m4a -b:a 80k output.m4a`. `Stream mapping: Stream #0:1 -> #0:0 (mjpeg (native) -> h264 (libx264)) Stream #0:0 -> #0:1 (aac (native) -> aac (native))`. The `mjpeg` 'stream' I think is just cover art. – Jake Dec 24 '21 at 02:42
9

I use this shellscript in order to not visit this stackoverflow-page over and over again :)

#!/bin/bash
[[ ! -n $1 ]] && { 
    echo "Usage: mp3convert <input.mp3> <output.mp3> <bitrate:56/96/128/256> <channels> <samplerate>"
    exit 0
}
set -x # print next command
ffmpeg -i "$1" -codec:a libmp3lame -b:a "$3"k -ac "$4" -ar $5 "$2"
coderofsalvation
  • 1,764
  • 16
  • 13
3

Make sure your version of FFmpeg has libmp3lame enabled. The selected answer didn't work for me, but this did:

ffmpeg -v debug -i "input.mp3" -c:a libmp3lame \
   -b:a 128k -ac 2 -ar 44100 -vn "output.mp3"

-ac 2 - output has 2 (stereo) audio channels -ar 44100 - sample rate of 44100Hz, which is ideal for high quality music.

Although, in 2022 I wouldn't recommend converting to 128kbps since storage space is much more cheap and abundant nowadays.

I think -b:a 192k strikes the best balance between compression and quality for most people (unless you're an audiophile with $1000 headphones, and even then you'd be better off using FLAC anyways).

Benji
  • 310
  • 3
  • 12