7

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.

11

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];\
[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

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

Explanation:

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.

Segments

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.


1:

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.

                [main]
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

6

enter image description here
Horizontal flipping occurs between 2-3 seconds in this example.

You can use the geq filter with the enable option ("timeline editing"):

ffmpeg example:

ffmpeg -i input -vf "geq='p(W-X,Y)':enable='between(t,90,120)'" -c:a copy output

ffplay example:

ffplay -vf "geq='p(W-X,Y)':enable='between(t,90,120)'" input

Notes:

  • hflip currently does not support timeline editing, so that is why geq was used in this example.

  • You can see what filters do support timeline editing with ffmpeg -filters. A "T" preceding the filter name indicates that it supports timeline editing.

  • This example will stream copy the audio assuming you want to simply re-mux instead of re-encode.

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.