[FFmpeg-user] Understanding two pass encoding

Moritz Barsnick barsnick at gmx.net
Thu Aug 6 16:26:52 CEST 2015


Hi Robin,

On Thu, Aug 06, 2015 at 19:42:39 +0530, Robin Lery wrote:
> 2nd pass has more frames than 1st pass

Because you didn't do a first pass. ;-)

> So, my concern is how will ffmpeg know which one is the first pass so that
> it can do its pass 2 encoding?

ffmpeg stores its findings from the first pass in a log file, and
reuses that during its second pass.

> And since many videos will be uploaded simultaneously I can't keep
> them all at the /dev/null as it will replace one with another. So how
> can I manage it?

If your Unix system is set up correctly, /dev/null has plenty of space
for holding _all_ the first pass videos. ;-) (If you don't get the
joke: Read up on /dev/null.)

The problem will be that the log files will interfere with each other.
Please do read ffmpeg's fine documentation on 2-pass encoding:

‘-pass[:stream_specifier] n (output,per-stream)’

    Select the pass number (1 or 2). It is used to do two-pass video
    encoding. The statistics of the video are recorded in the first
    pass into a log file (see also the option -passlogfile), and in the
    second pass that log file is used to generate the video at the
    exact requested bitrate. On pass 1, you may just deactivate audio
    and set output to null, examples for Windows and Unix:

      ffmpeg -i foo.mov -c:v libxvid -pass 1 -an -f rawvideo -y NUL
      ffmpeg -i foo.mov -c:v libxvid -pass 1 -an -f rawvideo -y /dev/null

‘-passlogfile[:stream_specifier] prefix (output,per-stream)’

    Set two-pass log file name prefix to prefix, the default file name
    prefix is “ffmpeg2pass”. The complete file name will be
    ‘PREFIX-N.log’, where N is a number specific to the output stream

This means if you do parallel encoding, you need to give each encode a
unique file name prefix with "-passlogfile". You can generate unique
file names (or prefixes) using "mktemp". For a web app like yours, I
would alternatively probably generate unique temporary directories, and
operate within them.

HTH,
Moritz


More information about the ffmpeg-user mailing list