Here is the solution using FFMPEG library use below function to trim or cut the video, may this will work for you:
private void executeCutVideoCommand(int startMs, int endMs) {
File moviesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES
);
String filePrefix = "cut_video";
String fileExtn = ".mp4";
String yourRealPath = getPath(VideoEffectActivity.this, selectedVideoUri);
File dest = new File(moviesDir, filePrefix + fileExtn);
int fileNo = 0;
while (dest.exists()) {
fileNo++;
dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
}
Log.d(TAG, "startTrim: src: " + yourRealPath);
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
Log.d(TAG, "startTrim: startMs: " + startMs);
Log.d(TAG, "startTrim: endMs: " + endMs);
filePath = dest.getAbsolutePath();
//String[] complexCommand = {"-i", yourRealPath, "-ss", "" + startMs / 1000, "-t", "" + endMs / 1000, dest.getAbsolutePath()};
String[] complexCommand = {"-ss", "" + startMs / 1000, "-y", "-i", yourRealPath, "-t", "" + (endMs - startMs) / 1000, "-vcodec", "mpeg4", "-b:v", "2097152", "-b:a", "48000", "-ac", "2", "-ar", "22050", filePath};
execFFmpegBinary(complexCommand);
}
private void execFFmpegBinary(final String[] command) {
try {
ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
@Override
public void onFailure(String s) {
Log.d(TAG, "FAILED with output : " + s);
}
@Override
public void onSuccess(String s) {
Log.d(TAG, "SUCCESS with output : " + s);
//You have to create a class of Preview Activity
//If you don't have please remove below Intent code
Intent intent = new Intent(VideoEffectActivity.this, PreviewActivity.class);
intent.putExtra(FILEPATH, filePath);
startActivity(intent);
}
@Override
public void onProgress(String s) {
progressDialog.setMessage("progress : " + s);
Log.d(TAG, "progress : " + s);
}
@Override
public void onStart() {
Log.d(TAG, "Started command : ffmpeg " + command);
progressDialog.setMessage("Processing...");
progressDialog.show();
}
@Override
public void onFinish() {
Log.d(TAG, "Finished command : ffmpeg " + command);
progressDialog.dismiss();
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
// do nothing for now
}
}
Put this dependency into gradle file:
compile 'com.writingminds:FFmpegAndroid:0.3.2'