23

I was able to reverse with:

ffmpeg -i input.mp4 -vf reverse output_reversed.mp4

And I can concat with:

ffmpeg -i input.mp4 -i input.mp4 -filter_complex "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4

But can I concat with a reverse version of the video, with a single command?

What I am trying to achieve is a ping pong effect, where the video plays once, then plays backwards right after.

Thanks!

Vitam
  • 333
  • 1
  • 2
  • 6

1 Answers1

34

Technically, you can do it using

ffmpeg -i input.mp4 -filter_complex "[0:v]reverse,fifo[r];[0:v][0:a][r] [0:a]concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4

But the reverse filter will use a lot of memory for large videos. I've added a fifo filter to avoid frame drops. But test and see. (I haven't reversed the audio)

If your clip has no audio, the above command will throw an error – instead, use:

ffmpeg -i input.mp4 -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" output.mp4
JeffThompson
  • 1,538
  • 3
  • 24
  • 47
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • That's perfect, thanks! It will be used for very short videos, with no audio, so that's pretty much all I needed. – Vitam Feb 15 '17 at 19:06
  • 7
    If you're using this on something with no audio stream (I wanted to run this on a gif) you'll get an error, but this works instead: `ffmpeg -i input.mp4 -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" output.mp4` – tobek Sep 27 '17 at 23:32
  • @tobek is it possible to let ffmpeg repeat the operation 2 or more times? To get the video looped back and forth a few times. – SharpAffair Mar 22 '18 at 21:55
  • @SharpAffair probably, but I'm a bit of a hack with ffmpeg and I don't know how to do it. I needed the same, and just repeated the command a few times (changing input and output obviously). You could script this if you needed to do it a bunch, or maybe ask a new question about it. – tobek Mar 23 '18 at 02:19
  • 2
    Added @tobek's non-audio solution to the answer above – JeffThompson Nov 11 '18 at 16:27
  • 1
    Tried to do this on a 200mb video file. It ate all of my 16gb of ram and then froze my computer. Just to add an example of what "a lot of memory" means – Metareven Aug 03 '20 at 13:10