Skip to content

Instantly share code, notes, and snippets.

@psyburr
Last active December 27, 2024 06:16
Show Gist options
  • Select an option

  • Save psyburr/e0aa6072a57dbd151a528b9462266a82 to your computer and use it in GitHub Desktop.

Select an option

Save psyburr/e0aa6072a57dbd151a528b9462266a82 to your computer and use it in GitHub Desktop.
Quick How-To guide on recording PulseAudio with ffmpeg

[[[ Capturing your Desktop / Screen Recording ]]]

[[PageOutline(1, Contents)]]

Here are a few solutions for capturing your desktop and recording a video of your screen with ffmpeg. (A Chinese version of this page is [[Capture/Desktop中文版本|also available]].)

For the sake of brevity, these commands do not specify any additional encoder settings. For more info about H.264 encoding, see [[Encode/H.264|the H.264 encoding guide]].

By default, these commands will use the x264 encoder, which should be reasonably fast on modern machines. See [#lossless-recording Lossless Recording] if you need to improve performance.

= Linux =

Use the [https://ffmpeg.org/ffmpeg-devices.html#x11grab x11grab] device: {{{ ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 output.mp4 }}}

This will grab the image from desktop, starting with the upper-left corner at x=100, y=200 with a width and height of 1024⨉768.

If you need audio too, you can use [https://ffmpeg.org/ffmpeg-devices.html#alsa-1 ALSA] (see [[Capture/ALSA]] for more info): {{{ ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 -f alsa -ac 2 -i hw:0 output.mkv }}}

Or the [https://ffmpeg.org/ffmpeg-devices.html#pulse pulse] input device (see [[Capture/PulseAudio]] for more info): {{{ ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 -f pulse -ac 2 -i default output.mkv }}}

= macOS =

Use the [https://ffmpeg.org/ffmpeg-devices.html#avfoundation avfoundation] device: {{{ ffmpeg -f avfoundation -list_devices true -i "" }}}

This will enumerate all the available input devices including screens ready to be captured.

Once you've figured out the device index corresponding to the screen to be captured, use: {{{ ffmpeg -f avfoundation -i ":" output.mkv }}}

This will capture the screen from <screen device index> and audio from <audio device index> into the output file output.mkv.

= Windows =

== Use DirectShow ==

Use a [[DirectShow]] [https://github.com/rdp/screen-capture-recorder-to-video-windows-free device]: {{{ ffmpeg -f dshow -i video="screen-capture-recorder" output.mkv }}}

This will grab the image from entire desktop. You can refer to a [http://betterlogic.com/roger/2010/07/list-of-available-directshow-screen-capture-filters/ list of alternative devices].

If you need audio too: {{{ ffmpeg -f dshow -i video="UScreenCapture":audio="Microphone" output.mkv }}} If you want to capture the audio that is playing from your speakers you may also need to configure so-called "Stereo Mix" device.

or {{{ ffmpeg -f dshow -i video="UScreenCapture" -f dshow -i audio="Microphone" output.mkv }}}

You can list your devices with: {{{ ffmpeg -list_devices true -f dshow -i dummy }}}

== Use built-in GDI screengrabber ==

You can also use [https://ffmpeg.org/ffmpeg-devices.html#gdigrab gdigrab] as input device to grab video from the Windows screen.

To capture all your displays as one big contiguous display: {{{ ffmpeg -f gdigrab -framerate 30 -i desktop output.mkv }}}

If you want to limit to a region, and show the area being grabbed: {{{ ffmpeg -f gdigrab -framerate 30 -offset_x 10 -offset_y 20 -video_size 640x480 -show_region 1 -i desktop output.mkv }}}

To grab the contents of the window named "Calculator": {{{ ffmpeg -f gdigrab -framerate 30 -i title=Calculator output.mkv }}}

= Hardware Encoding =

You can use [[HWAccelIntro|hardware acceleartion]] to speed up encoding and reduce the load on your CPU. For example, with NVIDIA hardware encoding:

{{{ ffmpeg -f gdigrab -framerate 30 -i desktop -c:v h264_nvenc -qp 0 output.mkv }}}

= Lossless Recording =

If your CPU is not fast enough, the encoding process might take too long. To speed up the encoding process, you can use lossless encoding and disable advanced encoder options, e.g.:

{{{ ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 -c:v libx264rgb -crf 0 -preset ultrafast output.mkv }}}

{{{-crf 0}}} tells x264 to encode in lossless mode; {{{-preset ultrafast}}} advises it to do so fast. Note the use of libx264rgb rather than libx264; the latter would do a lossy conversion from RGB to yuv444p.

The encoder should be fast enough on most modern hardware to record without any framedrop, and even leave enough CPU headroom for other applications.

If you're going to archive the recording or are concerned about file size, re-encode it losslessly again, but with a slower preset. Note that since the initial recording was lossless, and the re-encode is lossless too, no quality loss is introduced in this process in any way.

{{{ ffmpeg -i output.mkv -c:v libx264rgb -crf 0 -preset veryslow output-smaller.mkv }}}

Using FFmpeg

Capturing audio with FFmpeg and PulseAudio is pretty straightforward:

```ffmpeg -f pulse <input_options> -i <input_device> ... OUTPUT.EXT```

See the [https://ffmpeg.org/ffmpeg-devices.html#pulse FFmpeg PulseAudio input device documentation] for more info.


Capturing PulseAudio

Input

  • input_device tells ffmpeg which PulseAudio source you would like to use. To get the list of all PulseAudio available sources, you can type: pactl list short sources If you want more detail for each source, run pactl list sources

  • You can reference sources either by number: -f pulse -i 5 or by name: -f pulse -i alsa_input.pci-0000_00_1b.0.analog-stereo or just use: -f pulse -i default to use the source currently set as default in PulseAudio.

    • You can select the default PulseAudio source running pactl set-default-source SOURCE-NAME

Mixer tools

You can select the source device and the recording level, with different tools. === pavucontrol ===

The easiest way is to use the pavucontrol GUI: go to the Input Devices tab, filter the list of source devices using the Show menu, select a device (and possibly its Port, for example to choose between a built-in microphone or a pluggable one), make sure the device in unmuted, set the recording volume level, using the VU meter. You can set this device as the default source by checking the Set as fallback icon.

=== pactl ===

If you prefer CLIs, or want to automate things, you can achieve the same results issuing pactl commands; for example:

{{{ pactl set-source-port alsa_input.pci-0000_00_1b.0.analog-stereo analog-input-internal-mic pactl set-source-mute alsa_input.pci-0000_00_1b.0.analog-stereo 0 pactl set-source-volume alsa_input.pci-0000_00_1b.0.analog-stereo 60% }}}

will select the internal microphone as source, unmute it (this is the equivalent of turning on the "Capture" feature in ALSA parlance), and set a recording volume level of 60%.

= Input options =

PulseAudio supports common options like -sample_rate (audio sample rate) and -channels (audio channels), and special options like -server, to record from remote hosts running the PulseAudio server. All options are documented at https://ffmpeg.org/ffmpeg-devices.html#pulse

= Examples =

Once you set your source, recording volume and possibly other options, you can issue the complete ffpmpeg command to record sound from a PulseAudio source.

== Record audio from your microphone ==

{{{ ffmpeg -f pulse -i alsa_input.pci-0000_00_1b.0.analog-stereo -ac 1 recording.m4a }}} == Record audio from an application ==

{{{ ffmpeg -f pulse -i alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -ac 2 recording.m4a }}}

Actually this will record audio from ''all'' the applications currently playing sounds; to select a single application, you can use pavucontrol, go to the Playback tab, and mute all the applications you don't want to record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment