Android sdk cut/trim video file

2019-01-07 11:19发布

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?

4条回答
狗以群分
2楼-- · 2019-01-07 11:22

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

查看更多
趁早两清
3楼-- · 2019-01-07 11:37

We can cut video using ffmpeg in Android.

For integrating FFmpeg in android we can use precompiled libraries like 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

seeks to position

-y

Overwrite output files without asking.

-i

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

-t

limit the duration of data read from the input file

-s

video output size

-r

Set frame rate

-vcodec

Set the video codec.

-b:v

Set the video bitrate

-b:a

Set the audio bitrate

-ac

Set the number of audio channels.

-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-

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

and its tutorial at-

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

查看更多
冷血范
4楼-- · 2019-01-07 11:40

You can try INDE Media for Mobile - 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

enter image description here

查看更多
我只想做你的唯一
5楼-- · 2019-01-07 11:42

You can do this with my mp4parser library. Have a look at the ShortenExample it 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.

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)

查看更多
登录 后发表回答