The point of this post is to capture the screen on an x11 with webcam overlay in the bottom right corner (as people usually do with obs).
Capture screen with x11grab
On x11 systems, we can capture the screen using -f x11grab
with ffmpeg.
Typically the command looks like
ffmpeg -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 screen-cap.mp4
Where we specify the resolution, frame rate and the offset for recording (here is 0,0 which is the top left corner).
Capture audio along with the screen grab
If we want to include audio as well (in this case we will use pulse).
ffmpeg -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 -f pulse -ac 2 -i default screen-cap.mp4
Using x264 for output
Using libx264
ffmpeg -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 \
-f pulse -ac 2 -i default -vcodec libx264 -preset fast \
-crf 18 -pix_fmt yuv444p screen-cap.mp4
Capturing the webcam input
For webcam; the process is simple.
ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 webcam-cap.mp4
Putting everything together
Let’s mix these two together, the aim is to play the screen capture in the background and setup an overlay of video capture on the bottom right corner.
-filter_complex
is the argument for doing that where we have to pass overlay=withth:height
and the pixel format.
for example
'overlay=<width>:<height>:format=<pixelformat>'
Also instead of hardcoding width
and height
we can use variables main_w-overlay_w
for the width
and main_h-overlay_h
for the height. So when everything is put together the command looks like:
ffmpeg -f x11grab -video_size 1366x768 -framerate 30 -i :0.0 \
-f v4l2 -video_size 320x180 -framerate 30 -i /dev/video0 \
-filter_complex 'overlay=main_w-overlay_w:main_h-overlay_h:format=yuv444' \
-vcodec libx264 -preset ultrafast -qp 0 -pix_fmt yuv444p video.mp4