简体   繁体   中英

ffmpeg how to convert audio to aac but keep bit rate at what the old file used?

I don't expect this to happen often, but while re-encoding video files via batch file to h265 I'm checking to make sure the audio is in aac. If it isn't then I want to convert to aac, but keep the bit rate at what ever the old file uses since if I just convert to aac ffmpeg is going to use the default 128kbps value. For any old videos I have the bit rate is probably going to be lower than that so upconverting is going to increase the file size a little.

Is there any way to convert to aac but keep the old bit rate?

Here's what I was trying but it keeps converting the old mp3 89kbps stream to aac 128 kbps:

ffmpeg -i test.mp4 -acodec aac -vcodec copy test.aac.mp4

Note that above is just for test purposes, I am actually converting the video.

Note 2: My question isn't at all similar to the other question that it has been suggested as similar to. I have no trouble storing ffprobe results in variables nor did I even mention that.

You could detect the bitrate of the audio stream from your input file using ffprobe , and then depending on the output from that command run the appropriate FFMPEG command.

Here's a small bash script that will detect the bitrate on the audio stream and if it is less than 128Kbps just use that original bitrate during conversion. This should avoid up sampling:

#!/usr/bin/env bash
AUDIO_BITRATE=`ffprobe -v error -select_streams a:0  -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $1`
if [[ $AUDIO_BITRATE < 128000 ]]; then
  ffmpeg -i $1 -acodec aac -ab ${AUDIO_BITRATE}k -vcodec copy new-$1
else
  ffmpeg -i $1 -acodec aac -vcodec copy new-$1
fi

Alternatively if you need to convert into other video formats and don't have FFMPEG installed you could use a commercial conversion API such as Zamzar .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM