Android sdk 剪切/修剪视频文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11205299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:19:53  来源:igfitidea点击:

Android sdk cut/trim video file

androidvideomediatrim

提问by Catalin

Is there any way to cut a video (mp4 or 3gp) on android, like use only the last 5 seconds of the movie... on iphone this is possible using the AVAssetExportSession but on android I haven't found anything similar, just maybe some references to ffmpeg library which seems complicated. Is there any easier way to do it?

有什么方法可以在 android 上剪切视频(mp4 或 3gp),比如只使用电影的最后 5 秒...一些对 ffmpeg 库的引用,这看起来很复杂。有没有更简单的方法来做到这一点?

采纳答案by Sebastian Annies

You can do this with my mp4parserlibrary. Have a look at the ShortenExampleit does exactly what the name suggests. Since the library cannot re-encode the video it can only cut the video at I-frames. So the points in time where you can make a cut are quite coarse.

你可以用我的mp4parser库来做到这一点。看一看ShortenExample它完全符合名称的含义。由于库无法重新编码视频,因此只能以 I 帧剪切视频。因此,您可以进行切割的时间点非常粗糙。

On Android 4.1 you can access the hardware codecs via MediaCodec API which could be an option (but I haven't seen any example of that yet)

在 Android 4.1 上,您可以通过 MediaCodec API 访问硬件编解码器,这可能是一个选项(但我还没有看到任何示例)

回答by Android Developer

We can cut video using ffmpeg in Android.

我们可以在 Android 中使用 ffmpeg 剪切视频。

For integrating FFmpeg in android we can use precompiled libraries like ffmpeg-android.

为了在 android 中集成 FFmpeg,我们可以使用预编译库,如ffmpeg-android

To cut a video we can use below command-

要剪切视频,我们可以使用以下命令-

String[] complexCommand = {"-ss", "" + startMs / 1000, "-y", "-i", inputFileAbsolutePath, "-t", "" + (endMs - startMs) / 1000, "-s", "320x240", "-r", "15", "-vcodec", "mpeg4", "-b:v", "2097152", "-b:a", "48000", "-ac", "2", "-ar", "22050", outputFileAbsolutePath};

Here,

这里,

-ss

-ss

seeks to position

寻求定位

-y

-y

Overwrite output files without asking.

无需询问即可覆盖输出文件。

-i

-一世

ffmpeg reads from an arbitrary number of input “files” specified by the -i option

ffmpeg 从 -i 选项指定的任意数量的输入“文件”中读取

-t

-t

limit the duration of data read from the input file

限制从输入文件读取数据的持续时间

-s

-s

video output size

视频输出尺寸

-r

-r

Set frame rate

设置帧率

-vcodec

-vcodec

Set the video codec.

设置视频编解码器。

-b:v

-b:v

Set the video bitrate

设置视频比特率

-b:a

-b:a

Set the audio bitrate

设置音频比特率

-ac

-ac

Set the number of audio channels.

设置音频通道数。

-ar

-ar

sets the sampling rate for audio streams if encoded

如果已编码,则设置音频流的采样率

startMs

开始时间

start time of video in milliseconds from where you want to cut

以毫秒为单位的视频开始时间,从您要剪切的位置开始

endMs

结束时间

end time of video in milliseconds up to which you want to cut

您要剪切的视频结束时间(以毫秒为单位)

I have created a sample android project on editing videos using FFMpeg which includes cutting video.Check it out-

我创建了一个使用 FFMpeg 编辑视频的示例 android 项目,其中包括剪切视频。检查一下-

https://github.com/bhuvnesh123/FFmpeg-Video-Editor-Android

https://github.com/bhuvnesh123/FFmpeg-Video-Editor-Android

and its tutorial at-

及其教程在-

https://androidlearnersite.wordpress.com/2017/03/17/ffmpeg-video-editor/

https://androidlearnersite.wordpress.com/2017/03/17/ffmpeg-video-editor/

回答by Shijil

try this

尝试这个

Intent trimVideoIntent = new Intent("com.android.camera.action.TRIM");

// The key for the extra has been discovered from com.android.gallery3d.app.PhotoPage.KEY_MEDIA_ITEM_PATH
trimVideoIntent.putExtra("media-item-path",FilePath);
trimVideoIntent.setData(videoUri);

// Check if the device can handle the Intent
List<ResolveInfo> list = getPackageManager().queryIntentActivities(trimVideoIntent, 0);
if (null != list && list.size() > 0) {
    startActivity(trimVideoIntent); // Fires TrimVideo activity into being active
}else {
    Toast.makeText(this, "not supported",Toast.LENGTH_SHORT).show();
}

its work on Gallery2 package installed devices

它在 Gallery2 包安装的设备上的工作

回答by Marlon

You can try INDE Media for Mobile - https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials

您可以尝试移动版 INDE Media - https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials

It has transcoding\remuxing functionality as MediaComposer class and a possibility to select segments for resulted files. Since it uses MediaCodec API inside it is very battery friendly and works as fast as possible

它具有作为 MediaComposer 类的转码\重新混合功能,并且可以为结果文件选择片段。由于它在内部使用 MediaCodec API,因此对电池非常友好,并且运行速度尽可能快

enter image description here

在此处输入图片说明