lossless-screen-recording.md

 1---
 2title: Lossless screen recording
 3subtitle: Never waste resources with OBS again
 4author: Amolith
 5description: Recording your screen (or monitors) with ffmpeg for a high-quality lossless video that uses very few system resources
 6date: 2018-08-12T17:15:20-04:00
 7draft: false
 8cover: /assets/pngs/ffmpeg-lossless.png
 9categories:
10  - Technology
11tags:
12  - FFmpeg
13  - CLI
14  - Minimalism
15---
16
17I've been trying off and on for the past few weeks to figure out how to
18record my 1920x1080 monitor. The recording is going to be some music
19videos for a friend. Originally, it was just going to be a single
20background image for the whole video then I had the idea of using
21[cava](https://github.com/karlstav/cava) in a transparent terminal on
22top of the background. This didn't work at all because it actually kept
23freezing when I tried to record it. So I tried switching to
24[ncmpcpp's visualiser.](http://ncmpcpp.rybczak.net/) This still had
25horrible lag so I've been puzzling over how to use ffmpeg to
26*losslessly* record my second monitor. The reason OBS and similar screen
27recorders are so slow is because, most of the time, they encode to the
28end format while recording and that uses a lot of system resources. I
29finally figured it out and have pasted the command below.
30
31``` bash
32ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -draw_mouse 0 -i :0.0+1366,0 -c:v libx264 -crf 0 -preset ultrafast output.mkv
33```
34
35Above is exactly what I used for my 1080p monitor with 768p laptop
36screen. I've modified the command so you can see what you need to edit
37for your use-case.
38
39``` bash
40ffmpeg -video_size <target-resolution> -framerate 30 -f x11grab -i :0.0+<width-of-unused-monitor>,0 -c:v libx264 -crf 0 -preset ultrafast <filename>.mkv
41```
42
43If you do *not* want the cursor recorded, add `-draw_mouse 0` directly
44after `x11grab` like I did in the first command.
45
46My video was 470mb for a ~13 minute video. If you're going to archive
47the recording or are concerned about file size, re-encode it with a
48slower preset. This will be a lot slower and take a lot of CPU but the
49resulting file is *significantly* smaller than the original and still
50lossless. I this as a general purpose screen recorder. Previously, I was
51using OBS and the lag in the video was incredible but with ffmpeg, it's
52smooth as butter. The command for re-encoding is below:
53
54``` bash
55ffmpeg -i output.mkv -c:v libx264 -crf 0 -preset veryslow output-smaller.mkv
56```
57
58![](/assets/gifs/ffmpeg-lossless.gif)
59
60## Note
61This command only works with X, not Wayland. Skimming `ffmpeg`'s man
62page, I see that `video4linux2` is another option for capturing video so
63you may be able to replace `x11grab` with it for the same result. I have
64not tested this so I don't know if it'll work or not.