@Gyan is correct. You can reverse a video using ffmegs
built in reverse video filter:
ffmpeg -i video.mp4 -vf reverse video_reversed.mp4
However this is incredibly memory intensive. I wont go into details but it has to do with the video is saved in a compressed format. To reverse it, it needs basically load each frame of the video into memory.
Therefor for any reasonably sized video (> 10 minutes) you are going to have a tough time using the above command. Thus to complete @Gyan's answer. Here is how to segment the video, reverse each part, and then concatenate them again.
You can first segment a video into 300
second chunks using:
ffmpeg -i video.mp4 -map 0 -c copy -f segment -segment_time 300 -reset_timestamps 1 video_%03d.mp4
This will output a bunch of video files named video_000.mp4
, video_001.mp4
etc. Next you need to reverse each of these segments. You can either do that manually using the original reverse command. Alternatively for long videos you might want to use a loop. This loop looks for all mp4
files in the current directory and reverses each of them:
for f in *.mp4; do
ffmpeg -i $f -vf reverse ${f/.mp4/_reversed.mp4}
done
or if you need to reverse audio, too
for f in *.mp4; do
ffmpeg -i $f -vf reverse -af areverse ${f/.mp4/_reversed.mp4}
done
NOTE: Make sure you remove the original video video.mp4
from the folder before using this loop (otherwise you are back to the start).
You will now have a set of reversed segments med video_000_reversed.mp4
, video_001_reversed.mp4
etc. The final command concatenates them into a single video. To do that we need a text file with all the files we want to concatenate. You can automatically create this text file using the following:
for f in ./SegmentedVideos/*_reversed.mp4; do
echo file \'$f\' > tmp.txt
cat fileList.txt >> tmp.txt
rm fileList.txt
mv tmp.txt fileList.txt
done
At this point you should have a text file fileList.txt
that contains the video files in reverse order like this:
file './video_XXX_reversed.mp4'
...
file './video_001_reversed.mp4'
file './video_000_reversed.mp4'
Then all we need to do is concatenate everything:
ffmpeg -f concat -safe 0 -i fileList.txt -c copy final_revered_video.mp4