11

I am converting a video using the following command:

ffmpeg -i "input.avi" -vcodec mpeg4 -r 15 -sameq "output.avi"

However I'm only interested in the file size of output.avi. Is there a command I can give ffmpeg so that it doesn't actually write a file, but tells me how big it would be?

2 Answers 2

12

You can't really estimate the size the of a video file before encoding. This is not possible due to the way the codec works. It allocates a certain amount of bits to every frame, but this amount usually depends on the contents of the frame itself.

However, there are some solutions.


1. The cheap one:

Let it encode, check the file size and trash the file again

2. The "workaround":

Calculate the file size yourself using a simple calculator. This only works when using constant bit rate. Specify it with, for example, -b 500k for 500 kBit/s. You have to make sure that you specify a bit rate the codec can use. for example, there's no way to compress a 1080p video with 200k bit rate in mpeg4, because it would need much more than that.

3. The "real" solution:

The last option I had in mind was piping the output to /dev/null and measuring the piped file size. That, however, won't work for all formats, because FFmpeg needs a seekable file to produce valid output.

Still, for AVI, it works pretty well. The following command will pipe into /dev/null, thereby leaving no trace of the file itself, and finally output the encoded file size.

ffmpeg -i input.avi -vcodec mpeg4 -b 3M -f avi pipe:1 | pv > /dev/null

… for example like this:

5.42MB 0:00:10 [ 521kB/s]

What does it do?

  • You have to specify the format using -f avi. Otherwise FFmpeg won't know which format to use.
  • pipe:1 tells FFmpeg to write the output to a pipe.
  • We will feed this output into pipe viewer, short pv.
  • Pipe viewer will measure the transferred size and output your video to /dev/null.

The only minor drawback is that the output looks a bit weird until the video is finished. I haven't yet found a way to fully disable FFmpeg's output and get pv to work with this.

0
0

If what you're concerned about is speed/wear of writing to disk, you can consider a RAM mounted volume instead. They're easy to set up on Linux, but maybe not so much on Windows. The advantage is that it'd work for all formats and allows for copying to disk afterwards rather quickly. The downside would be requiring at least as much spare RAM as the converted video.

As mentioned in the accepted answer, the size of a video using a varying bitrate codec cannot be calculated upfront, otherwise.

3
  • 1
    What question are you answering?
    – Toto
    Nov 13, 2023 at 13:18
  • What answer are you commenting on?
    – Unknown
    Nov 13, 2023 at 13:23
  • Thanks Unknown, wear isn't the concern, but thanks for the tip anyway!
    – Fidel
    Nov 14, 2023 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .