Slow VP8 and VP9 encoding with ffmpeg

Kirill K picture Kirill K · Jun 30, 2017 · Viewed 9.4k times · Source

I saw this answer, but it's a little old. Maybe the situation has changed?

I want to re-encode a stream from an IP camera to WebM (VP8 or VP9) format with ffmpeg. I need real time speed, but my CPU is a Core i5 (2017) and too busy (load avarage too more 100%).

  • Can I buy hardware that is better suited for such an encoding task?

  • What parametres for ffmpeg are recommended for transcoding in realtime?

At the moment I'm using this command (with overlay chroma key):

./ffmpeg \
-i \
bg.jpg \
-thread_queue_size 512 \
-rtsp_transport tcp -i rtsp://ip_cam:port/stream \
-codec:v libvpx -quality realtime -r 25 -crf 30 \
-b:v 2M -qmin 10 -qmax 50 -maxrate 2.5M -bufsize 5M \
-speed 1 \
-b:v 2M \
-cpu-used 0 -threads 4 \
-auto-alt-ref 0 \
-c:a libopus -b:a 96k \
-filter_complex "[1:v]chromakey=0x70de77:0.1:0.0[ckout];[0:v][ckout]overlay[out]" \
-map "[out]" \
-f webm udp://ip_destination:1935/name/stream

Answer

slhck picture slhck · Jun 30, 2017

The speed/quality options for VP8/VP9 are explained in the documentation. Note that in ffmpeg, you have to specify the parameters differently (see ffmpeg -h encoder=libvpx-vp9):

  • CPU Usage:
    • ffmpeg: -cpu-used (legacy option: -speed)
    • libvpx: --cpu-used
  • Quality / Deadline:
    • ffmpeg: -deadline realtime, -deadline good (legacy option: -quality)
    • libvpx: --rt, --good

The -cpu-used should be your main control knob. While the default is 0, the documentation says that:

Setting --cpu-used=1 or --cpu-used=2 will give further significant boosts to encode speed, but will start to have a more noticeable impact on quality and may also start to effect the accuracy of the data rate control.

Setting a value of 4 or 5 will turn off "rate distortion optimisation" which has a big impact on quality, but also greatly speeds up the encoder.

For live encoding particularly, you want to set -deadline realtime:

--rt Real-time mode allows the encoder to auto adjust the speed vs. quality trade-off in order to try and hit a particular cpu utilisation target. In this mode the --cpu-used parameter controls the %cpu target as follows:

target cpu utilisation = (100*(16-cpu-used)/16)%

Legal values for -cpu-used when combined with --rt mode are (0-15).

It is worth noting that in --rt mode the encode quality will depend on how hard a particular clip or section of a clip is and how fast the encoding machine is. In this mode the results will thus vary from machine to machine and even from run to run depending on what else you are doing.

But of course, with an i5 CPU, depending on how many parallel transcoding tasks you have and what level of quality you want to reach, and what the final latency should be, investing into a beefy CPU from the latest Intel i7 series would make sense.

Intel's Kaby Lake chips apparently support hardware-assisted encoding through Intel QuickSync, and ffmpeg supports that through VA-API.