FFmpeg Picture in Picture Command on Linux

This article provides a quick overview and a practical guide on how to create a picture-in-picture (PiP) video effect using FFmpeg on Linux. By leveraging the powerful overlay video filter, you can easily overlay a smaller secondary video onto a main background video. Below, we break down the exact command structure, explain what each parameter does, and provide a few advanced tweaks to customize your video layouts.

To create a picture-in-picture effect where a secondary video is placed in the bottom-right corner of the main video, use the following FFmpeg command:

ffmpeg -i main_video.mp4 -i overlay_video.mp4 -filter_complex "[1:v]scale=480:-1[pip]; [0:v][pip]overlay=W-w-10:H-h-10:shortest=1" -c:a aac output.mp4

Understanding how this command works allows you to customize the output to fit your specific needs. The filter_complex flag is the core of this operation, breaking down into two distinct steps:

  • [1:v]scale=480:-1[pip]: This takes the video stream of the second input (1:v, which is overlay_video.mp4) and rescales it. In this example, it sets the width to 480 pixels and automatically calculates the height (-1) to keep the original aspect ratio. It labels this resized stream as [pip].
  • [0:v][pip]overlay=W-w-10:H-h-10:shortest=1: This takes the main video stream (0:v) and overlays the [pip] stream on top of it.

The positioning variables inside the overlay filter give you precise control over where the smaller video sits:

  • **W and H**: The width and height of the main background video.
  • **w and h**: The width and height of the smaller overlay video.
  • 10: A 10-pixel padding or margin from the edges of the frame.
  • shortest=1: This tell FFmpeg to stop recording as soon as the shortest video ends, preventing a frozen frame if one video is longer than the other.

You can easily change the positioning math to place your video in different corners of the screen depending on your project requirements:

  • Top-Left Corner: overlay=10:10
  • Top-Right Corner: overlay=W-w-10:10
  • Bottom-Left Corner: overlay=10:H-h-10