Convert a video by setting the final size

9,383

Going from 100mb to 10mb is a slightly unrealistic 90% drop in size but I will give an example of reducing to 50mb creating an H.264 video, which is a more reasonable 50% reduction in size as well as reducing to 40mb using HEVC.

I am using the following sample file:

wget https://web.archive.org/web/20190412004321/http://dl3.h265files.com/TearsOfSteel_720p_h265.mkv

You can use this sample file to confirm the following results that I have given and perhaps experiment a little further yourself. MediaInfo reveals the following for this downloaded file:

mediainfo \
--Inform="General;Duration=%Duration/String3%\nFile size=%FileSize/String1%" \
TearsOfSteel_720p_h265.mkv
Duration=00:12:14.058   <-----
File size=101 MiB       <-----

The arrows of course are my own! You then perhaps have 2 really good choices:

  1. Re-Encode to H.264
  2. Re-encode to HEVC

1. Re-Encode to H.264 (50mb)

H.264 is widely accepted now and would be an excellent choice for your output video file. The formula to calculate the output bitrate for the desired 50mb would then be:

(50 MiB * 8192 [converts MiB to kBit]) / 734 seconds = ~558 kBit/s total bitrate
558 - 128 kBit/s (desired audio bitrate) = 430 kBit/s video bitrate

To accomplish this use the following FFmpeg 2 pass command:

ffmpeg -y -i TearsOfSteel_720p_h265.mkv \
      -c:v libx264 -b:v 430k -pass 1 \
      -c:a libmp3lame -b:a 128k -f mp4 /dev/null && \
ffmpeg -i TearsOfSteel_720p_h265.mkv \
       -c:v libx264 -b:v 430k -pass 2 \
       -c:a libmp3lame -b:a 128k TearsOfSteel_smaller.mp4

The resulting file size is 49.4MiB with quite reasonable, but not amazing, viewing quality. Pretty good for a 50% reduction in file size and a huge drop in video bitrate actually!

2. Re-encode to HEVC (40mb)

Another choice (as suggested by emk2203) is to re-encode the existing HEVC stream with a lower bitrate, this time aiming for 40mb as HEVC claims better quality at a lower bitrate.

This time the formula would be:

(40 MiB * 8192 [converts MiB to kBit]) / 734 seconds = ~446 kBit/s total bitrate
446 - 128 kBit/s (desired audio bitrate) = 318 kBit/s video bitrate

and the FFmpeg 2 pass command line is:

ffmpeg -y -i TearsOfSteel_720p_h265.mkv \
      -c:v libx265 -x265-params pass=1 -b:v 318k \
      -c:a libmp3lame -b:a 128k -f mp4 /dev/null && \
ffmpeg -i TearsOfSteel_720p_h265.mkv \
       -c:v libx265 -x265-params pass=2 -b:v 318k \
       -c:a libmp3lame -b:a 128k TearsOfSteel_smaller.mp4

And this provides a 40mb file with quite reasonable quality.

In conclusion:

From these example you can experiment further by decreasing the required MiB in the formula and observing the subsequent viewing quality. Have Fun!!

References:

Share:
9,383
Asme Just
Author by

Asme Just

Updated on September 18, 2022

Comments

  • Asme Just
    Asme Just almost 2 years

    Is there a way to set the maximum final size for video converting using ffmpeg (or any other CLI based video converter)?

    Like if I have an 100mb video and want to convert it to a 10mb video with the highest quality possible, considering the final format isn't important.

  • Asme Just
    Asme Just about 6 years
    Can you give an example please? Notes, The quality isn't really important for me, Even though I want it to be the highest possible.
  • Asme Just
    Asme Just about 6 years
    Any comment about the libx264 video codec? The rest is well detailed.
  • andrew.46
    andrew.46 about 6 years
    @AsmeJust Added some comments :)
  • emk2203
    emk2203 about 6 years
    Going to the old H.264 codec is not a good idea in this situation. H.265 was especially made for bitrate-starved situations like this one, where OP wants to compress to 10% of the original size. So, a better solution would be to keep H.265 with a 'lib265' part in the ffmpeg lines. OPs demands here make H.265 practically mandatory. It's the only format which can do extreme compression and still retain a modicum of image quality.
  • andrew.46
    andrew.46 about 6 years
    @emk2203 Good point. Mind you if you do the maths for a 10mb encode for this file you will see that the target bitrate for either H.264 or HEVC is actually negative 16MiB :). I will however add an example in for HEVC re-encoding after finding the sweet spot for file size and at least respectable video quality...
  • andrew.46
    andrew.46 about 6 years
    @emk2203 Added an example in....
  • Asme Just
    Asme Just about 6 years
    @andrew.46 It works as expected, I've few additional questions though, How does reducing the frame rate affect the quality? Example if I set the fps to 10 from the normal ~24 , which is less than half, does this reduce the file size to at least half considering the b:v isn't changed? I know 10fps is too low but it don't matter that much for certain video like diapo.../tutorials where even 5fps will be fine. And Is there any tips to make the conversion a little faster? This is way too slow, specially for H.265
  • andrew.46
    andrew.46 about 6 years
    @AsmeJust H.265 is slow unfortunately, this is a feature :). As for framerate: you will have to experiment a little I suspect. For a faster encode you can experiment with the -threads option: askubuntu.com/q/471687/57576. But best option for video encoding is always: get a faster computer :) It is a very hungry process...
  • Asme Just
    Asme Just about 6 years
    @andrew.46 I just test for a smaller frame rate on a 3MiB H.265 file using this: ffmpeg -i input.mp4 -c:a copy -c:v vp9 -r 8 output.mp4 and get a 705KiB at 8 fps without any drop in quality, which is great. H.265 is really awsome.
  • andrew.46
    andrew.46 about 6 years
    @AsmeJust Good to see you exploring! Mind you -c:v vp9 should create a WebM file using libvpx; this is the competitor to HEVC. This is a choice I left out because as you see it can get complicated fast :).
  • Asme Just
    Asme Just about 6 years
    @andrew.46 I just find out for vp9 while looking for HEVC alternative. My bad...
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 6 years
    Just a thought... what if you converted it to a GIF?
  • andrew.46
    andrew.46 about 6 years
    @WinEunuuchs2Unix An interesting thought :). I read up here: engineering.giphy.com/how-to-make-gifs-with-ffmpeg but I suspect that you have the beginnings of a new question there...
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 6 years
    @andrew.46 So true. I had just made a cell phone video of grub booting in three different distros to upload here where image file size is capped at 2MB. The videos are 30MB for 20 seconds and was thinking of converting them to GIF so they would fit here.
  • jave.web
    jave.web over 5 years
    you may want to sudo apt install ffmpeg if not already installed, also if you copy the command to ONELINE, don't forget to remove ` back-slashes. And final note - the 2 ffmpeg calls are actually 1 command input (they are joined with &&`).
  • Würgspaß
    Würgspaß over 3 years
    @andrew.46 Ace, man! I was able to shrink videos by 40:1 from 1GB to 25 MB without mentionable loss of quality. Thank u.
  • andrew.46
    andrew.46 over 3 years
    @Würgspaß Great news! It is messages like yours that keep me going on AskUbuntu :)
  • Smeterlink
    Smeterlink over 3 years
    If you are on Windows ffmpeg, use NUL instead of /dev/null