39

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

0

2 Answers 2

65

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!

0
11

@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, or you can use the loop provided below.

NOTE: Make sure you remove the original video video.mp4 from the folder before using the loop below (otherwise you are back to the start).

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:

To reverse only the video but not the audio:

for f in *.mp4; do
    ffmpeg -i $f -vf reverse ${f/.mp4/_reversed.mp4}
done

To reverse the video and audio:

for f in *.mp4; do
    ffmpeg -i $f -vf reverse -af areverse ${f/.mp4/_reversed.mp4}
done

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
  • I tried this, got as far as the last step, but at that point got the moov atom not found error.
    – Lori
    May 5 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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