How to convert High bitrate MP3 to lower rate using FFmpeg - audio

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.

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

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"

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).

Related

MPEG Audio Constant bit rate conversion

I am trying to convert few .wav files to .mp3 format
The desired .mp3 format is :
I tried with FFmpeg with this code :
ffmpeg -i input.wav -vn -ac 2 -b:a 160k output1.mp3
This is the output of this command on one .wav format
I am getting the result but two things are different
Overall bit rate mode and Writing library
Writing library: LAME3.99.5 vs LAME3.100 ( I think this shouldn't
make any problem?)
bit rate mode Constant Vs variable
How to change bit rate mode from variable to Constant? and do I need to convert using the same Writing library?
Thanks!
The output using ffmpeg -i input.wav -vn -ac 2 -b:a 160k output1.mp3 is constant bit rate, however ffmpeg writes a header with the title Xing and Mediainfo infers that to indicate VBR. Disable writing that header if you want Mediainfo to detect Constant bit rate.
ffmpeg -i input.wav -vn -ac 2 -b:a 160k -write_xing 0 output1.mp3
Note that the actual MP3 encoding won't change.
I ended up using sox instead of FFmpeg :
sox -t wav -r 48000 -b 16 -c 2 file.wav -C 160 -t mp3 sock33.mp3
Sample rate of 48 kHz (-r 48000)
two channel (-c 2)
16 bits bit depth (-b 16)

How to monitor volume level during ffmpeg capture

I capture sound from soundcard, convert it to mp3 and stream to multicast IP.
Command looks like:
ffmpeg -f alsa -i hw:0 -async 1 -vn -acodec libmp3lame -ac 1 -ar 44100 -b:a 128k -flush_packets 0 -f mpegts -pes_payload_size 426 -mpegts_start_pid 0x44 udp://233.21.215.101:1234?pkt_size=1316
But I need to monitor volume level of capturing. If there is no sound (or noise only) I need to switch to another (reserve) channel.
Can I get volume level in ffmpeg output? How I can do it?
P.S: I can get volume level by "volumedetect" filter but it's not in realtime. I need realtime monitoring.
P.P.S: I run ffmpeg programmatically from my application and can't monitor additional windows (like video frame when I use showvolume filter).
Right now, I can suggest using the showvolume filter
ffmpeg -f alsa -i hw:0
-async 1 -vn -acodec libmp3lame -ac 1 -ar 44100 -b:a 128k
-flush_packets 0 -f mpegts
-pes_payload_size 426 -mpegts_start_pid 0x44 udp://233.21.215.101:1234?pkt_size=1316
-filter_complex showvolume[a] -map [a] -f sdl -
A small video window will pop up with a bar graph display of the current volume and also as text labels. You'll need a ffmpeg build with SDL library linked.
You can't close this video window while your streaming is active.

ffmpeg - Have troubling syncing up audio and video together

I have a webcam and a separate mic. I want to record what is happening.
It almost works, however the audio seems to play quickly and parts missing while playing over the video.
This is the command I am currently using to get it partially working
ffmpeg -thread_queue_size 1024 -f alsa -ac 1 -i plughw:1,0 -f video4linux2 -thread_queue_size 1024 -re -s 1280x720 -i /dev/video0 -r 25 -f avi -q:a 2 -acodec libmp3lame -ab 96k out.mp4
I have tried other arguments, but unsure if it has to do with the formats I am using or incorrect parameter settings.
Also, the next part would be how to stream it. Everytime I try going through rtp it complains about multiple streams. I tried doing html as well, but didn't like the format. html html://localhost:50000/live_feed or rts rts://localhost:5000
edit:
I am running this on a rpi 3.

libav / ffmpeg output optimisation for small file sizes

I am using the following command to take an audio mp3 file and make a video out of it (by using a static jpg picture). My aim is to get a mp3 audio that is as small as possible with still acceptable quality.
frequency="11000"
bitrate="45000"
avconv -loop 1 -i a.jpg -i audio.mp3 -shortest -r 1 -metadata STEREO_MODE=mono -c:v libx264 -ar "$frequency" -b:a "$bitrate" -ac 0 result.mkv
My questions are:
1. how can I achieve that the resulting file is MONO?
2. is it possible to reduce the bitrate furthermore? I would like to use values below 45000, too.
3. My aim is to get control of the parameters that influence the file size most significantly. Presently I know that the frequency is quite important. Are there any other parameters that would help me to get a very small output file with still acceptable quality?
Thanks in advance.
Since you are coding to a compressed audio codec, the frequency doesn't directly affect the file size. However, a frequency of 11 kHz will reduce quality of music.
Instead, I'd suggest
frequency="22050"
bitrate="48000"
ffmpeg -loop 1 -i a.jpg -i audio.mp3 -shortest -r 1 -c:v libx264 -crf 28 \
-ar "$frequency" -b:a "$bitrate" -ac 1 result.mkv
The CRF parameter controls video quality - lower values produce better quality but larger files. You'll get more savings from controlling that than audio bitrate, which is at the lower end anyway.
If your build has libfdk_aac included, you can instead use
frequency="22050"
bitrate="32000"
ffmpeg -loop 1 -i a.jpg -i audio.mp3 -shortest -r 1 -c:v libx264 -crf 28 \
-ar "$frequency" -c:a libfdk_aac -profile:a aac_he_v2 -b:a "$bitrate" -ac 1 result.mkv

Increasing a file's volume using VLC CLI

My goal is to have a script that takes an audio file and increases its volume by 50%.
I currently use the following AutoHotKey snippet to encode a file to MP3:
run_string := "bash -c ""\""c:\Program Files\VideoLAN\VLC\vlc.exe\"" -I dummy \""" . file_path . "\"" --sout='#transcode{acodec=mp3,vcodec=dummy}:standard{access=file,mux=raw,dst=\""" . file_path . ".mp3\""}' vlc://quit"""
How can I modify this line to not only encode to mp3, but also increase the volume of the file by 50%? I tried setting --volume 150 but it just made the file play, while I don't want to play, I want to have it saved with that volume.
If you have suggestions for other Windows-compatible tools to modify audio that can do this, (along with instructions on how to do this) I'll be happy to hear about them.
I suggest you to use ffmpeg. it is very powerful, cross platform 32 or 64 bit, audio and video converter. Can be downloaded from Zeranoe FFmpeg - Builds
Below sample commands work for audio extracting from video, or audio converter with volume increasing or decreasing support.
Extract audio from video to MP3, or convert audio to MP3 (sample InputFilePath_VideoOrAudio = "e:\video.mp4" or "e:\audio.m4a")
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec libmp3lame -ab 192k -ar 48000 -sn -dn -vn "E:\out.mp3"
Extract audio from video to MP3 and increase volume 150% while extracting add -af "volume=1.5" parameter.
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec libmp3lame -ab 192k -ar 48000 -sn -dn -vn -af "volume=1.5" "E:\out.mp3"
List of audio converter parameters (mp3,ogg,ac3,wma,flac,wav,aiff,m4a....). to change volume level while converting to audio add -af "volume=VolumeValue" parameter.
VolumeValue=0.5 decrease volume %50
VolumeValue=1.5 increase volume %150
VolumeValue=2.0 increase volume %200 and so on.
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec libmp3lame -ab 192k -ar 48000 -sn -dn -vn -af "E:\out.mp3"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec ac3 -ab 192k -ar 48000 -sn -dn -vn "E:\out.ac3"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -f ogg -acodec libvorbis -ab 192k -ar 48000 -sn -dn -vn "E:\out.ogg"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec wmav2 -ab 192k -ar 48000 -sn -dn -vn "E:\out.wma"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec flac -sn -dn -vn "E:\out.flac"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -sn -dn -vn "E:\out.wav"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -f aiff -sn -dn -vn "E:\out.aiff"
e:\ffmpeg\ffmpeg.exe -y -i "InputFilePath_VideoOrAudio" -acodec aac -ab 192k -ar 48000 -sn -dn -vn "E:\out.m4a"
Note 1: some codecs can be experimental in such case you should use -strict experimental or -strict -2 parameters.
Note 2: -ab parameter means audio bit rate. Some devices can not play audio file that bit rate greater than -ab 192k. Use -ab 128k or -ab 192k with -ar 44100 parameters to produce audio file that can be playable most of the mobile devices. -ac 2 parameter means stereo -ac 1 means mono.
to convert specific part of the input file use -ss 00:00:00 and -t parameters. -ss means Start From -t means duration. Important: parameter -ss should placed before the -i parameter, otherwise ffmpeg seeks to -ss position slowly.
Samples: assume that input file duration is 00:20:00 (20 minutes)
using only -ss 00:05:00 means convert input file starting from 5th minute to end of the input file. Duration of the output file will be 15 minutes.
using -ss 00:05:00 with -t 120 or -t 00:02:00 means convert 120 seconds, starting from 5th minute. Duration of the output file will be 120 seconds.
e:\ffmpeg\ffmpeg.exe -y -ss 00:05:00 -i "InputFilePath_VideoOrAudio" -t 120 -acodec libmp3lame -ab 192k -ar 48000 -sn -dn -vn -af "E:\out.mp3"
Note: -y means in advance YES to ffmpeg's yes/no questions such as output file already exist, over write? with -y parameters ffmpeg over writes the output file if it is already exist without asking the user.
-sn disables subtitle, -vn disable video, -dn disable data streams for output file.
If you just want a CLI tool then you could use ffmpeg:
ffmpeg.exe -i test.mp3 -af volume=1.5 loud.mp3
^ ^ ^
input new volume level output name
If you'd like to be able to do it programmatically, looking at your profile I deduced that python should not be a problem :)
So you can use the nice pydub module together with ffmpeg (or avconv which it also supports) for your task.
E.g:
from pydub import AudioSegment
AudioSegment.converter = r"C:\PATH_TO_FFMPEG_DIR\bin\ffmpeg.exe"
sound = AudioSegment.from_mp3("test.mp3") # <- the input file
new = sound.export("loud.mp3", format="mp3", parameters=["-vol", "384"]) # 384 <-> 150% volume
new.flush()
new.close()
The reason for 384 is that the ffmpeg doc states that
-vol volume change audio volume (256=normal)
So 256*1.5 = 384
Tested this on my windows 7 machine just now...
Hope this helps.
The "--volume" option in VLC doesn't actually change the volume of the output video as you would think it would. What you want to do is add the compressor filter and then set the "compressor-makup-gain". Set it to a value from 1-24 depending on how loud you want the video to be. So your command would be something like this:
run_string := "bash -c ""\""c:\Program Files\VideoLAN\VLC\vlc.exe\"" -I dummy \""" . file_path . "\"" --sout='#transcode{acodec=mp3,vcodec=dummy,afilter=compressor}:standard{access=file,mux=raw,dst=\""" . file_path . ".mp3 --compressor-makeup-gain=20\""}' vlc://quit"""
By the way, for anyone who is trying to figure out how to use VLC to increase the volume of the audio in a video file, here's how you can do that:
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" yoursourcefile.mp4 :sout=#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100,afilter=compressor}:file{dst=outputfilename.mp4} :sout-all :sout-keep --compressor-makeup-gain=20
Replace "yoursourcefile.mp4" and "outputfilename.mp4" with your own file names. In my experience, VLC crashed about half the time I ran this command, so you may need to try it more than once if it crashes on you.
Run this on a dir to increasing all files volume on that dir, one by one (or else it would eat up all CPU)
FOR %f IN (*) DO (start /wait "" "C:\Program Files
(x86)\VideoLAN\VLC\vlc.exe" %f
:sout=#transcode{acodec=mp3,afilter=compressor}:file{dst=Boost%f}
:sout-all :sout-keep --play-and-exit --compressor-makeup-gain=10)
I believe mp3gain has a command line option for this. You could run this as a separate pass over the generated file:
http://mp3gain.sourceforge.net/

Resources