Requirement: ffmpeg must be compiled with the
Similar to the high-quality Gif guide we cut the input Video down to it’s relevant parts, remove any audio, pick an appropriate frame-rate and scale it down for the use-case:
ffmpeg -ss <start> -i <video-file> -t <length> -filter_complex "[0:v] fps=30,scale=620:-2" -map v cut.mp4
A few things are happening in this single command, lets break it down:
- Before the input, we specify the seek-position (absolute) to start at.
- Then, the input file is specified
- After the input-file, the length of the output is specified (relative)
- Then, apply a filter to create a new video output stream which…
- takes the only video stream from the first input file (the Video) …
- sets it’s FPS to
- scales it down to
620pxwidth, keeping the aspect ratio (by specifying
-2for height. We use
-2here to guarantee that the height is dividable by two).
- For the output, we discard everything except the single filtered video stream.
- The output is then written to the
This time, no palette is required. Instead, since we’re using WEBM v9, a double pass encoding is recommended for better quality and smaller files:
# First pass (note that the output file is NOT required, only the .log file that is also created in this step!) ffmpeg -i cut.mp4 -c:v libvpx-vp9 -pass 1 -b:v 1000K -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1 -an -f webm discard.tmp # Second pass (which ouputs the final file) ffmpeg -i cut.mp4 -c:v libvpx-vp9 -pass 2 -b:v 1000K -threads 8 -speed 1 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -f webm out.webm
The first command runs the first pass (denoted by the
-pass 1 argument). It’s output file can be discarded, only the
ffmpeg2pass-0.log (or similar) file which is also created is important. It will be used implicitly on the second pass!
The second command runs the final second pass (denoted by
-pass 2 argument). Here, the information from the first pass is used to create the final output file.
All additional arguments for both passes are taken from Googles official recommendation for VOD, which I’ll repeat here for prosperity:
c:v libvpx-vp9tells FFmpeg to encode the video in VP9.
b:v 1000Ktells FFmpeg to encode the video with a target of 1000 kilobits.
- Most of the current VP9 decoders use tile-based, multi-threaded decoding. In order for the decoders to take advantage of multiple cores, the encoder must set
lag-in-frames >= 12will turn on VP9’s alt-ref frames, a VP9 feature that enhances quality.
speed 4tells VP9 to encode really fast, sacrificing quality. Useful to speed up the first pass.
speed 1is a good speed vs. quality compromise. Produces output quality typically very close to
speed 0, but usually encodes much faster.
- Multi-threaded encoding may be used if
-threads > 1and
-tile-columns > 0