title: Construct ffmpeg command to do picture-in-picture for part of a video
body: <p>I run a programming hobbyist users' group, and I've started to video-record the talks given each time we meet.  I'm trying to construct an ffmpeg command to take two video input streams, one a screen capture and the other of a speaker, and combine them into one video with picture-in-picture, taking the audio from the speaker video.  I have basic picture-in-picture working in a simple ffmpeg command, but I want to be able to specify that the beginning and end of the video show just the speaker, while the middle is picture-in-picture with the speaker in the upper right-hand corner.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>ffmpeg \
    -ss 22 \
    -i "capture.mp4" \
    -ss 32 \
    -i "speaker.mov" \
    -filter_complex "[1] split=3[s1][s2][s3]; [s1] trim=0:10, scale=1920x1080 [prefix]; [s2] trim=10:, scale=iw/5:ih/5 [pip]; [s3] trim=0:0, scale=1920x1080 [suffix]; [0] copy [c]; [c][pip] overlay=main_w-overlay_w:0 [body]; [prefix][body][suffix] concat=n=3 [out]" \
    -map "[out]" \
    -profile:v baseline \
    -level 3.0 \
    -b:v 440k \
    -vcodec libx264 \
    -acodec copy \
    -y \
    ffmpeg-test.mov
  </code></pre>
  
  <p>Right now, there are two problems:</p>
  
  <ul>
  <li><p>no sound appears in the output</p></li>
  <li><p>in the picture-in-picture section, the speaker's motion isn't smooth; the video jumps as if he had been sampled every half second even though the speaker video is at 29.97 fps.  (Perhaps the problem is that it's matching the capture frame rate, which is 5 fps.)</p></li>
  </ul>
  
  <p>I'm trying to do this in just one command in order to make the processing as quick as possible.</p>
  
  <p>Is there any way to accomplish this with ffmpeg?  It would really help me to be able to automate this process as much as possible.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: Per LordNeckbeard's request, here is the full console output:</p>
  
  <pre><code>ffmpeg version 2.8.1 Copyright (c) 2000-2015 the FFmpeg developers
    built with Apple LLVM version 7.0.0 (clang-700.0.72)
    configuration: --prefix=/usr/local/Cellar/ffmpeg/2.8.1_1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-vda
    libavutil      54. 31.100 / 54. 31.100
    libavcodec     56. 60.100 / 56. 60.100
    libavformat    56. 40.101 / 56. 40.101
    libavdevice    56.  4.100 / 56.  4.100
    libavfilter     5. 40.101 /  5. 40.101
    libavresample   2.  1.  0 /  2.  1.  0
    libswscale      3.  1.101 /  3.  1.101
    libswresample   1.  2.101 /  1.  2.101
    libpostproc    53.  3.100 / 53.  3.100
  Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'capture.mp4':
    Metadata:
  major_brand     : mp42
  minor_version   : 0
  compatible_brands: mp42isomavc1
  creation_time   : 2015-11-09 17:29:41
    Duration: 01:42:51.90, start: 0.000000, bitrate: 115 kb/s
  Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 4 kb/s (default)
  Metadata:
    handler_name    : AAC stream handler
  Stream #0:1(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 109 kb/s, 5 fps, 5 tbr, 90k tbn, 180k tbc (default)
  Metadata:
    handler_name    : Citrix h264 stream handler
    encoder         : AVC Coding
  Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'speaker.mov':
    Metadata:
  major_brand     : qt  
  minor_version   : 537199360
  compatible_brands: qt  
  creation_time   : 2015-11-09 21:17:52
    Duration: 00:59:57.60, start: 0.000000, bitrate: 7264 kb/s
  Stream #1:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(bt709), 1440x1080 [SAR 4:3 DAR 16:9], 5724 kb/s, 29.97 fps, 29.97 tbr, 2997 tbn, 59.94 tbc (default)
  Metadata:
    creation_time   : 2015-11-09 21:17:52
    handler_name    : Apple Alias Data Handler
    encoder         : AVCHD 1080i60
    timecode        : 00:00:00;00
  Stream #1:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s (default)
  Metadata:
    creation_time   : 2015-11-09 21:17:52
    handler_name    : Apple Alias Data Handler
  Stream #1:2(eng): Data: none (tmcd / 0x64636D74)
  Metadata:
    creation_time   : 2015-11-09 21:17:52
    handler_name    : Apple Alias Data Handler
    timecode        : 00:00:00;00
  [libx264 @ 0x7f99a980de00] using SAR=1/1
  [libx264 @ 0x7f99a980de00] frame MB size (120x68) &gt; level limit (1620)
  [libx264 @ 0x7f99a980de00] DPB size (1 frames, 8160 mbs) &gt; level limit (0 frames, 8100 mbs)
  [libx264 @ 0x7f99a980de00] MB rate (244555) &gt; level limit (40500)
  [libx264 @ 0x7f99a980de00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
  [libx264 @ 0x7f99a980de00] profile Constrained Baseline, level 3.0
  [libx264 @ 0x7f99a980de00] 264 - core 148 r2601 a0cd7d3 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=440 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
  Output #0, mov, to 'cli-ffmpeg-test.mov':
    Metadata:
  major_brand     : mp42
  minor_version   : 0
  compatible_brands: mp42isomavc1
  encoder         : Lavf56.40.101
  Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 440 kb/s, 29.97 fps, 11988 tbn, 29.97 tbc (default)
  Metadata:
    encoder         : Lavc56.60.100 libx264
  Stream mapping:
    Stream #0:1 (h264) -&gt; copy
    Stream #1:0 (h264) -&gt; split
    concat -&gt; Stream #0:0 (libx264)
  Press [q] to stop, [?] for help
  frame=184483 fps=117 q=-1.0 Lsize=  256071kB time=01:42:35.58 bitrate= 340.8kbits/s dup=153456 drop=0    
  video:255343kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.285118%
  [libx264 @ 0x7f99a980de00] frame I:779   Avg QP: 5.70  size:181781
  [libx264 @ 0x7f99a980de00] frame P:183704 Avg QP:10.00  size:   652
  [libx264 @ 0x7f99a980de00] mb I  I16..4: 77.8%  0.0% 22.2%
  [libx264 @ 0x7f99a980de00] mb P  I16..4:  0.1%  0.0%  0.0%  P16..4:  0.4%  0.1%  0.1%  0.0%  0.0%    skip:99.3%
  [libx264 @ 0x7f99a980de00] final ratefactor: 13.35
  [libx264 @ 0x7f99a980de00] coded y,uvDC,uvAC intra: 21.1% 28.5% 19.4% inter: 0.2% 0.2% 0.1%
  [libx264 @ 0x7f99a980de00] i16 v,h,dc,p: 74% 23%  1%  2%
  [libx264 @ 0x7f99a980de00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 26% 15%  4%  4%  5%  4%  4%  5%
  [libx264 @ 0x7f99a980de00] i8c dc,h,v,p: 68% 20% 10%  1%
  [libx264 @ 0x7f99a980de00] kb/s:339.82
  </code></pre>
  
  <p>Thanks again.</p>