wiki:

Capture

/

Desktop


Version 14 (modified by fratti, 4 years ago) (diff)

Added notes about lossless recording

Linux

Use the x11grab device:

ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 output.flv

This will grab the image from desktop, starting with the upper-left corner at (x=100, y=200) with the width and height of 1024x768.

If you need audio too, you can use alsa like this:

ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 -f alsa -ac 2 -i pulse output.flv

OS X

Use the avfoundation device:

ffmpeg -f avfoundation -list_devices true -i ""

This will enumerate all the available input devices including screens ready to be captured.

(Did not work for me; only webcam gets enumerated, desktop/audio devices do not. Any other prerequisites? -Vultaire)

Once you've figured out the device index corresponding to the screen to be captured use:

ffmpeg -f avfoundation -i "<screen device index>:<audio device index>" out.avi

This will capture the screen from <screen device index> and audio from <audio device index> into the output file out.avi.

Windows

Use a dshow device:

ffmpeg -f dshow -i video="screen-capture-recorder" output.flv

This will grab the image from entire desktop. Here is a list of alternative devices to use as well:

If you need audio too, you can use this:

ffmpeg -f dshow -i video="UScreenCapture":audio="Microphone" output.flv

If you want to capture the audio that is playing from your speakers, you may also need to configure so-called "Stereo Mix" device.

or

ffmpeg -f dshow -i video="UScreenCapture" -f dshow -i audio="Microphone" output.flv

You can list your devices with:

ffmpeg -list_devices true -f dshow -i dummy

There is also a "gdigrab" you can use to grab from the screen in windows (video)

Example recipe: live screen capture to webm

Here's a recipe used by one user for doing a live full-screen 1920x1080 screen capture to WebM format (VP8/vorbis). Note that you may need to tweak the -cpu-used or -b:v parameters depending on how fast your system is.

SET "audio_device=<audio_device>"
SET "video_device=<video_device>"
SET "threads=<physical cores minus 1>"
ffmpeg -f dshow -i audio="%audio_device%":video="%video_device%" -c:v libvpx -b:v 2M -deadline realtime -cpu-used -5 -threads %threads% -c:a libvorbis -y output.webm

It seems that the audio and video might not be perfectly in sync, but this should be a good starting point and may be "good enough" for some.

References:

General note

If you have a slow computer, it will not be smart to grab and encode your video at the same time, because slow CPU will not be able to do this. In that case, first grab all you need and save it as uncompressed video/audio and when you finish the grabbing process then start converting it to whatever you need:

Linux:

ffmpeg -framerate 25 -video_size 1024x768 -f x11grab -i :0.0+100,200 -f alsa -ac 2 -i pulse -vcodec libx264 -crf 0 -preset ultrafast -acodec pcm_s16le output.flv
ffmpeg -i output.flv -acodec ... -vcodec ... final.flv

Windows:

ffmpeg -f dshow -i video="screen-capture-recorder":audio="Microphone" -vcodec libx264 -crf 0 -preset ultrafast -acodec pcm_s16le output.flv
ffmpeg -i output.flv -acodec ... -vcodec ... final.flv

Lossless recording

If you want a perfect recording of your desktop, x264 can help. Use lossless encoding, e.g.:

ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 -c:v libx264 -qp 0 -preset ultrafast -profile:v high444 capture.mkv

The high444 profile is used to ensure a lossless RGB to YUV transformation. Otherwise, the capture will not be lossless, and text will look blurry. -qp 0 tells x264 to encode in lossless mode, -preset ultrafast advises it to do so fast.

The encoder should be fast enough on most modern hardware to record without any framedrop, and even leave enough CPU headroom for other applications.

If most of your screen will be static, it may be useful to use the -tune stillimage parameter, i.e.:

ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 -c:v libx264 -qp 0 -tune stillimage -preset ultrafast -profile:v high444 capture.mkv

If you're going to archive the recording or are concerned about file size, re-encode it losslessly again but with a slower preset. Note that since the initial recording was lossless, and the re-encode is lossless too, no quality loss is introduced in this process in any way.

ffmpeg -i capture.mkv -c:v libx264 -qp 0 -tune stillimage -preset veryslow -profile:v high444 capture_smaller.mkv