1

Completely new to working with FFMPEG, what I'm trying to achieve is applying overlaying graphics at certain positions and times, and cutting out sections of a single input video.

I've worked out the overlaying graphics, so this code is working:

ffmpeg -i /Users/username/projectdir/static/video.mp4 -I overlay.png -i overlay2.png -filter_complex "[0:v][1:v] overlay=192:108:enable='between(t, 0, 5)'[ov0]; [ov0] overlay=192:108:enable='between(t, 5, 10)'" -pix_fmt yuv420p output_overlayed.mp4 

But when I try to cut out sections using this code:

ffmpeg -i /Users/username/projectdir/static/video.mp4 -I overlay.png -i overlay2.png -filter_complex "[0:v][1:v] overlay=192:108:enable='between(t, 0, 5)'[ov0]; [ov0] overlay=192:108:enable='between(t, 5, 10)', select='between(t,0,5)+between(t,10,15)', setpts='N/FRAME_RATE/TB'" -pix_fmt yuv420p output_overlayed_trimmed.mp4 

It seems to cut correctly, so the original video starts playing from 0 seconds until 5 seconds and then plays from 10 seconds in until 15 seconds and cuts out. But after the point where the video cuts out it's just a black screen for the duration of the video. I can't seem to get it to work so it affects the overall duration of the video.

(The values being passed in are just examples by the way, eg. I've got it to start an overlay 5 seconds in but also cut 5 seconds in)

I have the timestamps for when the overlays should appear on the non-trimmed video, so the overlaying should happen first and then the trimming. If the video is trimmed first then the overlays will appear at the wrong times.

An alternative way of achieving this that is currently working is by performing the first line of code (which just produces a new video file with the overlay) and then separately take this new file and perform the trimming independently:

ffmpeg -ss 0 -to 5 -i /Users/username/projectdir/static/output_overlayed.mp4 -ss 15 -to 20 -i /Users/username/projectdir/static/output_overlayed.mp4 -filter_complex "[0][1]concat=n=2:v=1:a=1" output_trimmed.mp4

But this means working with 2 separate files and then having to remove the first after the 2nd execution is complete. Ideally I'd combine them into one command which doesn't produce multiple files.

Would appreciate any help - thanks!

0

1 Answer 1

1

Assuming video.mp4 has an audio stream, the length of the result matches the length of the audio stream.

  • Add aselect and asetpts filters for selecting the relevant part of the audio stream:
    [0:a]aselect='between(t,0,5)+between(t,10,15)',asetpts='N/SAMPLE_RATE/TB'
  • Because there are two streams, we may give the video stream the temporary name [v], give the audio streams the temporary name [a], and apply streams mapping: -map "[v]" -map "[a]".

Updated command:
ffmpeg -y -i video.mp4 -i overlay.png -i overlay2.png -filter_complex "[0:v][1:v]overlay=192:108:enable='between(t,0,5)'[ov0];[ov0][2:v] overlay=192:108:enable='between(t,5,10)',select='between(t,0,5)+between(t,10,15)',setpts='N/FRAME_RATE/TB'[v]; [0:a]aselect='between(t,0,5)+between(t,10,15)',asetpts='N/SAMPLE_RATE/TB'[a]" -shortest -map "[v]" -map "[a]" -pix_fmt yuv420p output_overlayed_trimmed.mp4


Testing:

Build input video and images using FFmpeg:

ffmpeg -y -f lavfi -i testsrc=size=384x216:rate=25 -f lavfi -i sine=frequency=400 -t 30 video.mp4
ffmpeg -y -f lavfi -i mandelbrot=size=192x108 -v:frames 1 overlay.png
ffmpeg -y -f lavfi -i color=blue:size=192x108 -v:frames 1 overlay2.png

Execute the "updated command" with the above inputs.


The total length of the output video is 10 seconds.

First frame:
enter image description here

For very short time we have the blue overlay2.png overlay:
enter image description here

Sample frame at the end:
enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.