ffmpeg: how to resample audio file - audio

I need to convert a 44KHz stero m4a audio file to 22KHz mono mp3 VBR file, how can I do that with ffmpeg on linux terminal? Thanks.

Gyan's comment is what I want, here is the full command line:
ffmpeg -i in.m4a -ac 1 -ar 22050 -c:a libmp3lame -q:a 9 out.mp3
with the option for VBR encoding. The number after -q:a specifies encoding quality (bitrate), with 0 being the best quality (largest file) and 9 being the worst quality (smallest file).
Here is the document on ffmpeg wiki.

Related

How to convert AC3 audio to Wav audio?

I would like to convert a AC3 audio file (ATSC A/52 aka AC-3 aka Dolby Digital stream 6 channels) to Wave audio file (16khz mono/1 channel).
While searching on the internet, a lot of people just used ffmpeg -i file.ac3 file.wav however, i'm not sure if that even works.
I keep getting
[ac3 # 0x55ac1a0b0660] exponent -1 is out-of-rangets/s speed= 125x
[ac3 # 0x55ac1a0b0660] error decoding the audio block
[ac3 # 0x55ac1a0b0660] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
etc
while I do the same command.
How do I convert ac3 to wav (16khz mono)?
*Note:
I also tried ffmpeg -i file.ac3 -codec:a:1 ac3 -codec copy -b:a 384 file.wav -ac 1 -ar 16000. But this doesn't output an actual wav file.
ffmpeg -i file.ac3 -vcodec copy -acodec pcm_s16le -ar 16000 -ab 128k -ac 1 file.wav should do it!
Also you can convert Eac3 to wav in high quality mode.
The wav file will be 48KHz 24Bit 6 Channels.
ffmpeg -i "input.eac3" -acodec pcm_s24le -ar 48000 -ac 6 "output.wav"
If you want to export in 8 channels, just write 8 instead of 6.

FFmpeg output video file much smaller than uncompressed input audio file, using option to preserve original audio quality

I attempt to create a video slideshow from a number of image files and an audio file in 2 steps:
Create a temporary video file from a sequence of image files
Add an audio file to the temporary video file with a delay of 5 seconds
The audio file is an uncompressed stereo wav file, encoded with a sample rate of 44100 Hz and a bit depth of 32 bits, with a size of 40.1 MB. To preserve the lossless quality of the input audio file I use the option -c:a aac -b:a 192k as per Slideshow Wiki. However, the final output video file has a size of only 4.49 MB.
How can the output video file be about 10 times smaller than the input audio file and still preserve the original lossless quality?
My code:
ffmpeg -f concat -i slide-sequence.txt -c:v libx264 -r 30 -filter_complex format=yuv420p temp.mp4
ffmpeg -i temp.mp4 -i audio.wav -af "adelay=5000|5000" -c:v copy -c:a aac -b:a 192k out.mp4
How can the output video file be about 10 times smaller than the input audio file and still preserve the original lossless quality?
It does not. AAC is a lossy format. It uses encoding methods to make it sound good although it is lossy.
There are formats that are both compressed and lossless, such as FLAC. YouTube supports this, so use:
ffmpeg -i temp.mp4 -i audio.wav -af "adelay=5000|5000" -c:v copy -c:a flac out.mkv
Note the change of the output container format from MP4 to Matroska (.mkv). YouTube supports Matroska.

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)

convert m4a to WAV file (containing signed 16-bit PCM samples) in ffmpeg

I have a MP4a file which I am looking to convert to WAV file, containing signed 16-bit PCM samples. I have ffmpeg at my disposal, and looking at previous SOF posts, I have tried:
ffmpeg -y -i input.mp4 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 output.pcm
but, the program I use complains that this converted file has data in unknown format. I was wondering if anyone had any pointers on how to go from m4a to wav with pcm samples.
ffmpeg -i input.mp4 output.wav
This command will output WAV file containing signed 16-bit PCM samples. Your command is outputting raw PCM, not WAV.
You can add -c:a pcm_s16le output option if you prefer, but that's the default encoder for WAV so it can be omitted.

How to convert High bitrate MP3 to lower rate using FFmpeg

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

Resources