21

I understand that this is a very open ended question. I have done some initial reading into FFmpeg, but now require some guidance.

Problem

  • I have a video input.mov.
  • I would like to overlay another video on top of overlay.wov.
  • The result should be a single video (output.mov).

Notes

Thanks - C.

Edits

  1. Backend is Go/Ruby. Open to using a new language.
  2. The audio from the first video should be kept.
  3. Setting the interval at which the overlay starts would be great.

Current Solution

ffmpeg -i input.mov -i overlay.mov -filter_complex "[0:0][1:0]overlay[out]" -shortest -map [out] -map 0:1 -pix_fmt yuv420p -c:a copy -c:v libx264 -crf 18  output.mov

This nearly works, however:

  • Overlay is cut short even though the two videos (input.mov & overlay.mov) are the same length.
  • I cannot start the overlay at any interval apart from 0:00.
Ryan M
  • 18,333
  • 31
  • 67
  • 74
cdrev
  • 5,750
  • 4
  • 20
  • 27
  • what backend technology you are using? PHP, .NET, struts, python!!? – Tarunn Feb 08 '16 at 12:19
  • @Tarunn - added answer to post. – cdrev Feb 08 '16 at 12:37
  • @LordNeckbeard thanks for the comment, have added quite a lot more detail and included my current solution. I believe it is different due to the audio and interval requirements. – cdrev Feb 09 '16 at 11:10

1 Answers1

25

If you just want a ffmpeg command, try

ffmpeg -i input.mov -i overlay.mov \
-filter_complex "[1:v]setpts=PTS-10/TB[a]; \
                 [0:v][a]overlay=enable=gte(t\,5):shortest=1[out]" \
-map [out] -map 0:a \
-c:v libx264 -crf 18 -pix_fmt yuv420p \
-c:a copy \
output.mov

This starts the overlay at 5 seconds with the overlaid video start point being 00:15.

setpts=PTS-10/TB is setpts=PTS+(overlay_delay-video_trim_in)/TB

overlay=enable=gte(t\,5) is overlay=enable=gte(t\,overlay_delay)

Gyan
  • 85,394
  • 9
  • 169
  • 201
  • Thanks for the answer! Slightly confused by the interval. Could you explain it with these parameters if possible? `input.mov` is 10 secs long. `overlay.mov` is 3 secs long. `output.mov` should be 10 secs long. `overlay.mov` should start at 7 seconds into `input.mov` – cdrev Feb 09 '16 at 15:11
  • 1
    Overlay starts at 7, so `overlay=enable=gte(t\,7)`. Overlay.mov is shown from its beginning, so `setpts=PTS+7-0/TB` == `setpts=PTS+7/TB` – Gyan Feb 09 '16 at 15:26
  • 2
    How about if i want to have also the audio kept in both videos? Because now when i try it, the audio from the overlay is gone. Thanks! – gabo Jan 05 '17 at 13:52
  • Insert `[0][1]amix[a]` into the filtergraph after the overlay and change `-map 0:a` to `-map [a]` – Gyan Jan 05 '17 at 14:50
  • @Gyan can you please give an example of how to add this `[0][1]amix[a]` Is it supposed to be added in the same chain as overlay command? – Qandeel Abbassi Dec 02 '18 at 19:25
  • I got it working but i had to remove `-c:a copy` since copy can't be used with filters, right? – Qandeel Abbassi Dec 02 '18 at 21:53
  • @Gyan what should we change (in your command) if we want the overlay start at `0`, like what OP was doing? I did `gte(t\,0)` and `setpts=PTS+0/TB` but that didn't work. – Tina J Jan 04 '19 at 19:30
  • @Gyan I have a very similar question and I have found a working solution, however it seems overly complex, if you have an idea (because you seems to be a ffmpeg guru!), it would be great :) https://stackoverflow.com/questions/66256414/mixing-various-audio-and-video-sources-into-a-single-video – Basj Feb 18 '21 at 11:06