Reverse video in android
Asked Answered
F

2

14

I have recorded a video from camera in my app and saved in device storage.Now I want to reverse the video such that it plays from backwards.i.e. if video is of 10 seconds then the last frame at 10th second will become first frame and it starts playing from there to 1st second first frame.I want to save the reversed video in a file.How should i proceed in that?

Fredericksburg answered 20/10, 2015 at 7:17 Comment(4)
@MohanadKholy its gud article..but i couldn't find way to reverse video in it?Fredericksburg
this is not related to android this is something related to videolibrary which can reverse your videoPegboard
@Pegboard I want to reverse video recorded in my android app and show the reversed video in my android app..is there any library for tht?Fredericksburg
Another option would be OpenCV.Generatrix
F
4

Thanks to Mick for giving me an idea to use ffmpeg for reversing video.

I have posted code at github for reversing video along with performing other video editing operation using ffmpeg and complete tutorial in my blog post here.

As written in my blog post,

For reversing video,first we need to divide video into segments with duration of 10 seconds or less because reverse video command for ffmpeg will not work for long duration videos unless your device has 32 GB of RAM.

Hence,to reverse a video-

1.Divide video into segments with duration of 10 seconds or less.

2.Reverse the segmented videos

3.Concatenate reversed segmented videos in reverse order.

For dividing video into segments with duration of 6 seconds we can use the below command-

String[] complexCommand = {"-i", inputFileAbsolutePath, "-c:v", "libx264", "-crf", "22", "-map", "0", "-segment_time", "6", "-g", "9", "-sc_threshold", "0", "-force_key_frames", "expr:gte(t,n_forced*6)", "-f", "segment", outputFileAbsolutePath};

Here,

-c:v libx264

encodes all video streams with libx264

-crf

Set the quality for constant quality mode.

-segment_time

time for each segment of video

-g

GOP size

-sc_threshold

set scene change threshold.

-force_key_frames expr:gte(t,n_forced*n)

Forcing a keyframe every n seconds

After segmenting video,we need to reverse the segmented videos.For that we need to run a loop where each segmented video file will be reversed.

To reverse a video with audio(without removing its audio) we can use the below command-

String command[] = {"-i", inputFileAbsolutePath, "-vf", "reverse", "-af", "areverse", outputFileAbsolutePath};

To reverse a video with audio removing its audio we can use the below command-

String command[] = {"-i", inputFileAbsolutePath, "-an", "-vf", "reverse", outputFileAbsolutePath};

To reverse a video without audio we can use the below command-

String command[] = {"-i",inputFileAbsolutePath, "-vf", "reverse", outputFileAbsolutePath};

After reversing segmented videos,we need to concatenate reversed segmented videos in reverse order.For that we sort videos on the basis of last modified file using Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE).

Then, to concatenate reversed segmented videos(with audio) we can use the below command-

String command[] = {"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath .....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:v0] [0:a0] [1:v1] [1:a1]...[N:vN] concat=n=N:v=1:a=1 [v] [a],"-map","[v]","-map","[a]", outputFileAbsolutePath};

To concatenate reversed segmented videos(without audio) we can use the below command-

String command[] = {"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath .....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:0] [1:0] [2:0]...[N:0] concat=n=N:v=1:a=0",outputFileAbsolutePath};

Here,

-filter_complex [0:v0] [0:a0] [1:v1] [1:a1]…[N:vN] tells ffmpeg what streams to send to the concat filter.In the above case, video stream 0 [0:v0] and audio stream 0 [0:a0] from input 0,video stream 1 [1:v1] and audio stream 1 [1:v1] from input 1 and so on.

concat filter is used to concatenate audio and video streams, joining them together one after the other.The filter accepts the following options:

n

Set the number of segments. Default is 2.

v

Set the number of output video streams, that is also the number of video streams in each segment. Default is 1.

a

Set the number of output audio streams, that is also the number of audio streams in each segment. Default is 0.

Fredericksburg answered 17/6, 2017 at 13:8 Comment(3)
How is the performance of this solution if I might ask. Because last time I tinker with ffmpeg on android, the performance is not great. It would take a good 50 seconds to manipulate a 15 seconds video, even with all the optimization I can find.Hayrick
@Hayrick Process of reversing video does takes some time.FFmpeg provides certain presets which are collection of options that provide certain speed to the process. Utrafast preset is specially useful to speed up the process.To use preset just add “-preset”, “ultrafast” to the command.Please check out here for detailed explanation on choosing a preset.Also it is recommend to use the slowest preset that you have patience for.Fredericksburg
@BhuvneshVarma Can i get link to the android app?Cemetery
K
6

If you are prepared to use ffmpeg you can use this approach - it essentially breaks the video into frames and then builds it again in reverse order:

There are several ways to use ffmpeg in Android but the 'wrapper' approach is one which I have found a reasonable blend of performance and ease of use. Some example Android ffmpeg wrapper:

It's worth being aware that this will be time-consuming on a Mobile - if you have the luxury of being able to upload to a server and doing the reversal there it might be quicker.

Kaleb answered 20/10, 2015 at 7:47 Comment(6)
Previously I have used this dependency compile 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5' for scrubbing video..should i proceed with this dependency for reversing video?Fredericksburg
In theory with the wrapper approach, if one command works then any command should work, but it is worth remembering that ffmpeg was not actually designed to be wrapped like this so I don't think you can be 100% sure without testing. If it worked for you for scrubbing I would start there anyway as there is a very good probability it will work for this also. One thing to be aware of - if you need the audio reversed also (for some reason...) then the above approach uses 'sox' which is a separate utility. The guardian link above includes sox although I have not used it personally.Kaleb
thanks..How can i integrate sox utility in my android project?Fredericksburg
Take a look at the 'combine' function in: github.com/guardianproject/android-ffmpeg-java/blob/master/src/…Kaleb
@AndroidLearner :- have you find the code of reverse video functionality in android? i want too..Sunless
@MayankSugandhi Yes,check out my answer and complete code at repositoryFredericksburg
F
4

Thanks to Mick for giving me an idea to use ffmpeg for reversing video.

I have posted code at github for reversing video along with performing other video editing operation using ffmpeg and complete tutorial in my blog post here.

As written in my blog post,

For reversing video,first we need to divide video into segments with duration of 10 seconds or less because reverse video command for ffmpeg will not work for long duration videos unless your device has 32 GB of RAM.

Hence,to reverse a video-

1.Divide video into segments with duration of 10 seconds or less.

2.Reverse the segmented videos

3.Concatenate reversed segmented videos in reverse order.

For dividing video into segments with duration of 6 seconds we can use the below command-

String[] complexCommand = {"-i", inputFileAbsolutePath, "-c:v", "libx264", "-crf", "22", "-map", "0", "-segment_time", "6", "-g", "9", "-sc_threshold", "0", "-force_key_frames", "expr:gte(t,n_forced*6)", "-f", "segment", outputFileAbsolutePath};

Here,

-c:v libx264

encodes all video streams with libx264

-crf

Set the quality for constant quality mode.

-segment_time

time for each segment of video

-g

GOP size

-sc_threshold

set scene change threshold.

-force_key_frames expr:gte(t,n_forced*n)

Forcing a keyframe every n seconds

After segmenting video,we need to reverse the segmented videos.For that we need to run a loop where each segmented video file will be reversed.

To reverse a video with audio(without removing its audio) we can use the below command-

String command[] = {"-i", inputFileAbsolutePath, "-vf", "reverse", "-af", "areverse", outputFileAbsolutePath};

To reverse a video with audio removing its audio we can use the below command-

String command[] = {"-i", inputFileAbsolutePath, "-an", "-vf", "reverse", outputFileAbsolutePath};

To reverse a video without audio we can use the below command-

String command[] = {"-i",inputFileAbsolutePath, "-vf", "reverse", outputFileAbsolutePath};

After reversing segmented videos,we need to concatenate reversed segmented videos in reverse order.For that we sort videos on the basis of last modified file using Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE).

Then, to concatenate reversed segmented videos(with audio) we can use the below command-

String command[] = {"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath .....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:v0] [0:a0] [1:v1] [1:a1]...[N:vN] concat=n=N:v=1:a=1 [v] [a],"-map","[v]","-map","[a]", outputFileAbsolutePath};

To concatenate reversed segmented videos(without audio) we can use the below command-

String command[] = {"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath .....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:0] [1:0] [2:0]...[N:0] concat=n=N:v=1:a=0",outputFileAbsolutePath};

Here,

-filter_complex [0:v0] [0:a0] [1:v1] [1:a1]…[N:vN] tells ffmpeg what streams to send to the concat filter.In the above case, video stream 0 [0:v0] and audio stream 0 [0:a0] from input 0,video stream 1 [1:v1] and audio stream 1 [1:v1] from input 1 and so on.

concat filter is used to concatenate audio and video streams, joining them together one after the other.The filter accepts the following options:

n

Set the number of segments. Default is 2.

v

Set the number of output video streams, that is also the number of video streams in each segment. Default is 1.

a

Set the number of output audio streams, that is also the number of audio streams in each segment. Default is 0.

Fredericksburg answered 17/6, 2017 at 13:8 Comment(3)
How is the performance of this solution if I might ask. Because last time I tinker with ffmpeg on android, the performance is not great. It would take a good 50 seconds to manipulate a 15 seconds video, even with all the optimization I can find.Hayrick
@Hayrick Process of reversing video does takes some time.FFmpeg provides certain presets which are collection of options that provide certain speed to the process. Utrafast preset is specially useful to speed up the process.To use preset just add “-preset”, “ultrafast” to the command.Please check out here for detailed explanation on choosing a preset.Also it is recommend to use the slowest preset that you have patience for.Fredericksburg
@BhuvneshVarma Can i get link to the android app?Cemetery

© 2022 - 2024 — McMap. All rights reserved.