Capture Audio and Video on macOS Using FFmpeg AVFoundation
This article provides a step-by-step guide on how to simultaneously capture video and audio on macOS using FFmpeg’s AVFoundation framework. You will learn how to locate your connected hardware input devices, map the audio and video sources correctly, and run a single command to record them into a synchronized media file.
Step 1: List Available Input Devices
Before capturing, you need to identify the index numbers of your video and audio input devices. Open your Terminal and run the following command to query AVFoundation:
ffmpeg -f avfoundation -list_devices true -i ""This command will output a list of available AVFoundation video and audio devices. The output will look similar to this:
[AVFoundation indev @ 0x7f9a12345678] AVFoundation video devices:
[AVFoundation indev @ 0x7f9a12345678] [0] FaceTime HD Camera
[AVFoundation indev @ 0x7f9a12345678] [1] Capture screen 0
[AVFoundation indev @ 0x7f9a12345678] AVFoundation audio devices:
[AVFoundation indev @ 0x7f9a12345678] [0] MacBook Pro Microphone
Note the index numbers in the square brackets (e.g., [0]
or [1]) for both the video and audio devices you want to
use.
Step 2: Run the Capture Command
To capture video and audio simultaneously, use the index numbers you
found in the previous step. The input syntax for AVFoundation uses the
format "[video_index]:[audio_index]".
For example, to capture video from the “FaceTime HD Camera” (index
0) and audio from the “MacBook Pro Microphone” (index
0), use the following command:
ffmpeg -f avfoundation -framerate 30 -i "0:0" -c:v libx264 -pix_fmt yuv420p -c:a aac output.mp4Explaining the Command Flags:
-f avfoundation: Specifies the input format as macOS AVFoundation.-framerate 30: Sets the capture frame rate to 30 frames per second.-i "0:0": Specifies the input device indices. The value before the colon (0) is the video source, and the value after the colon (0) is the audio source.-c:v libx264: Encodes the video stream using the H.264 codec.-pix_fmt yuv420p: Sets the pixel format to YUV 4:2:0, ensuring the output video is compatible with default macOS applications like QuickTime.-c:a aac: Encodes the audio stream using the AAC codec.output.mp4: The name of the resulting video file.
Step 3: Stop the Recording
Once the command is running, FFmpeg will continuously record the
input. To stop the recording safely and finalize the output file, press
q or
Ctrl + C in your Terminal window.