Wednesday, June 1, 2016

Simple Screen Capture in Windows using FFmpeg

 One day, I want to record my FL Studio activity. I can't use FRAPS because I use Windows 8 where capturing Aero Desktop option in FRAPS is useless. I also tried CamStudio but it lags alot. I remembered that I have FFmpeg installed and use that for screen recording instead.

FFmpeg is a command-line application that focus mostly on audio and video related. Although it can be used to re-encode video to another format and it's bit complex, using it for screen recording is actually simple.

Before proceed, here are things you need
  • FFmpeg. Can be downloaded here
  • Stereo Mix audio input enabled in the audio devices (optional, only needed if you want to capture the audio also)
Now, extract the FFmpeg somewhere and double-click "ff-prompt.bat". It will add "ffmpeg" to the command prompt for the current CMD window. Now we can start recording.

To record specific window, the input will be title="<the window name>". To record the entire desktop, the input will be desktop.
Now to start recording, just type ffmpeg -f gdigrab -i <the input> capture.mkv and to stop recording, just press Ctrl+C in the CMD Window. The captured screen will be stored in "capture.mkv" file.

If you have stereo mix audio input, you can also record the audio. First we need to get the exact name of the stereo mix input name. Type ffmpeg -list_devices true -f dshow -i dummy 2>&1 | findstr /I "stereo mix". If there's no output, that means you don't have stereo mix. In my laptop, it output something like this

D:\pvid>ffmpeg -list_devices true -f dshow -i dummy 2>&1 | findstr /I "stereo mix"
[dshow @ 0000000001cbd040]  "Stereo Mix (Realtek High Definition Audio)"


"Stereo Mix (Realtek High Definition Audio)" is example of the actual stereo mix input name, at least in my laptop.

Now to record the audio+video, type ffmpeg -f dshow -i audio="<stereo mix name>" -f gdigrab -i <the input> capture.mkv

If you feel that your PC lags alot when capturing the screen or want to capture without losing any quality, you may want to capture the video (and audio if you capture it too) in uncompressed format first, then re-encode it later. FFmpeg also can be used to re-encode the video if you know how.

To record the uncompressed video only, type ffmpeg -f gdigrab -i <the input> -c:v libx264 -qp 0 -preset ultrafast capture.mkv. The size of the video might be large, but not as large as fraps.
For the audio+video, type ffmpeg -f dshow -i audio="<stereo mix name" -c:a pcm_s6le -f gdigrab -i <the input> -c:v libx264 -qp 0 -preset ultrafast capture.mkv

If possible, try to capture specific window only instead of the whole desktop. Capturing the whole desktop only gives framerate of 30FPS, while capturing specific window gives framerate 60FPS. It doesn't matter if the window size is small or big when capturing specific window, it will gives 60 FPS instead of the whole desktop which gives 30 FPS.

Saturday, January 16, 2016