S
S
Sergey Kostikov2020-06-14 13:39:19
Video
Sergey Kostikov, 2020-06-14 13:39:19

What is the best way to compress video in ffmpeg?

In general, there is a video library and the task is to compress each video up to 1.5GB with minimal loss.
I already have all these codecs / presets / crf / double passes and other garbage that makes my head swell (I have never dealt with video encoding before)
Please advise the appropriate ffmpeg settings for my task. Encoding time is not important (as long as it's not completely terrible like av1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@nidalee, 2020-06-14
@sergo1217

The safest way would be CRF with a bitrate limit:

ffmpeg -i input -c:v libx264 -preset veryslow -crf 23 -maxrate X -bufsize 2M output.mp4

Where X is the maximum bitrate in megabits per second, for example 6M is 6 megabits per second. Now we consider our limit for these same megabits:
Bitrate in mega bits = file size in gigabytes / (number of minutes * .0075 )

For an hourly file with a 1.5 gigabyte limit, that's 1.5 / (60*0.0075) = 3.3 megabits per second. Do not forget that at least 128 kilobits must be left for sound. They will have to be subtracted from the maximum bitrate for the video. Here is a calculator.
Then:
ffmpeg -i input -c:v libx264 -preset veryslow -crf 23 -maxrate 3M -bufsize 2M -acodec aac -b:a 128k output.mp4

This, in fact, is very small, but it will definitely fit into the limit. I think the bitrate is decent enough for most files to be around 10 megabits per second, but in fact it all depends on the picture, if there is a lot of static, then 3 may be enough .. Well, I hope that your files are still not hourly . :)
The same thing, but with bitrate instead of CRF, you can also try and compare the results:
ffmpeg -i input -vcodec libx264 -preset veryslow -b:v 3M -pass 1 -an -f mp4 NUL
ffmpeg -i input -vcodec libx264 -preset veryslow -b:v 3M -pass 2 -acodec aac -b:a 128k output.mp4
This goodness goes in two consecutive lines to the bat-file, it must be executed one after the other.
Using HEVC (H.265) is probably not practical for 1080P. But you can try:
ffmpeg -i input -c:v libx265 -preset slower -crf 23 -maxrate 3M -bufsize 2M output.mp4

If it suddenly turns out that the average bitrate of the received file is below the limit, then it was limited by the target CRF quality. Then the quality can be improved, for this the figure needs to be lowered. For example, -crf to 21.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question