4

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.

2
  • In principle, that looks good. If you can't get audio, first step is to verify you got the correct .monitor source. so echo $A, start pavucontrol, verify that the sink corresponding to it is the one that gets the sound. Next step is to verify that you can record from it with some other program, e.g. parecord. Next step is to try ffmpeg to record only audio.
    – dirkt
    Dec 15, 2018 at 10:36
  • @dirkt thanks apparently I did break a ffmpeg principle but I got it working now :) . Dec 17, 2018 at 17:00

1 Answer 1

7

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.

2
  • That might be a stupid question, but why the second -f pulse -i default? Do you want to record from a second audio source (because the original audio is only about the desktop/internal audio)?
    – Alf
    Jul 15, 2020 at 18:43
  • Wayland example; ffmpeg.org/ffmpeg-devices.html#Examples-4 Mar 18, 2021 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.