Post

Record Video in Linux with avconv and ffmpeg

Record Video in Linux with avconv and ffmpeg

Screen Recording in Linux

This guide covers how to record your screen in Linux using both avconv (the ffmpeg replacement in Ubuntu) and ffmpeg, along with how to prepare videos for sharing on platforms like YouTube.

Recording with avconv

avconv is the replacement for ffmpeg in Ubuntu and some other distributions.

Basic Screen Recording with Audio

1
2
# Record screen with audio at 25fps in 1080p
avconv -f alsa -i pulse -f x11grab -r 25 -s 1920x1080 -i :0.0+0,0 -vcodec libx264 -pre:0 lossless_ultrafast -threads 0 video.mkv

Parameter Explanation:

  • -f alsa -i pulse: Capture audio from PulseAudio
  • -f x11grab: Capture from X11 display
  • -r 25: Set frame rate to 25fps
  • -s 1920x1080: Set resolution to 1920x1080 (Full HD)
  • -i :0.0+0,0: Capture from display 0.0, starting at coordinates 0,0
  • -vcodec libx264: Use H.264 video codec
  • -pre:0 lossless_ultrafast: Use ultrafast preset for minimal CPU usage
  • -threads 0: Use all available CPU threads
  • video.mkv: Output filename

Compressing for YouTube

After recording, compress the video for uploading to YouTube:

1
2
# Convert raw recording to compressed MP4
avconv -i video.mkv -c:v libx264 outputfile.mp4

Recording with ffmpeg

If you’re using a distribution with ffmpeg instead of avconv, use these commands.

Basic Screen Recording

1
2
# Record screen at 15fps in 1280x800
ffmpeg -f x11grab -r 15 -s 1280x800 -i :0.0 -vcodec libx264 -vpre lossless_ultrafast -threads 4 -y -sameq out.mkv

Parameter Explanation:

  • -f x11grab: Capture from X11 display
  • -r 15: Set frame rate to 15fps
  • -s 1280x800: Set resolution to 1280x800
  • -i :0.0: Capture from display 0.0
  • -vcodec libx264: Use H.264 video codec
  • -vpre lossless_ultrafast: Use ultrafast preset
  • -threads 4: Use 4 CPU threads
  • -y: Overwrite output file if it exists
  • -sameq: Maintain quality level
  • out.mkv: Output filename

Compressing for YouTube

Compress the recorded video for YouTube:

1
2
# Convert raw recording to compressed MP4 with high quality
ffmpeg -i video.mkv -vcodec libx264 -vpre hq -crf 22 -threads 0 video.mp4

Parameter Explanation:

  • -vcodec libx264: Use H.264 video codec
  • -vpre hq: Use high quality preset
  • -crf 22: Constant Rate Factor (lower values = higher quality, 18-28 is typical)
  • -threads 0: Use all available CPU threads

Additional Options

Recording a Specific Window

To record a specific window instead of the entire screen:

1
2
3
4
5
# Get window ID
xwininfo

# Record specific window
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0+100,200 -vcodec libx264 window_recording.mkv

Recording with Different Audio Source

1
2
3
4
5
# List available audio devices
arecord -L

# Record with specific audio device
ffmpeg -f alsa -i hw:0,0 -f x11grab -r 25 -s 1920x1080 -i :0.0 -vcodec libx264 video_with_audio.mkv
This post is licensed under CC BY 4.0 by the author.