Skip to content

Instantly share code, notes, and snippets.

@ThomazPom
Created September 28, 2024 13:22
Show Gist options
  • Save ThomazPom/0244b4a90b6b0561fd0fe9d379357443 to your computer and use it in GitHub Desktop.
Save ThomazPom/0244b4a90b6b0561fd0fe9d379357443 to your computer and use it in GitHub Desktop.

USB Video Capture with FFmpeg

This guide explains how to capture video and audio from a USB capture device and process it using FFmpeg in two steps. The current method captures video at a custom frame rate with sound, followed by a second step to process the video.

Step 1: Capture Video and Audio

The first command captures both video and audio using a USB video capture device and a virtual audio source. It records the video at 34 frames per second for 200 seconds.

ffmpeg -f dshow -rtbufsize 100M -i video="usb video" -f dshow -i audio="virtual-audio-capturer" -preset ultrafast -framerate 34 -t 200 -y output2.mp4
  • -f dshow: Uses DirectShow as the input format for capturing video and audio.
  • -rtbufsize 100M: Sets the buffer size for real-time capture.
  • -i video="usb video": Specifies the USB video capture device.
  • -i audio="virtual-audio-capturer": Specifies the virtual audio capture source.
  • -preset ultrafast: Uses the fastest compression preset.
  • -framerate 34: Sets the frame rate to 34 fps.
  • -t 200: Limits the capture duration to 200 seconds.
  • -y output2.mp4: Overwrites the output file output2.mp4 if it already exists.

Step 2: Process the Captured Video

The second command takes the captured video file output2.mp4 and processes it to ensure compatibility by converting it to the H.264 codec with AAC audio.

ffmpeg -y -i ".\output2.mp4" -pix_fmt yuv420p -vcodec h264 -t 60 -acodec aac -ac 2 ".\final_video.mp4"
  • -pix_fmt yuv420p: Ensures compatibility by using the YUV420p pixel format.
  • -vcodec h264: Converts the video to H.264 format.
  • -t 60: Limits the output video to 60 seconds.
  • -acodec aac: Encodes the audio in AAC format.
  • -ac 2: Uses 2-channel stereo audio.
  • -y final_video.mp4: Overwrites the output file final_video.mp4 if it already exists.

Proposed Single Command (Not Tested)

To merge both commands into one, you can try the following approach, although it has not been tested:

ffmpeg -f dshow -rtbufsize 100M -i video="usb video" -f dshow -i audio="virtual-audio-capturer" -preset ultrafast -framerate 34 -t 60 -pix_fmt yuv420p -vcodec h264 -acodec aac -ac 2 -y final_video.mp4

This single command attempts to capture and process the video in one step. Be sure to test it to confirm its effectiveness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment