ShadowKat Studios
Projects
About

webm encoding

I have a number of videos on my site, mostly in the webm format and encoded with VP9 for video and vorbis for audio. I thought I might share how I do my encoding in case it can be useful for someone.

I should note that a lot of the information in this article came from the WebM article on the Install Gentoo wiki.

Test file

For these examples my input will be subnautica-jellyray-base.mkv, a video I recorded with OBS while playing Subnautica. It's a 1680x1050 2.5Mbps video encoded as H264 (H264 - MPEG-4 AVC (part 10) (avc1)), and is about 11 seconds long.

VP9

VP9 is the latest stable format for WebM video. It provides better file sizes at a given bitrate and resolution than VP8, with the downside that it takes forever to encode and isn't supported on most imageboards, which may be a problem.

I use a script in my home directory, vp9.sh, which is set up for two-pass encoding of VP9 at 720 pixels high. As most of my inputs are 16:10 this usually ends up being 1152x720, scaled from 1680x1050.

vp9.sh
\#!/bin/bash  
if [ -z "$3" ]; then  
 bitrate="1M"  
else  
 bitrate="$3"  
fi  
echo Encoding at a bitrate of $bitrate  
ffmpeg -y -i "$1" -c:v libvpx-vp9 -vf scale=-1:720 -pass 1 -qmin 0 -qmax 50 -crf 10 -b:v "$bitrate" -threads 8 -tile-columns 4 -frame-parallel 1 -speed 4 -g 128 -aq-mode 0 -an -sn -f webm /dev/null  
ffmpeg -i "$1" -c:v libvpx-vp9 -vf scale=-1:720 -pass 2 -qmin 0 -qmax 50 -crf 10 -b:v "$bitrate" -c:a libvorbis -b:a 96K -vbr on -threads 4 -tile-columns 4 -frame-parallel 1 -speed 1 -auto-alt-ref 1 -lag-in-frames 25 -g 128 -aq-mode 0 -sn -f webm "$2"

The script is invoked like this: ./vp9.sh input output bitrate, defaulting to 1M for bitrate. For my testing I used ./vp9.sh subnautica-jellyray-base.mkv subnautica-jellyray-base-vp9.webm.

I also timed it:

real    2m23.494s  
user    2m27.100s  
sys     0m0.192s

That is on my E3 1231 v3 box, with 4 cores and 8 threads at 3.4Ghz, and no small amount of RAM. The result?

-rw-r--r-- 1 izaya izaya 1954809 Feb  9 21:36 subnautica-jellyray-base-vp9.webm

So, that's a 1.9MB webm.

VP8

VP8 is the initial format specified in the WebM standard. It provides good quality for a given bitrate, though not as good as VP9.

vp8.sh
\#!/bin/bash  
if [ -z "$3" ]; then  
 bitrate="1M"  
else  
 bitrate="$3"  
fi  
echo Encoding at a bitrate of $bitrate  
ffmpeg -y -i "$1" -c:v vp8 -vf scale=-1:720 -pass 1 -qmin 0 -qmax 50 -crf 10 -b:v "$bitrate" -threads 8 -tile-columns 4 -frame-parallel 1 -speed 4 -g 128 -aq-mode 0 -an -sn -f webm /dev/null  
ffmpeg -i "$1" -c:v vp8 -vf scale=-1:720 -pass 2 -qmin 0 -qmax 50 -crf 10 -b:v "$bitrate" -c:a libvorbis -b:a 96K -vbr on -threads 4 -tile-columns 4 -frame-parallel 1 -speed 1 -auto-alt-ref 1 -lag-in-frames 25 -g 128 -aq-mode 0 -sn -f webm "$2"

Invoking it is the same as vp9.sh: izaya@asakura:~$ time ./vp8.sh subnautica-jellyray-base.mkv subnautica-jellyray-base-vp8.webm

...  

real    0m18.689s  
user    0m46.928s  
sys     0m3.960s  
izaya@asakura:/export/Marcus/Videos/Subnautica$ ls -l subnautica-jellyray-base-vp8.webm   
-rw-r--r-- 1 izaya izaya 2530240 Feb  9 21:54 subnautica-jellyray-base-vp8.webm

The result is a 2.5MB webm in 1/8th the time it took to encode the VP9 video, and the final comparison of file sizes is:

3.9M subnautica-jellyray-base.mkv  
2.5M subnautica-jellyray-base-vp8.webm  
1.9M subnautica-jellyray-base-vp9.webm

I'm going to say, encode as VP9 if you have the time and processing power, unless you need to post the webm on 4chan.

By Izaya
2018/02/09 21:22 +1100

Tags: tech linux video

© ShadowKat Studios
The software used to generate this page is licensed under the GNU GPL version 3 and can be found here