C.W.K.
Stream
Lesson 04 of 08 · published

Screen Recording

~10 min · screen-record, avfoundation, x11grab

Level 0Viewer
0 XP0/73 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Per-platform devices

FFmpeg can capture the screen via OS-specific input devices:

  • macOSavfoundation. List devices with ffmpeg -f avfoundation -list_devices true -i "". Index 1 is typically the main display; index 2 a second display.
  • Linuxx11grab for X11 (:0.0). Wayland uses kmsgrab + intermediate layers.
  • Windowsgdigrab or ddagrab (DXGI Desktop Duplication; faster).

The macOS pattern

On macOS, system audio capture requires a virtual audio device (BlackHole, Loopback). FFmpeg can capture mic audio directly — pair the screen device with the mic device in one ffmpeg invocation.

Code

macOS screen capture·bash
# List devices first
ffmpeg -f avfoundation -list_devices true -i ""

# Capture display 1 + microphone 0 → MP4
ffmpeg \
  -f avfoundation -framerate 30 -capture_cursor 1 -i "1:0" \
  -c:v h264_videotoolbox -b:v 8M \
  -pix_fmt yuv420p \
  -c:a aac -b:a 192k -ar 48000 \
  recording.mp4

# Press 'q' to stop cleanly. Don't Ctrl+C — that may leave the file
# without a moov atom (unplayable).

# Window-only capture (macOS 14+)
ffmpeg \
  -f avfoundation -framerate 30 -capture_cursor 1 -i "3:0" \
  -c:v h264_videotoolbox -b:v 4M \
  -pix_fmt yuv420p \
  -c:a aac -b:a 192k \
  window.mp4
Linux X11 capture·bash
# Full screen 1920x1080 at 30fps with PulseAudio default source
ffmpeg \
  -f x11grab -framerate 30 -video_size 1920x1080 -i :0.0 \
  -f pulse -i default \
  -c:v libx264 -preset veryfast -crf 22 \
  -pix_fmt yuv420p \
  -c:a aac -b:a 192k \
  recording.mp4

# Region capture: -i :0.0+100,150 starts at offset (100, 150)

External links

Exercise

Record a 30-second screen capture with mic audio. Verify the output plays cleanly. Then attempt to capture system audio (install BlackHole if needed). Trim the recording with -c copy to remove the first 3 seconds (always cluttered with the 'starting' indicator).

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.