在 android 中反转视频
Reverse video in android
我在我的应用程序中录制了来自相机的视频并保存在设备中 storage.Now 我想反转视频以便它从 backwards.i.e 播放。如果视频是 10 秒,那么第 10 秒的最后一帧将成为第一帧,它首先从那里开始播放到第一秒 frame.I 想要将反转的视频保存在 file.How 中,我应该继续吗?
如果您准备使用 ffmpeg,您可以使用这种方法 - 它基本上将视频分成帧,然后以相反的顺序再次构建它:
在 Android 中有多种使用 ffmpeg 的方法,但我发现 'wrapper' 方法是性能和易用性的合理结合。一些示例 Android ffmpeg 包装器:
- http://hiteshsondhi88.github.io/ffmpeg-android-java/
- https://github.com/guardianproject/android-ffmpeg
值得注意的是,这在移动设备上会很耗时 - 如果您有能力上传到服务器并在那里进行反转,则可能会更快。
感谢 Mick 给了我使用 ffmpeg 倒车视频的想法。
我在 github for reversing video along with performing other video editing operation using ffmpeg and complete tutorial in my blog post here 处 post 编辑了代码。
如我的博客所写 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.
我在我的应用程序中录制了来自相机的视频并保存在设备中 storage.Now 我想反转视频以便它从 backwards.i.e 播放。如果视频是 10 秒,那么第 10 秒的最后一帧将成为第一帧,它首先从那里开始播放到第一秒 frame.I 想要将反转的视频保存在 file.How 中,我应该继续吗?
如果您准备使用 ffmpeg,您可以使用这种方法 - 它基本上将视频分成帧,然后以相反的顺序再次构建它:
在 Android 中有多种使用 ffmpeg 的方法,但我发现 'wrapper' 方法是性能和易用性的合理结合。一些示例 Android ffmpeg 包装器:
- http://hiteshsondhi88.github.io/ffmpeg-android-java/
- https://github.com/guardianproject/android-ffmpeg
值得注意的是,这在移动设备上会很耗时 - 如果您有能力上传到服务器并在那里进行反转,则可能会更快。
感谢 Mick 给了我使用 ffmpeg 倒车视频的想法。
我在 github for reversing video along with performing other video editing operation using ffmpeg and complete tutorial in my blog post here 处 post 编辑了代码。
如我的博客所写 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.