Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg conversion - keep audio bitrate

I'm using ffmpeg to extract the audio from different video formats (flv, mp4) and convert it to mp3.

%~dp0ffmpeg.exe -i %1 -ar 44100 -ac 2 -ab 128k "%~dpn1.mp3"

This works just fine. However, in my input files, the audio bitrate varies, and I want to adjust the output bitrate accordingly. Even by extensive Google searching, I didn't find any hint how to just keep the original bitrate.

What I would need would be something like:

-ab copy

Which, of course, does not work.

Is there anything that will work?

P.S: As you might have figured from the formatting above, I'm using a windows batch file. There would be the hack to use %~dp0ffmpeg.exe -i, get the audio bitrate by grep and insert it in the command line. I just think there has to be an easier and more elegant way.

like image 627
clausvdb Avatar asked Jun 25 '12 20:06

clausvdb


People also ask

Does FFmpeg reduce audio quality?

For the most part, audio problems stem from DRM and losses in quality. While FFMPEG can't always help with DRM, it can help you to convert your files without losing quality.

How do I set bitrate in FFmpeg?

To set the bitrate target in FFmpeg, use the -b:v code (bitrate:video) below : ffmpeg -i Test_1080p. MP4 -c:v libx264 -b:v 5000k Test_DR_5M.

Is FFmpeg lossless?

ffmpeg - Lossless universal video format - Super User. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Can FFmpeg convert audio?

FFmpeg is a great tool for quickly changing an AV file's format or quality, extracting audio, creating GIFs, and more.


1 Answers

even though the original thread was looking for an answer without grepping anything, nate's script seems to be the most useful post. but it has some limitations, for example not all outputs give you a bitrate grepped, some turnout to give you just the result "default". here's a little more improved version of it.

#!/bin/env bash
ext=$1
for f in *.${ext}; do
    x=${f%.*} ;
    x=${x% - YouTube}; # I usually download some song covers from YouTube.
    x=$x".mp3";
    bit=`ffmpeg -i "${f}" 2>&1 | grep Audio | awk -F", " '{print $5}' | cut -d' ' -f1`
    if [ -n "$bit" ]; then
        ffmpeg -i "$f" -ab ${bit}k "$x"
    else
        ffmpeg -i "$f" "$x" # this is if we don't have an output from the grep line above, ffmpeg will keep the original quality, that is 192k for 192k
    fi        
done
like image 51
eaydin Avatar answered Sep 27 '22 22:09

eaydin