HOWTO: recording desktop video with ffmpeg

Post tutorials, HOWTO's and other useful resources here.
User avatar
M. Eerie
Moderator
Moderator
Posts: 620
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

HOWTO: recording desktop video with ffmpeg

Post#1 by M. Eerie » 07 Jun 2023, 11:24

It appears that Peek is no longer maintained. So there's need for a replacement.

For those who don't know it, Peek is a tool that allows you to generate a .gif capturing the desktop. Very convenient when it comes to show the (in)correct operation of something (and therefore to help or ask for help).

Fortunately, we have ffmpeg. This is what I'm using by now:

Code: Select all

ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0 input.mp4   (grabs the full 1920x1080 screen)
ffmpeg -video_size 1280x800 -framerate 25 -f x11grab -i :0.0+50,30 input.mp4   (grabs a 1280x800 res screen starting at coordinates 50,30 from the left-top)
Then, you convert to (720 height) .gif:

Code: Select all

ffmpeg -i input.mp4 -vf "fps=10,scale=-1:720:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
or if you want full resolution, in which case, you will have to upload to another service, because the output file will be too large to be hosted at a free pic hosting server:

Code: Select all

ffmpeg -i input.mp4 -vf "fps=10,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
As the generated .gif will be too large, you can reduce its size (and quality) with:

Code: Select all

ffmpeg -i input.mp4 -vf "fps=10,scale=-1:720:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=128[p];[s1][p]paletteuse=dither=bayer" -loop 0 output.gif
or use imagemagick:

Code: Select all

convert output.gif -loop 0 -delay 10 -fuzz 20% -layers optimize output-optimized.gif
Play with the fuzz value to compare results. Lower value means bigger size and better quality.

Then you can upload to your free image hosting service of choice. For example IBB
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

HOWTO: recording desktop video with ffmpeg

Post#2 by Rava » 15 Dec 2023, 04:32

M. Eerie wrote:
07 Jun 2023, 11:24

Code: Select all

ffmpeg -video_size 1280x800 -framerate 25 -f x11grab -i :0.0+50,30 input.mp4
This command, and the full screen capture as well, will record everything on the desktop and run forever.

You will have to stop it by using Ctrl+C.

The downside: it will also record the terminal you started the above command with. And this is probably not what you want to record to begin with.

You can avoid that by using a sleep command at the beginning, maybe with a command that plays a sound between the sleep and the ffmpeg recording (to signal you when the recording starts). And start all these on another desktop, and during the sleep switch to a different desktop. But you will still have to switch back to the desktop you started the commands to stop the recording via Ctrl+C. And then you have the witching back to the other desktop also recorded, and again this is something you do not want.

Example: Sleep 5 seconds.
the "beep" command is a valid command that plays a sound. (you have to either code this command to suit your local needs yourself, or use some mpg123 or mpv command playing some valid multimedia sound file. It should only be a sound file not containing any video)
and the ffmpeg command:

Code: Select all

sleep 5;beep;ffmpeg -video_size 1280x800 -framerate 25 -f x11grab -i :0.0+50,30 input.mp4
M. Eerie, do you know a way to tell ffmpeg to only record a certain time? Or to only record a certain amount of frames?
Because even when you use sleep like above, you still would have to switch back to the desktop you started my above example, and that will create video parts you not want in your resulting video.

The only solution would be, starting the ffmpeg command and also starting a command that waits a certain time and then sends the ffmpeg command the signal equivalent of Ctrl+C to interrupt the recording, but I am not sure how that can be implemented. (It could be done when you use a script that uses the ffmpeg command as above, but puts that into the background by adding " &" (minus the quotes) at the end, then wait for the wanted seconds and send the signal to the ffmpeg command, but I am not sure if the ffmpeg screen recording command would work as intended when put it into the background?)

The only way using ffmpeg to record the screen and not also having the terminal that starts the ffmpeg command in it would be:

Use the upper left corner of the screen for the things you want to record.
Move the terminal that starts the cropped ffmpeg screen capture record to the lower right corner and (when needed) move it partially outside your visible screen.
Make sure the command is typed correctly when the terminal is fully visible but to not yet press enter.
Then move the terminal window outside the recorded area into the lower right corner and when needed even partially outside your visible screen.
Start the recording by pressing enter. (You can add the pause and sound playing command as well so that you can move the mouse to an area inside the recorded area prior the start of the recording)
During the recording do what you want to be recorded. When you want the recording to end, move the mouse cursor to the terminal, bring it into focus and press Ctrl+C.

Since the terminal sits outside your recorded area, this part will not be recorded. (You will have some unnecessary second(s) at the end of your resulting video file where seemingly nothing happens, though)

donald
Full of knowledge
Full of knowledge
Posts: 2064
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

HOWTO: recording desktop video with ffmpeg

Post#3 by donald » 15 Dec 2023, 05:30

Rava wrote:
15 Dec 2023, 04:32
...do you know a way to tell ffmpeg to only record a certain time?
-t <some value>

Example:

Code: Select all

ffmpeg -f x11grab -r 25 -s 1360x768 -i :0.0 -vcodec libx264 -preset ultrafast -t 00:00:08 output.mkv
...this will record for 8 seconds.

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

HOWTO: recording desktop video with ffmpeg

Post#4 by Ed_P » 15 Dec 2023, 06:17

donald wrote:
15 Dec 2023, 05:30
-t <some value>
And to be clear:

Code: Select all

-t 12:34:56
which is

12 hours, 34 minutes and 56 seconds.
Ed

User avatar
M. Eerie
Moderator
Moderator
Posts: 620
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

HOWTO: recording desktop video with ffmpeg

Post#5 by M. Eerie » 15 Dec 2023, 10:44

Rava wrote:
15 Dec 2023, 04:32
M. Eerie, do you know a way to tell ffmpeg to only record a certain time?
Once you've finished recording, you can take the input.mp4 (following the given example at first post) video and trim the relevant part:

Code: Select all

ffmpeg -ss [initial_time] -i input.mp4 -t [duration] -c copy trimmed-output.mp4
Where initial_time and duration, can be expressed in seconds or in a typical time format (even with milliseconds)

Note that the -t parameter can be omitted if you only want to keep the rest of the videoclip from the seek starting point (-ss)

See here

You can then proceed with the .gif conversion
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

HOWTO: recording desktop video with ffmpeg

Post#6 by Rava » 15 Dec 2023, 18:28

Thanks donald Ed_P and M. Eerie.
donald wrote:
15 Dec 2023, 05:30
ffmpeg -f x11grab -r 25 -s 1360x768 -i :0.0 -vcodec libx264 -preset ultrafast -t 00:00:08 output.mkv
this, combined with a pause at the beginning and end, like so

Code: Select all

sleep 3;beep;ffmpeg -f x11grab -r 25 -s 1360x768 -i :0.0 -vcodec libx264 -preset ultrafast -t 00:00:08 output.mkv;beep
again when the command "beep" is a valid command playing some short notice sound works well.
One must just move the terminal way out to the right side so that it will not be part of the recording.
Cheers!
Yours Rava

Post Reply