{"id":3047,"date":"2017-07-31T00:00:53","date_gmt":"2017-07-31T07:00:53","guid":{"rendered":"http:\/\/192.168.3.4\/?p=3047"},"modified":"2018-01-09T06:51:50","modified_gmt":"2018-01-09T14:51:50","slug":"picture-in-picture-video-with-ffmpeg","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=3047","title":{"rendered":"Picture in Picture Video with FFMPeg"},"content":{"rendered":"<p>Creating a video with a smaller thumbnail video inside is known as a picture in picture effect.\u00a0 I&#8217;ve used other video editors in the past to merge various camera views together to give a more complete experience.\u00a0 In this post I&#8217;ll cover how to use FFMpeg to do the same thing.\u00a0 It expands the idea of tiled video, were multiple cameras share equal amounts of screen space.\u00a0 Thumbnails offer flavoring for the main camera view.\u00a0 I&#8217;m going to step by step through an example with the final results.\u00a0 Let&#8217;s begin.<\/p>\n<p>First, I&#8217;ll get my segments I want to use from my stock footage.<\/p>\n<p>I have a camera called L3 and the content I&#8217;m intersted in starts at 0:05 to 0:27, for 22 seconds in length.<br \/>\nMy next camera called L4 starts at 3:00 to 3:22, again at 22 seconds in length.<\/p>\n<pre>ffmpeg -ss 00:00:05 -i \"L3.MOV\" -t 00:00:22 -c copy \"L3_Segment.MOV\"\r\nffmpeg -ss 00:03:00 -i \"L4.MOV\" -t 00:00:22 -c copy \"L4_Segment.MOV\"\r\n<\/pre>\n<p>Next, I&#8217;ll extract a still image from the segments so I can determine the best placement for insert. I&#8217;m extracting the first frame at 12 seconds.\u00a0 This makes it easier to test my settings without wasting time processing entire video clips with FFMpeg.<\/p>\n<pre>ffmpeg -i \"L3_Segment.MOV\" -ss 00:00:12.00 -vframes 1 \"L3_Segment.MOV.PNG\"\r\nffmpeg -i \"L4_Segment.MOV\" -ss 00:00:12.00 -vframes 1 \"L4_Segment.MOV.PNG\"\r\n<\/pre>\n<p>I&#8217;ll be scaling the inserts down from 1920&#215;1080 to 720&#215;405, or .375 times smaller.<\/p>\n<p>L3 will have L4 inserted at 1180,655 that will span to 1900,1060<br \/>\nL4 will have L3 inserted at 20,20 that will span to 740&#215;425<\/p>\n<p>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&#8217;s point used as a reference.<\/p>\n<pre>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\"\r\nffmpeg -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\"\r\n<\/pre>\n<p>Once all looks fine, I&#8217;ll run the same command against the video segments.<\/p>\n<pre>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\"\r\nffmpeg -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\"\r\n<\/pre>\n<p>Next, I&#8217;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&#8217;s faster.<\/p>\n<pre>ffmpeg -i \"L3_Segment_PicInPic.MOV\" -vf drawbox=x=1179:y=654:w=721:h=406:color=black@1 \"L3_Segment_PicInPic_Rectangle.MOV\"\r\nffmpeg -i \"L4_Segment_PicInPic.MOV\" -vf drawbox=x=20:y=20:w=720:h=405:color=black@1 \"L4_Segment_PicInPic_Rectangle.MOV\"\r\n<\/pre>\n<p>Now, I&#8217;ll concatenate both videos into one video before I upload to Youtube.<\/p>\n<pre>ffmpeg -f concat -safe 0 -i \"mylist.txt\" -c copy \"Final_Segment_PicInPic_Rectangle.MOV\"\r\n<\/pre>\n<p>Here are the results.<\/p>\n<p><iframe loading=\"lazy\" title=\"FFMpeg Picture in Picture\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/WBMuihgHe-w?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a video with a smaller thumbnail video inside is known as a picture in picture effect.\u00a0 I&#8217;ve used other video editors in the past to merge various camera views together to give a more complete experience.\u00a0 In this post I&#8217;ll cover how to use FFMpeg to do the same thing.\u00a0 It expands the idea of tiled video, were multiple cameras share equal amounts of screen space.\u00a0 Thumbnails offer flavoring for the main camera view.\u00a0 I&#8217;m going to step by&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=3047\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,6],"tags":[],"class_list":["post-3047","post","type-post","status-publish","format-standard","hentry","category-computer-vision","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/3047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3047"}],"version-history":[{"count":5,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/3047\/revisions"}],"predecessor-version":[{"id":3053,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/3047\/revisions\/3053"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}