PICTURE IN PICTURE effect using FFMPEG
Posted By : Akash Sharma | 09-Nov-2013
In a recent time I had a task to make a picture in picture effect of two videos using ffmpeg.In this blog I am going to share the details of how to make a PIP effect using ffmpeg and also configuring some of its factors.
I have two flv videos with download links :
Here are the youtube links:
I have ffmpeg commands for various PIP combinations.
Combination 1: overlaying pipInput2.flv on bottom right with sound of pipInput1.flv video
Following is the command for doing this operation.
For Windows
ffmpeg -i pipInput1.flv -i pipInput2.flv -filter_complex "[1]scale=iw/5:ih/5 [pip]; [0][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10" -profile:v main -level 3.1 -b:v 440k -ar 44100 -ab 128k -s 720x400 -vcodec h264 -acodec libvo_aacenc PIP_output1.mp4
For Ubuntu
ffmpeg -i pipInput1.flv -i pipInput2.flv -filter_complex "[1]scale=iw/5:ih/5 [pip]; [0][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10" -profile:v main -level 3.1 -b:v 440k -ar 44100 -ab 128k -s 720x400 -vcodec h264 -acodec libfaac PIP_output1.mp4
Filters in ffmpeg are used configure input file parameters based on some multiple parallel processing.Filters can have multiple inputs and multiple outputs.Filters in the same linear chain are separated by commas, and distinct linear chains of filters are separated by semicolons.The points where the linear chains join are labelled by names enclosed in square brackets.
In this command, there are two filters one after the other in chain.First filter scale down one video, and second filter overlays one video on another.
-filter_complex "[1]scale=iw/5:ih/5 [pip]; [0][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10"
scale option will reduce the size of the video having index=1 to width=w/5 and height=h/5.Indexing of files starts with 0 i.e. pipInput1.flv has index 0 and pipInput2.flv has index 1.
The scaled version of pipInput2.flv video has named as pip in the filter chain.
Next this scaled pip video and video at index=0 i.e. “[0][pip]” are allowed to overlayed.That is, pipInput2.flv will be on full screen and pipInput1.flv will be overlayed.
The position for the scaled version of pipInput2.flv will be decided by parameters of “overlay” parameter.
dimension of pipInput1.flv video = main_w
dimension of scaled pipInput2.flv video = overlay_w
PIP output video:
NOTE: Sound of first video in ffmpeg command, having index=0, will come in final PIP output video.
Combination 2: overlaying pipInput2.flv on bottom right with sound of pipInput2.flv video
For Windows
ffmpeg -i pipInput2.flv -i pipInput1.flv -filter_complex "[0]scale=iw/5:ih/5 [pip]; [1][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10" -profile:v main -level 3.1 -b:v 440k -ar 44100 -ab 128k -s 720x400 -vcodec h264 -acodec libvo_aacenc PIP_output2.mp4
For Ubuntu
ffmpeg -i pipInput2.flv -i pipInput1.flv -filter_complex "[0]scale=iw/5:ih/5 [pip]; [1][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10" -profile:v main -level 3.1 -b:v 440k -ar 44100 -ab 128k -s 720x400 -vcodec h264 -acodec libfaac PIP_output2.mp4
To make PIP video having sound of pipInput2.flv it must have index=0 in ffmpeg command.But this will change the indexing for creating scaled video.so just swaping index of 0 and 1.
PIP output video:
Combination 3: overlaying pipInput1.flv on top right with sound of pipInput2.flv video
For Windows
ffmpeg -i pipInput2.flv -i pipInput1.flv -filter_complex "[1]scale=iw/5:ih/5 [pip]; [0][pip] overlay=main_w-overlay_w-10:10" -profile:v main -level 3.1 -b:v 440k -ar 44100 -ab 128k -s 720x400 -vcodec h264 -acodec libvo_aacenc PIP_output3.mp4
For Ubuntu
ffmpeg -i pipInput2.flv -i pipInput1.flv -filter_complex "[1]scale=iw/5:ih/5 [pip]; [0][pip] overlay=main_w-overlay_w-10:10" -profile:v main -level 3.1 -b:v 440k -ar 44100 -ab 128k -s 720x400 -vcodec h264 -acodec libfaac PIP_output3.mp4
Replacing “main_h-overlay_h-10” with “10” will make the overlayed video at top right.
PIP output video:
Thanks
Akash Sharma
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis