FFmpeg facilitates USB camera and screen recording through command-line operations. This approach utilzies C#'s Process class to execute FFmpeg commands and captures console output for comprehensive recording functionality.
1. Listing Available Devices
Execute:
ffmpeg.exe -list_devices true -f dshow -i dummy
Device information appears in the error stream. Redirect standard error and parse output to identify video and audio devices. Example output:
[dshow @ 0000018b1a24c740] "USB2.0 HD UVC WebCam" (video)
[dshow @ 0000018b1a24c740] "Microphone Array (Realtek Audio)" (audio)
Set UTF-8 encoding to prevent character corruption.
2. Retrieving Video Device Resolutions
Run:
ffmpeg.exe -list_options true -f dshow -i video="USB Camera"
Resolution details appear in the error stream. Extract supported resolutions and frame rates from output segments like:
min s=1280x720 fps=30 max s=1280x720 fps=30
3. Obtaining Video Encoders
Command:
ffmpeg.exe -encoders
Encoder data outputs to satndard output. Parse entries prefixed with 'V' for video encoders. Example:
V....D libx264 H.264 encoder
V....D libx265 HEVC encoder
4. Recording Procedures
Maintain the Process instance during recording and terminate it to stop.
4.1 USB Camera Recording
ffmpeg.exe -f dshow -i video="Camera Name":audio="Microphone" -c:v libx264 -s 1280x720 -r 15 output.mp4
Parameters: -c:v specifies encoder, -s sets resolution, -r defines frame rate.
4.2 Screen Capture
ffmpeg.exe -f dshow -i audio="Microphone" -f gdigrab -i desktop -c:v libx264 -r 15 output.mp4
Use -f gdigrab for screen capture. Add -offset_x, -offset_y, and -video_size to record specific regions.
4.3 Stopping Recording Send 'q' to the process input stream and await termination:
recordingProcess.StandardInput.WriteLine("q");
recordingProcess.WaitForExit();
recordingProcess.Close();