Learn
FFmpeg
Recipes
Picture in Picture

Creating Picture-in-Picture Videos with FFmpeg

Picture-in-picture (PiP) is a popular feature in multimedia technology, allowing a smaller video to be displayed within a larger one. This feature is commonly used in broadcasting, video conferencing, and online tutorials. FFmpeg, a versatile command-line tool, provides a straightforward way to create PiP videos. This article will delve into the details of creating PiP videos with FFmpeg, including aspects like PiP dimensions, rounded borders, position, and more.

Understanding Picture-in-Picture

Picture-in-picture (PiP) is a feature that allows a video to be displayed in a small window on top of another video. This is particularly useful in scenarios where you want to show two videos simultaneously, such as a video call within a presentation or a game walkthrough with a commentator.

Creating PiP Videos with FFmpeg

FFmpeg provides a powerful and flexible way to create PiP videos using its filter_complex option. This option allows you to define a complex filtergraph for processing your videos. Here's a basic example of how to create a PiP video with FFmpeg:

ffmpeg -i main.mp4 -i pip.mp4 -filter_complex "[1]scale=iw/4:ih/4 [pip]; [0][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10" output.mp4

In this command, -i main.mp4 and -i pip.mp4 specify the main video and the PiP video, respectively. The -filter_complex option defines a filtergraph that first scales the PiP video to one-fourth of its original size (scale=iw/4:ih/4) and then overlays it on the main video at the bottom right corner (overlay=main_w-overlay_w-10:main_h-overlay_h-10).

Adjusting the PiP Dimensions

You can adjust the size of the PiP video by changing the scale parameters. For example, if you want the PiP video to be half the size of the main video, you can change scale=iw/4:ih/4 to scale=iw/2:ih/2.

Adding Rounded Borders

FFmpeg doesn't directly support adding rounded borders to the PiP video. However, you can achieve a similar effect by first creating a mask image with rounded corners and then overlaying it on the PiP video.

Here's an example command:

ffmpeg -i main.mp4 -i pip.mp4 -i mask.png -filter_complex "[1][2]alphamerge[masked]; [0][masked]overlay" output.mp4

In this command, main.mp4 is the main video, pip.mp4 is the PiP video, and mask.png is the mask image. The alphamerge filter is used to apply the mask to the PiP video, and the overlay filter is used to overlay the masked PiP video onto the main video.

Positioning the PiP Video

You can change the position of the PiP video by adjusting the overlay parameters. For example, if you want the PiP video to be displayed at the top right corner, you can change overlay=main_w-overlay_w-10:main_h-overlay_h-10 to overlay=main_w-overlay_w-10:10.

Conclusion

Creating picture-in-picture videos with FFmpeg is a powerful way to enhance your multimedia content. With its flexible command syntax and extensive feature set, FFmpeg allows you to create PiP videos that meet your specific needs, whether it's adjusting the PiP dimensions, adding rounded borders, or changing the position of the PiP video.