32

i am use to FFmpeg library in android for reverse video. I can dump images from video but how to reverse all images and make video. See this link that from i am use to library in my gradle.. Library is compile 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5' i can use some of command and some are not. i use command as like below

-i /storage/emulated/0/ffvid/frameCount.mp4 -an -qscale 1 /storage/emulated/0/ffimg/revi%06d.jpg

above command use for dump images from video. i can't use command as liki $ffmpeg , $sox and $cat

i have referred many link but not success in see below some links...

https://unix.stackexchange.com/questions/214327/how-can-i-reverse-a-video-clip

http://jiaxihu.blogspot.in/2012/05/reverse-video-clip-with-ffmpeg.html

So please help me and give me solution. if other library available then suggest me. thanks advance

1
  • 3
    It is discouraged crosspost the same question to multiple Stack Exchange sites.
    – llogan
    Feb 22, 2016 at 17:20

2 Answers 2

50

FFmpeg has a reverse video filter and reverse audio filter.

For video only:

ffmpeg -i /storage/emulated/0/ffvid/frameCount.mp4 -vf reverse reversed.mp4

For audio and video:

ffmpeg -i /storage/emulated/0/ffvid/frameCount.mp4 -vf reverse -af areverse reversed.mp4

This filter buffers the entire clip. For larger files, segment the file, reverse each segment and then concat the reversed segments. Remember that you have to concat the reversed segments in the reverse order!

11
  • No such filter: 'reverse' getting error Feb 24, 2016 at 9:19
  • 4
    This method consumes too much memory. How can I segment the file?
    – bukkojot
    Apr 20, 2018 at 20:10
  • 1
    @Gyan please add the code for segment muxer code. May 27, 2019 at 10:08
  • 1
    I am surprised this cannot be done without encoding.
    – bonobo
    Dec 31, 2020 at 12:47
  • 3
    @bonobo Video encoding is usually asymmetric / unidirectional. To reduce size, it describes how to transition from frame 1 to frame 2, but not the other way around. Feb 25, 2021 at 9:06
2

@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

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
1
  • This is the best solution for large files!
    – Anduin
    Jun 17 at 13:27

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.