Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sdk cut/trim video file

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?

like image 958
Catalin Avatar asked Jun 26 '12 10:06

Catalin


People also ask

How do you cut out parts of a video app?

AndroVid – Video Editor (Beta) You can zoom the slider for big videos that makes it easy to cut them. This app also lets you delete some parts from the middle of the video without affecting the full clip. You can also extract MP3, create GIF, add music, crop video and add text using this app.


2 Answers

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)

like image 53
Sebastian Annies Avatar answered Oct 12 '22 01:10

Sebastian Annies


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 with re-encoding ,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};

Below mentioned that which command is work for what.

-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

To cut a video without re-encoding ,we can use below command-

String[] complexCommand = { "-y", "-i", inputFileAbsolutePath,"-ss", "" + startMs / 1000, "-t", "" + (endMs - startMs) / 1000, "-c","copy", outputFileAbsolutePath};

Here,

"-c", "copy" copies the video, audio and bit stream from the input to the output file without re-encoding them.

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/

like image 36
Android Developer Avatar answered Oct 12 '22 03:10

Android Developer