Recording Your Screen and Webcam
To create a Loom-like video, you first need to capture two distinct sources: your desktop environment and your face via a webcam. While specialized software exists, FFmpeg can handle both tasks simultaneously if configured correctly. The process varies slightly depending on your operating system because each platform uses different backend libraries for screen capture.
On Linux, the x11grab input device is standard for X11-based systems. You must specify the display name and the area to capture. For example, to record the entire primary display at 30 frames per second, use the following command structure:
ffmpeg -f x11grab -r 30 -s 1920x1080 -i :0.0+0,0 output.mp4
The :0.0+0,0 syntax tells FFmpeg to grab from display :0, screen 0, starting at offset 0,0. If you are using Wayland, you may need to use pulseaudio for audio and potentially gdigrab equivalents or third-party tools like OBS as an intermediary, as native Wayland support in FFmpeg has historically been limited compared to X11.
For Windows users, the dshow or gdigrab filters are commonly used. To list available devices, run ffmpeg -list_devices true -f dshow -i dummy. Once identified, you can combine screen and camera inputs. On macOS, the avfoundation input device is the most reliable method. It allows you to select specific devices by index or name.
Compositing Multiple Inputs
Once you have separate video files for your screen and your webcam, or if you are recording them in real-time using complex filters, you need to merge them visually. This is where FFmpeg's filtergraph comes into play. The goal is to place the webcam footage in a corner of the screen recording, similar to how Loom positions the user avatar.
You will use the overlay video filter. This filter takes two input streams and draws one on top of the other. Suppose you have a screen recording named screen.mp4 and a webcam clip named webcam.mp4. The basic command to overlay the webcam in the bottom-right corner looks like this:
ffmpeg -i screen.mp4 -i webcam.mp4 \
-filter_complex "overlay=W-w-10:H-h-10" \
-c:a copy output_loom.mp4
In this command, W-w-10 calculates the X-coordinate by taking the width of the main video (W) minus the width of the overlay (w) minus a 10-pixel margin. Similarly, H-h-10 handles the vertical positioning. This ensures the webcam box does not touch the edge of the screen recording, providing a clean aesthetic.
If you need to resize the webcam feed before overlaying it, you can chain filters within the -filter_complex argument. For instance, to scale the webcam to half its original size and then position it:
ffmpeg -i screen.mp4 -i webcam.mp4 \
-filter_complex "[1:v]scale=iw/2:ih/2[wm];[0:v][wm]overlay=W-w-10:H-h-10" \
-c:a copy output_scaled.mp4
This approach is highly efficient because it avoids re-encoding the entire screen video if you only adjust the overlay parameters. However, note that adding filters often forces a re-encode of the video stream unless you use stream copying for non-modified parts, which is not possible with the overlay filter since it modifies the pixel data.
Handling Audio Mixing
A Loom-style video typically includes narration from the microphone. If you recorded audio separately or captured it alongside the video, you must mix it with any existing system audio from the screen recording. FFmpeg’s aformat and amix filters facilitate this.
Assuming you have audio tracks from both sources, you can combine them using the amix filter. This is crucial because simply appending audio streams results in silent or distorted playback. The command might look like:
ffmpeg -i screen.mp4 -i webcam.mp4 \
-filter_complex "[0:a][1:a]amix=inputs=2:duration=first:dropout_transition=2[a]" \
-map "0:v" -map "[a]" output_final.mp4
The inputs=2 parameter specifies that two audio streams are being combined. The duration=first option ensures the output audio matches the length of the shortest input, preventing errors if one track is longer than the other. Adjusting the volume levels individually before mixing can prevent clipping, ensuring clear narration over background sounds.
Optimizing Output Settings
After composing the visual and audio elements, the final step is encoding the result into a shareable format. MP4 with H.264 video and AAC audio is the universal standard for web sharing. Use the -crf (Constant Rate Factor) flag to control quality. A value between 18 and 23 is generally recommended for high-quality recordings without excessive file sizes.
ffmpeg -i screen.mp4 -i webcam.mp4 \
-filter_complex "[1:v]scale=iw/2:ih/2[wm];[0:v][wm]overlay=W-w-10:H-h-10" \
-c:v libx264 -crf 21 -preset medium -c:a aac output_loom_final.mp4
The -preset medium setting offers a good balance between encoding speed and compression efficiency. Faster presets like fast may increase file size, while slower ones like slow take significantly longer to process with diminishing returns on quality. Always test with a short clip before processing a long recording to ensure the filtergraph logic holds up under load.
For more advanced editing, such as concatenating multiple clips while preserving timestamps, refer to our guide on FFmpeg: Honoring Outpoint Timestamps with Concat Demuxer and Preserving Input Codec Parameters. Additionally, if you wish to stack videos vertically rather than overlay them, check out Efficient Way of Stacking Two Videos Vertically Using FFmpeg.
Frequently Asked Questions
Why is my webcam overlay appearing black?
This usually happens if the webcam input fails to initialize or if the resolution mismatch causes rendering issues. Ensure your webcam is not being used by another application like Zoom or Teams, as these apps lock the device exclusively. Also, verify that the avfoundation or dshow device index corresponds to the correct camera in your system list.
Can I add text annotations to the video?
Yes, you can add text overlays using the drawtext filter within the -filter_complex chain. You would define the text string, font path, and position coordinates similarly to how you positioned the webcam overlay. This allows you to add titles or callouts directly during the FFmpeg processing stage.
How do I reduce the file size of the final video?
Increase the CRF value above 23, though this reduces quality. Alternatively, switch from libx264 to libx265 (HEVC), which offers better compression at the same quality level but requires significantly more CPU power during encoding. You can also lower the frame rate if 30fps is unnecessary for your content.
Is it possible to record and edit in real-time?
FFmpeg processes files sequentially, so it cannot "live edit" and output simultaneously in the way a video editor like Premiere Pro does. However, you can pipe outputs using Unix pipes or named pipes to create near-real-time workflows, but this introduces complexity and potential latency. For live streaming, consider using RTMP output instead of local file recording.
What should I do if the audio is out of sync?
Audio desync often occurs when video frames are dropped or when mixing audio streams with different sample rates. Ensure both input videos have consistent frame rates. When mixing, use the aresample filter to normalize sample rates before feeding them into amix. Checking the timestamps with ffprobe can help diagnose drift issues.