Click here to Skip to main content
15,867,141 members
Articles / Operating Systems / Windows
Tip/Trick

Video Screen Capture with Audio

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Jan 2021CPOL3 min read 15K   486   12   8
Most flexible and comprehensive yet very simple FFMpeg-based command captures all screen activity in a video file, optionally with audio
FFMpeg does it all. However, the combination of video and audio capture raises some delicate problems. The present article explains all the options and considerations for Windows

Contents

Motivation
Insights
Video
Audio
Synchronization between Audio and Video
Batch File
License

Motivation

Capturing video from a camera is commonplace. I wanted to capture video from the computer screen, for example, to produce a video lecture. I’ve noticed several CodeProject articles describing different means of capturing everything which happens on the computer screen. I did not found what I wanted. Some solutions are bound to some obsolete video containers or obsolete components, others are over-complicated or don’t work properly.

At the same time, the solution is way simpler and is readily available: compact, open-source, and built for most platforms FFMpeg does it all.

Insights

Basically, everything is done using FFMpeg with this command line:

ffmpeg
    -f dshow -i audio="Stereo Mix (Realtek Audio)" ^
    -f gdigrab -itsoffset 00:00:0.6 -i desktop -c:v libx264rgb ^
        -framerate 24 -crf 20 -preset ultrafast ^
    output-file.mp4

Two f “enforce format” options define the methods of capturing video. Unfortunately, these methods and corresponding -i “input” options are platform-dependent. So far, I’ve sorted out the usage only for Windows, but later may need to do the same with Linux.

The first line after ffmpeg is optional and defines audio capture, two following lines — video capture. The audio capture depends on the device to be used. Sometimes, this is the audio produced by computer software, and this is the case shown in the command line above. In other cases, we need the capture from a microphone.

Video

First of all, why using H264 (AVC) standard? The answer is: for the same reason as ultrafast: performance. Usually, we don’t need the best compression, we need the algorithm which gives us good quality but not overwhelms the CPU and other resources. If encoding is slow, it won’t encode frames in time, and it can even delay the computer operation. In this situation, AVC is a very good choice. If you have a better idea, you can always replace the codec (-c:v) parameter and codec options (next line) with something else.

Naturally, for final production, the resulting video will require transcoding using some more advanced codec with good quality and much better compression. At the time of writing, I strongly recommend the most advanced AOMedia AV1, but it can be something else. Please see FFMpeg documentation.

Video capture parameters:

Constant Rate Factor crf: valid values: 0-51, 0 is lossless, 23: default, 51: worse possible quality.

Frame rate framerate; common frame rates: 24, 25, 30, 48, 50, and 60

Presets: ultrafast, superfast, veryfast, faster, fast, medium (default), slow, veryslow; for the specific purpose of capture, we need the operation to be fast enough, caring of the compression factor much less. Perfectly compressed video with the best quality can be achieved after transcoding for production.

Audio

Audio capture is more sophisticated, as everything depends on system settings and the desired effect of capturing. On my very typical system, I have two recording devices “Microphone (Realtek Audio)” and “Stereo Mix (Realtek Audio)”. To obtain the list of available and enabled devices, use

ffmpeg -list_devices true -f dshow -i dummy

The use of a microphone is obvious, and “Stereo Mix” can be used to capture sounds produced by software.

The required device should be enabled in system settings: legacy control panel => Sound => (“Change sound card settings” => Recording) or (“Manage Audio Devices”).

Synchronization between Audio and Video

It was the first problem I noticed: the delay between video and audio. The parameter itsoffset solves this problem. In the command line sample shown above, it fixes the delay of audio relative to video by delaying video.

If this parameter is placed in the audio line, it will delay audio relative to video.

Batch File

@echo off
set tool=c:/app/Media/ffmpeg/bin/ffmpeg.exe

set videodelay=00:00:0.6
set framerate=24
set quality=20
set microphone="Microphone (Realtek Audio)"
set mix="Stereo Mix (Realtek Audio)"
set audioChoice=%mix%

set outputFile=%1
:request-file
if "%outputFile%"=="" set /p outputFile=Output file name: 
if "%outputFile%"=="" goto:request-file

%tool% ^
    -f dshow -i audio=%audioChoice% ^
    -f gdigrab -itsoffset %videodelay% -i desktop -c:v libx264rgb ^
        -framerate %framerate% -crf %quality% -preset ultrafast ^
    %outputFile%.mp4

The file name is requested by the script or entered as a command-line parameter, other parameters are rarely changed, can be adjusted manually.

The user will need to edit the path to “ffmpeg.exe” or setup the PATH environment variable accordingly.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Physics, physical and quantum optics, mathematics, computer science, control systems for manufacturing, diagnostics, testing, and research, theory of music, musical instruments… Contact me: https://www.SAKryukov.org

Comments and Discussions

 
GeneralThis is a helpful article Pin
Bryan Wagan27-May-21 18:02
Bryan Wagan27-May-21 18:02 
AnswerRe: This is a helpful article Pin
Sergey Alexandrovich Kryukov3-Jun-21 13:56
mvaSergey Alexandrovich Kryukov3-Jun-21 13:56 
Questionsounds promising ... Pin
Christ Kennedy29-Jan-21 9:13
mvaChrist Kennedy29-Jan-21 9:13 
this sounds like a great solution
but your article is a little sparse on ~user-friendly~ advise to us who are command-line-impaired.

without going into length about the intricacies of FFMPeg
could you explain, in a 'Video Screen Capture with Audio : FOR DUMMIES' sort of way,
how to use this command-line solution.

I've downloaded FFMPeg.exe and have changed the path in the script to locate it
Dummy-Users, like myself, could use a little help. Maybe a Windows style start/stop button that could do the interface for them.
How would I stop the video recording if I ever figured out how to start it?
I'm not going to rate this article because it promises a great solution but provides little help for those of us who haven't the slightest clue.

thanks for your input but
no matter how great your solution is, if the monkey-in-the-chair can't figure out how to use it it may as well be broken.
my code is perfect until i don't find a bug...

AnswerRe: sounds promising ... Pin
Sergey Alexandrovich Kryukov30-Jan-21 9:00
mvaSergey Alexandrovich Kryukov30-Jan-21 9:00 
QuestionAnother 5 Pin
AndyChisholm6-Jan-21 6:46
AndyChisholm6-Jan-21 6:46 
AnswerRe: Another 5 Pin
Sergey Alexandrovich Kryukov6-Jan-21 7:14
mvaSergey Alexandrovich Kryukov6-Jan-21 7:14 
GeneralMy vote of 5 Pin
S Houghtelin5-Jan-21 7:26
professionalS Houghtelin5-Jan-21 7:26 
AnswerRe: My vote of 5 Pin
Sergey Alexandrovich Kryukov5-Jan-21 8:40
mvaSergey Alexandrovich Kryukov5-Jan-21 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.