Ubuntu – record screen and internal audio with ffmpeg

ffmpegpulseaudiorecordingUbuntu

What is the ffmpeg command to record screen and internal audio (on Ubuntu 18.04)?

I'll omit the many things I tried that did not work and skip to the something close to what I am looking for;

V="$(xdpyinfo | grep dimensions | perl -pe 's/.* ([0-9]+x[0-9]+) .*/$1/g')"
A="$(pacmd list-sources | grep -PB 1 "analog.*monitor>" | head -n 1 | perl -pe 's/.* //g')"
F="$(date --iso-8601=minutes).mkv"
ffmpeg -video_size "$V" -framerate 10 -f x11grab -i :0.0 -f pulse -ac 2 -i "$A" "$F"

I can get video but no audio.

parecord  -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor  example.wav # index: 1

will get audio.

Best Answer

Framerate applied to both streams, but since ffmpeg documentation examples are scattered I'll leave an answer here

A="$(pacmd list-sources | grep -PB 1 "analog.*monitor>" | head -n 1 | perl -pe 's/.* //g')"
F="$(date --iso-8601=minutes | perl -pe 's/[^0-9]+//g').mkv"
V="$(xdpyinfo | grep dimensions | perl -pe 's/.* ([0-9]+x[0-9]+) .*/$1/g')"
ffmpeg -loglevel error -video_size "$V" -f x11grab -i :0.0 -f pulse -i "$A" -f pulse -i default -filter_complex amerge -ac 2 -preset veryfast "$F"

where

#A=1
#F=2018121711440500.mkv
#V=2560x1440
  • ffmpeg the tool
  • -loglevel error only print errors
  • -video_size "$V" resolution of your screen (or less if you only want a subsection recorded)
  • -f x11grab record the screen (screen recordings may not be possible on wayland?)
  • -i :0.0 the X11 screen ID, (can also add +x,y for offset)
  • -f pulse the audio driver
  • -i "$A" the id of the audio stream
  • -f pulse the audio driver again (maybe not needed?)
  • -i default normally the system microphone
  • -filter_complex amerge merge the 2 audio streams
  • -ac 2 convert the 4 audio channels to 2
  • -preset veryfast go light on video encoding to avoid stuttering
  • "$F" the output file

Remember that the parameter order matters, and pavucontrol can re-map audio only while ffmpeg is running.

Related Question