Capture Windows screen with ffmpeg

WindowsFfmpegScreen Capture

Windows Problem Overview


The ffmpeg is cross-platform and very powerful software to handle video/audio or to stream it. On Linux ffmpeg can capture X11 screen with a command below:

ffmpeg -f x11grab -r 25 -s cif -i :0.0 out.mpeg

But is it possible to grab Windows Desktop with ffmpeg?

Windows Solutions


Solution 1 - Windows

Use the built-in GDI screengrabber (no install needed) like this :

ffmpeg -f gdigrab -framerate 10 -i desktop [output]

This will capture ALL your displays as one big contiguous display.

If you want to limit to a region, and show the area being grabbed:

ffmpeg -f gdigrab -framerate ntsc -offset_x 10 -offset_y 20 -video_size 640x480 \
-show_region 1 -i desktop [output]

To grab the contents of the window named "Calculator":

ffmpeg -f gdigrab -framerate 25 -i title=Calculator [output]

I found that framerate 10 suits screen capture well (you can change it).

I have encoded to both files and streaming outputs and it works quite well.

Solution 2 - Windows

This will help for capturing the working screen on windows :

> ffmpeg -y -rtbufsize 100M -f gdigrab -t 00:00:30 -framerate 30 -probesize 10M -draw_mouse 1 -i desktop -c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p c:/video_comapre2.mp4

Solution 3 - Windows

*this code is tried successfully on windows XP Sp3 and ffmpeg (last version 28/12/2012 for windows) -ffmpeg.exe must be copied in c:\windows\system32 directory for being accessed from anywhere from your PC) ScreenCapture can be downloaded for free (google). Install it (msi file). Its registered automatically from the (ScreenCapture.ax file). It's sent with an IscrenCapture.h file also.

-capture screen video and audio (adjust the device audio you have-mine is RealTek AC97 Audio) I cannot install params of UscreenCapture in registry (tried even on IscrenCapture.h file ).It always provide the full scren capture only. There is an error in registry location,in parameters types (dwords are specified by the author but reg_binary is installed)... then i used the crop() ffmpeg function to capture any region on the screen.the command line is :

xwidth, xheight are the width & height of the region want to capture. xleft, xtop the coord of the top left point of the rectangle capture.

ffmpeg -f dshow -i video="UScreenCapture":audio="Realtek AC97 Audio" -vf crop=xwidth:xheight:xleft:xtop c:\output.flv

to capture video only can use

ffmpeg -f dshow -i video="UScreenCapture" -vf  crop=xwidth:xheight:xleft:xtop c:\output.flv

NB: x11grab dont work on windows (it's specifically for Linux/X11) can use wscript.shell to code the line command silently. i used the format video output as flv because i have the best rendering and small capacity.I dont success with mp4.

you can know your media devices with ffmpeg:

ffmpeg -list_devices true -f dshow -i dummy

-you can record any sound from your pc with this command line (adapt the device you have):

ffmpeg -f dshow -i audio="Realtek AC97 Audio" -acodec libmp3lame "c:\out.mp3"

Solution 4 - Windows

ffmpeg windows static version 4.2.2, screen recording with audio

to check your microphone

ffmpeg -list_devices true -f dshow -i dummy

next copy your audio="YOUR MICROPHONE OR STEREO MIX", mine is "Microphone (Realtek High Definition Audio)".

ffmpeg -rtbufsize 1500M -f dshow -i audio="Microphone (Realtek High Definition Audio)" -f -y -rtbufsize 100M -f gdigrab -t 00:00:30 -framerate 30 -probesize 10M -draw_mouse 1 -i desktop -c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p "d:\ffmpeg_testing.mp4"

Solution 5 - Windows

A patch to implement this was sent to the ffmpeg mailing list a while back. This would have implemented gdigrab to work just like x11grab.

Also, the ffmpeg codec page says "VfW" (Video for Windows? aka DirectShow?) capture is supported.

Solution 6 - Windows

http://nerdlogger.com/2011/11/03/stream-your-windows-desktop-using-ffmpeg/

explains how to do it.

basically, install uscreencapture dshow filter, then ffmpeg -f dshow -i video="UScreenCapture" out.mp4

Solution 7 - Windows

This can be done without using x11grab/xcbgrab/gdigrab with help of the below commands in linux .

To record a video,

 ffmpeg -f x11grab  -s 1366x768 -i :0.0 -r 25 -vcodec libx264  output.mkv

To record a frame,

./ffmpeg -f fbdev -framerate 1 -i /dev/fb1 -frames:v 1 screenshot3.jpeg

Solution 8 - Windows

I would like to add the command I use to capture the screen:

ffmpeg.exe -y ^
-vsync vfr ^
-f gdigrab ^
-indexmem 300M ^
-rtbufsize 1G ^
-probesize 7M ^
-max_probe_packets 50k ^
-draw_mouse 0 ^
-video_size 1280x720 ^
-offset_y 152 ^
-framerate 24 ^
-c:v bmp ^
-strict strict ^
-thread_queue_size 50k ^
-r 24 ^
-i desktop ^
-f dshow ^
-channel_layout stereo ^
-thread_queue_size 50k ^
-strict strict ^
-i "audio=Stereo-mix (Realtek High Definition Audio)" ^
-map 0:v ^
-max_muxing_queue_size 50k ^
-f mp4 ^
-movflags +faststart ^
-max_interleave_delta 0 ^
-c:v libx264 ^
-r 24 ^
-preset fast ^
-tune film ^
-strict strict ^
-crf 25 ^
-pix_fmt yuv422p ^
-map 1:a ^
-max_muxing_queue_size 50k ^
-max_interleave_delta 0 ^
-c:a aac ^
-strict strict ^
-ac 2 ^
screencapture.mp4

The value of probesize appears to have much influence on the audio/video synchronisation. Raising the value by a few megabytes may cause A/V out of sync. Change by 1M or 500k, e.g. to 7500k, at a time. If you leave out the probesize option, ffmpeg will, by default, set the probesize to 5M.

max_interleave_delta option prevents ffmpeg forcing output. If ffmpeg forces output, a message will appear in your log, e.g.: [mp4 @ 00000199f7512040] Delay between the first packet and last packet in the muxing queue is 10007271 > 10000000: forcing output

If ffmpeg takes to much processor time, try changing the -preset option (ultrafast, superfast, veryfast, faster, fast, medium (default), slow, slower, veryslow), larger -crf (constant rate factor), e.g. 32 or lower framerate (-framerate and -r options on several places in the command).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkamaeView Question on Stackoverflow
Solution 1 - WindowsfrustratedView Answer on Stackoverflow
Solution 2 - WindowsM. D. PView Answer on Stackoverflow
Solution 3 - WindowsYbenamView Answer on Stackoverflow
Solution 4 - WindowsSourcephyView Answer on Stackoverflow
Solution 5 - WindowsAndreas KlöcknerView Answer on Stackoverflow
Solution 6 - WindowssomejerkView Answer on Stackoverflow
Solution 7 - WindowsAtita HalemaniView Answer on Stackoverflow
Solution 8 - WindowsVictorView Answer on Stackoverflow