Screen Recording with ffmpeg

Publish date: Jan 26, 2019
Tags: ffmpeg, shell-script

Table of Contents

  1. Basic screen recording
  2. Screen/mic
  3. Screen/cam/mic
  4. Screencast to device
  5. Simple screen to device
  6. Compress to mp4
  7. Compress to mobile

Basic screen recording

# https://trac.ffmpeg.org/wiki/Capture/Desktop
# -r 30 => 30 fps
# -s 1680x724 => screen size
# -i :0.0+200,300 grab image from upper-left corner at (x=200, y=300)
# -f alsa -ac 2 -i hw:0  output.mkv => grab audio with alsa capture (failed!)
# -f pulse -ac 2 -i default output.mkv => grab audio with pulse capture

ffmpeg -r 30 -s 1680x724 -f x11grab -i :0.0+200,300 -vcodec msmpeg4v2 -qscale 2 /tmp/video.avi

Screen/mic

ffmpeg -r 30 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -f x11grab -i :0.0+200,300 -vcodec msmpeg4v2 -f pulse -ac 2 -i default -qscale 2 /tmp/video.avi

Screen/cam/mic

#!/usr/bin/env bash

if [ -n "$1" ]; then file="$1"; else file="screencast"; fi

echo "Saving screencast in '$file'."
ffmpeg -f alsa -i default \
       -f x11grab -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -r 25 \
       -i :0.0 -f video4linux2 \
       -i /dev/video0 -filter_complex '[2:v]scale=380:-1[cam];[1:v][cam]overlay=W-w-8:H-h-8' \
       -c:a flac \
       -qscale 0 $1.mkv

Screencast to device

#!/usr/bin/env bash

ffmpeg -f alsa -i default \
       -f x11grab -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -r 25 \
       -i :0.0 -f video4linux2 \
       -i /dev/video0 -filter_complex '[2:v]scale=380:-1[cam];[1:v][cam]overlay=W-w-8:H-h-8' \
       -c:a flac \
       -qscale 0 \
       -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video1

Simple screen to device

ffmpeg -f x11grab -r 15 -s 1920x1024 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video1

Compress to mp4

ffmpeg -i /tmp/video.mkv -vcodec libx264 -crf 22 -threads 0 /tmp/video.mp4

Compress to mobile

# record half screen
ffmpeg -r 30 -s 640x924 -f x11grab -i :0.0+1366,100 -vcodec msmpeg4v2 -qscale 2 /tmp/video.avi

# compress to mobile
ffmpeg -i /tmp/video.avi -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p /tmp/mobile.mp4