Picture in Picture Video with FFMPeg
Creating a video with a smaller thumbnail video inside is known as a picture in picture effect. I’ve used other video editors in the past to merge various camera views together to give a more complete experience. In this post I’ll cover how to use FFMpeg to do the same thing. It expands the idea of tiled video, were multiple cameras share equal amounts of screen space. Thumbnails offer flavoring for the main camera view. I’m going to step by step through an example with the final results. Let’s begin.
First, I’ll get my segments I want to use from my stock footage.
I have a camera called L3 and the content I’m intersted in starts at 0:05 to 0:27, for 22 seconds in length.
My next camera called L4 starts at 3:00 to 3:22, again at 22 seconds in length.
ffmpeg -ss 00:00:05 -i "L3.MOV" -t 00:00:22 -c copy "L3_Segment.MOV" ffmpeg -ss 00:03:00 -i "L4.MOV" -t 00:00:22 -c copy "L4_Segment.MOV"
Next, I’ll extract a still image from the segments so I can determine the best placement for insert. I’m extracting the first frame at 12 seconds. This makes it easier to test my settings without wasting time processing entire video clips with FFMpeg.
ffmpeg -i "L3_Segment.MOV" -ss 00:00:12.00 -vframes 1 "L3_Segment.MOV.PNG" ffmpeg -i "L4_Segment.MOV" -ss 00:00:12.00 -vframes 1 "L4_Segment.MOV.PNG"
I’ll be scaling the inserts down from 1920×1080 to 720×405, or .375 times smaller.
L3 will have L4 inserted at 1180,655 that will span to 1900,1060
L4 will have L3 inserted at 20,20 that will span to 740×425
The pic in pic command does the scaling already and the placement is done from the lower right hand corner. This inserts the thumbnail with its lower right hand corner at a place that is a negative x,y coordinate with respect to the main view lower right hand corner’s point used as a reference.
ffmpeg -i "L3_Segment.MOV.PNG" -vf "movie=L4_Segment.MOV.PNG, scale=720:405 [vid2]; [in][vid2] overlay=main_w-overlay_w-20:main_h-overlay_h-20" "L3_Segment_PicInPic.MOV.PNG" ffmpeg -i "L4_Segment.MOV.PNG" -vf "movie=L3_Segment.MOV.PNG, scale=720:405 [vid2]; [in][vid2] overlay=main_w-overlay_w-1180:main_h-overlay_h-655" "L4_Segment_PicInPic.MOV.PNG"
Once all looks fine, I’ll run the same command against the video segments.
ffmpeg -i "L3_Segment.MOV" -vf "movie=L4_Segment.MOV, scale=720:405 [vid2]; [in][vid2] overlay=main_w-overlay_w-20:main_h-overlay_h-20" "L3_Segment_PicInPic.MOV" ffmpeg -i "L4_Segment.MOV" -vf "movie=L3_Segment.MOV, scale=720:405 [vid2]; [in][vid2] overlay=main_w-overlay_w-1180:main_h-overlay_h-655" "L4_Segment_PicInPic.MOV"
Next, I’ll draw a rectangle box around the inserted video, just to give it some definition. My L3 shot was slightly higher than my rectangle, so I placed the rectangle up one pixel and expanded it out by one pixel. Sometimes you have to tweak the placements, so experiment with stills, it’s faster.
ffmpeg -i "L3_Segment_PicInPic.MOV" -vf drawbox=x=1179:y=654:w=721:h=406:color=black@1 "L3_Segment_PicInPic_Rectangle.MOV" ffmpeg -i "L4_Segment_PicInPic.MOV" -vf drawbox=x=20:y=20:w=720:h=405:color=black@1 "L4_Segment_PicInPic_Rectangle.MOV"
Now, I’ll concatenate both videos into one video before I upload to Youtube.
ffmpeg -f concat -safe 0 -i "mylist.txt" -c copy "Final_Segment_PicInPic_Rectangle.MOV"
Here are the results.