Tuesday 27 August 2019

Raspberry Pi 4 ffmpeg screen recording using x11grab

Introduction

This page provides simple instructions on how to screen record videos on your Raspberry Pi 4 using nothing more than ffmpeg .

This ffmpeg approach is favoured over using the more common graphical tools for screen recording, as ffmpeg provides greater user control. Not all screen recorders work well with the Raspberry Pi 4.

One interesting finding is that the h264_omx video encoder seems to work a lot faster when the input and output are matroska container files mkv .

The h264_omx video encoder is noticeably slower when the container output file is an mp4 . It is therefore suggested to copy the final h.264 encoded video from a mkv to mp4 container as the final step and to do all video transcoding between mkv container files.

With this ffmpeg based screen capture method the user can create a series of screen recordings that can be combined to create a YouTube video etc using a video editor such as Shotcut.


ffmpeg


echo $DISPLAY
:1

ffmpeg -f x11grab  -s 1824x984 -i :1.0 -r 30 -c:v mpeg2video -b:v 6M -f matroska -y mytest-6MB-r30.mkv 




Method

Key points to remember are:

  1. Use the MKV matroska container
  2. Use the mpeg2video video codec for original screen recording
  3. Convert to mp4 and h.264 (if desired) as later steps

Note these instructions consider video only, they would need to be expanded to included audio. See bottom of page for further details.

ffmpeg -f x11grab  -s 1920x1080 -i :0.0 -r 30 -c:v mpeg2video -b:v 6M -f matroska -y mytest-6MB-r30.mkv

This example uses a 6 megabit video bit rate and a frame rate of 30 frames per second (fps). See ffmpeg documentation. The command also records the full screen.  

Now convert to h.264 using the GPU hardware encoder h264_omx using a 3 megabit video bit rate. Note the movflags and faststart are flags are optional but enable the file to start up quickly without the need for the entire file to download first.

ffmpeg -i mytest-6MB-r30.mkv -c:v h264_omx -b:v 3M -movflags +faststart -f matroska -y mytest-3MB-r30-h264_omx.mkv

Alternatively, consider 2 pass encoding which produces higher quality video but takes longer to encode:

ffmpeg -i mytest-6MB-r30.mkv -c:v h264_omx -b:v 3M -pass 1 -y -f matroska /dev/null && ffmpeg -i mytest-6MB-r30.mkv -movflags +faststart -c:v h264_omx -b:v 3M -pass 2 -f matroska mytest-3MB-r30-h264_omx.mkv 

Finally copy the video encoded to the mp4 container if desired:

ffmpeg -i mytest-3MB-r30-h264_omx.mkv -c:v copy -f mp4 -y mytest-3MB-r30-copy.mp4


That is it!



Further Help

For further advice, including recording audio at the same time as video please see the below links.

ffmpeg Capture Desktop wiki


How to get near-perfect screen recording quality?

How to make an MPEG2 video file with the highest quality possible using FFMPEG?


Screen recording (in X11) with ffmpeg

An Overview of H.264 Advanced Video Coding

Video Encoding Settings for H.264 Excellence (good discussion)