🇩🇪 Deutsch

01 Sep 2023

Reduce Video Size

Es gibt viele Möglichkeiten, eine Video-Größe zu redizieren ohne merklichen Qualitätsverlust.


I’m not using ffmpeg, but mplayer and mencoder. First, We have to demux the audio with mplayer:

 mplayer -vo null -ao pcm:fast:file=<audio_pcm.wav> <video>

The -vo null and -ao null parameters tells mplayer to not extract video.

In the next steps, we’ll do a 3-pass compression with mencoder. At the first pass we’ll choose a suitable Constant Quality Mode compression (crf parameter) as a start point:

 mencoder <video> -ovc x264 -x264encopts ratetol=100:preset=veryslow:crf=<value>:pass=1  -nosound -o video1.h264

You can add slow_firstpass parameter to the -x264encopts if you are paranoid with the final quality of the video. Mencoder manual says that this option disable some parameters that “s>

You should try several values for crf - try starting from 25 and goes on increasing it until you note artifacts at the resulting video (higher values compresses more). Remember subseq>

Alternatives for the veryslow preset are slower, slow, medium etc. See mencoder manual for the complete list.

ratetol controls the bitrate variation - I’m not sure if I’m doing the right thing here, but I set it to the maximum value in order to let total freedom to mencoder to choose the righ>

After the first pass, you’ll note that the last line gives you the average bitrate you will use at the next steps:

  (...)
  x264 [info]: kb/s:526.43

Change the crf parameter, recommended at the first pass, to bitrate, required at the subsequent passes:

 mencoder <video> -ovc x264  -x264encopts slow_firstpass:ratetol=100:preset=veryslow:bitrate=526:pass=3  -nosound -o video2.h264

This second pass encoding will read the statistics generated at the first pass (divx2pass.log and divx2pass.log.mbtree) in order to optimize the compression.

Note you’ll use the same video input, not the generated by the first pass - first pass’ output video is only useful to check the initial quality.

Note also that the pass=3 (not pass=2) will generate a new statistics file, so you can repeat the last step as many times you want. I usually do pass=3 twice, always paying attention >

Meanwhile, you can compress the audio too, using lame or oggenc:

 oggenc -q<n> <audio_pcm.wav>

Finally, we’ll remux audio and video

 mencoder -audiofile <audio>.ogg video2.h264 -oac copy -ovc copy  -of lavf -lavfopts format=mp4 -o <video>.mp4

The -of lavf -lavfopts format=mp4 generates mp4 file format using the lavopts muxers.