H.264 multi-pass encoding with FFmpeg

1

I would like to encode a video in H.264 with multi-pass. I tried :

ffmpeg -y -ss 00:00:12.0 -i "c:\vid\example.mkv" -vframes 420 -vcodec libx264 -preset veryslow -refs 8 -b:v 1200k -an -pass 1 -f h264 /dev/null

ffmpeg -y -ss 00:00:12.0 -i "c:\vid\example.mkv" -vframes 420 -vcodec libx264 -preset veryslow -refs 8 -b:v 1200k -acodec copy -pass 2 "c:\vid\encoded.mkv"

but it shows error messages :

/dev/null: No such file or directory

ratecontrol_init: can't open stats file

Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Did I do something wrong? I'm not sure how to use multi-pass with FFmpeg by the way, any ideas how to fix the error?

Happy Face

Posted 2016-01-29T12:02:14.647

Reputation: 209

Are you sure you need to use two-passes? Unless you're targeting a specific output file size just use a single pass with -crf instead. See FFmpeg Wiki: H.264 Video Encoding Guide for more info on that. Why did you add -refs 8? The preset will handle refs. Lastly, you can change -f h264 to -f matroska since that's the container format your second command uses.

– llogan – 2016-01-29T17:21:47.780

@LordNeckbeard If I specify -refs 8, will the preset mess it up? And do you mean crf is better than 2-pass? – Happy Face – 2016-01-29T17:30:22.137

Are you trying to output to a certain file size? You didn't explain why you're using 2-pass or why you're adding -refs. Adding -refs should override the refs set by the preset, but I'm unsure why you're setting that in the first place. – llogan – 2016-01-30T01:39:42.057

@LordNeckbeard Basically I want to encode a video as lossless as possible without having gigantic file size like using -qp 0 or -crf 0. – Happy Face – 2016-01-30T05:38:25.953

Then use the highest crf value that still provides an acceptable quality and the slowest preset you have patience for. However, one of the two answers here should be an appropriate solution to the specific error for the question you posted. – llogan – 2016-01-30T07:55:27.847

Answers

2

Windows doesn't have /dev/null. Use NUL instead.

For more details, see the FFmpeg Wiki post on H.264 encoding.

llogan

Posted 2016-01-29T12:02:14.647

Reputation: 31 929

0

Try

ffmpeg -y -ss 00:00:12.0 -i "c:\vid\example.mkv" -vframes 420 -vcodec libx264 -preset veryslow -refs 8 -b:v 1200k -an -pass 1 -f matroska dummy

ffmpeg -y -ss 00:00:12.0 -i "c:\vid\example.mkv" -vframes 420 -vcodec libx264 -preset veryslow -refs 8 -b:v 1200k -acodec copy -pass 2 "c:\vid\encoded.mkv"

Delete the dummy file created in the first pass after 2nd pass is done.

Gyan

Posted 2016-01-29T12:02:14.647

Reputation: 21 016

1Instead of creating a temporary file, Windows users can use NUL. See @LordNeckbeard's answer. – slhck – 2016-01-30T18:05:32.457

I know, but I missed the file path cue and since there was no express OS indication, I went for the universally safe method. – Gyan – 2016-01-30T19:43:30.480