Changing the codec while keeping the bitrate with ffmpeg

0

So, I have a bunch of .mp4 files and I need to change the video and audio codecs in order to be able to play them on my device.

The video codec is currently h264 and I need mpeg4. The audio codec is aac and I need mp3.

I'm trying to convert them via this ffmpeg command:

ffmpeg -y -i input -s:v 800x480 -c:v mpeg4 -c:a mp3 output

It works fine, except for the quality because ffmpeg is using a very low bitrate.

My question now is if I need to specify a quality/bitrate or if it is possible to tell ffmpeg to use the original bitrate somehow.

-vcodec copy is not an option because the video codec stays the same.

GreenSmurf

Posted 2017-07-27T13:08:26.703

Reputation: 101

Keeping the bitrate would only make sense of you were using the same codec. Instead, you must use a higher bitrate, because mpeg4 is less efficient. – Daniel B – 2017-07-27T13:30:10.437

Default bitrate is 200 kbps. Instead of specifying a bitrate, set a quality factor: -q:v 5 – Gyan – 2017-07-27T13:33:18.257

Thanks. I thought I could save processing power if I somehow kept the bitrate. – GreenSmurf – 2017-07-27T13:50:22.603

No, absolutely not. The video stream will be first decompressed (using H264) and then compressed (using MPEG4 or whatever) again. These processes run completely independent from each other. – Daniel B – 2017-07-27T18:57:59.307

Answers

2

It works fine, except for the quality because ffmpeg is using a very low bitrate.

The defaults for mpeg4 are not well chosen, so the target bitrate is quite low. Specify your own target with -b:v 2M (depending on your resolution) or even better, use constant quality with -q:v 5 (as suggested by Mulvya in the comments). In the latter case, lower values mean better quality.

My question now is if I need to specify a quality/bitrate or if it is possible to tell ffmpeg to use the original bitrate somehow.

No – it wouldn't even make sense to do that. If you are going from one codec to another, they might be less or more efficient. Each codec offers different quality at different bitrates – for example, H.264 is much more efficient than MPEG-4 Part II. H.265 is 30–50% more efficient than H.264, etc. The same goes for the actual encoders implementing the codecs: x264 is more efficient than the reference H.264 encoder, etc.

Therefore, taking the original bitrate may not work well. And even keeping the same codec, if you re-compress, you might want to use an even higher bitrate to avoid generation loss.

slhck

Posted 2017-07-27T13:08:26.703

Reputation: 182 472

1Also different encoder implementations for the same format vary in encoding efficiency (x264 vs openh264 vs AME vs QuickTime vs H.264 hardware encoders, etc). Then there is the effect of re-encoding noisy compression artifacts. And of course there is the human factor: who knows if they person making the original video knew what they were doing in the first place. – llogan – 2017-07-27T22:02:36.977