I have a short 3:00 min clip that is only flipped left to right for about :30 sec in the middle.

So if the whole thing were flipped left to right, I would just want to apply:

ffmpeg -i 3min.mp4 -vf hflip flipped_3min.mp4

But since it's only partly flipped, is there a way, in a single ffmpeg command, to apply a time segment for that hflip filter (1:30-2:00)?

I am looking for some documentation on this and can't find any thing of the sort, am wondering if it's just not possible in FFmpeg or not.


How do I flip only a subsection of a video using ffmpeg?

Here, -filter_complex is your friend, as it can create chains of filtered input. We will still be using the hflip filter mentioned in the question in the filtergraph.

You can apply to a subsection like so:

ffmpeg -i input.mp4 -filter_complex "[0:v]trim=start=0:duration=90[a];\
[d][e]concat[out1]" -map [out1] flip.mp4

One line for easy copy + pasting:

ffmpeg  -i input.mp4 -filter_complex "[0:v]trim=start=0:duration=90[a];[0:v]trim=start=90:duration=30,setpts=PTS-STARTPTS[b];[b]hflip[c];[a][c]concat[d];[0:v]trim=start=120:duration=60,setpts=PTS-STARTPTS[e];[d][e]concat[out1]" -map [out1] flip.mp4


Using a filtergraph is a bit like creating a chain of actions, and there is a nice visual explanation1 of this in the 'Filtering Introduction' section of the filters documentation. Filters work with inputs - that's the bit you see before the filter, eg [0:v] (video of first input stream) or [a] (a created output named a) - and outputs. That is to say, an input or inputs are transformed according to a filter and then output either as an output or outputs.

We have a simple set of actions here, and if we go through those in sequence we can build up a filtergraph of inputs and outputs.


Create 3 segments of video (from input [0:v]) using trim:

  • First segment (90 seconds), do nothing to (output as [a])
  • Second segment (30 seconds), hflip and setpts to avoid duration issues (output trim as [b] and pass that to hflip to output as [c])

These two are then concated together to make the output [a]+[c] -> [d] (duration: 120 s)

  • Third segment (60 seconds), set PTS again (output as [e])

Join segments to make output [d] + [e] -> [out1], and -map that to final output in file.

That gives you a video of the same duration as the original (180 s), but with a section of 30 seconds in the middle which are horizontally flipped2.


Visual representation from ffmpeg filters documentation

In libavfilter, a filter can have multiple inputs and multiple outputs. To illustrate the sorts of things that are possible, we consider the following filtergraph.

input --> split ---------------------> overlay --> output
            |                             ^
            |[tmp]                  [flip]|
            +-----> crop --> vflip -------+

This filtergraph splits the input stream in two streams, then sends one stream through the crop filter and the vflip filter, before merging it back with the other stream by overlaying it on top.

2 Tested using ffmpeg version N-72939-g5b0f55a


Horizontal flipping occurs between 2-3 seconds in this example.
Horizontal flipping occurs between 2-3 seconds in this example.

Some filters have timeline support via the enable option.

You can see a list of filters and if they have timeline support with ffmpeg -filters. A "T" preceding the filter name indicates that it supports timeline editing. If your desired filter does not have timeline support then update your ffmpeg as filters sometimes get updated to support this. Otherwise, use the trim filters as explained in the other answer.

ffmpeg example:

ffmpeg -i input -vf "hflip=enable='between(t,90,120)'" -c:a copy output

ffplay example:

ffplay -vf "hflip=enable='between(t,90,120)'" input
  • Moral of the story: always check the documentation first to see if the filter comes with options. No-one wants to deal with complex filters unnecessarily. – Hashim Aziz Apr 14 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.