Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowtham committed Sep 5, 2020
1 parent 40038d6 commit 108b78c
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 101 deletions.
70 changes: 32 additions & 38 deletions app/src/main/java/com/gowtham/videotrimmer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
import android.view.View;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.cocosw.bottomsheet.BottomSheet;
import com.gowtham.library.ui.ActVideoTrimmer;
import com.gowtham.library.utils.LogMessage;
import com.gowtham.library.utils.TrimmerConstants;
import com.gowtham.library.utils.TrimType;
import com.gowtham.library.utils.TrimVideo;

import java.io.File;

Expand Down Expand Up @@ -63,8 +61,8 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == TrimmerConstants.REQ_CODE_VIDEO_TRIMMER && data != null) {
Uri uri = Uri.parse(data.getStringExtra(TrimmerConstants.TRIMMED_VIDEO_PATH));
if (requestCode == TrimVideo.VIDEO_TRIMMER_REQ_CODE && data != null) {
Uri uri = Uri.parse(TrimVideo.getTrimmedVideoPath(data));
Log.d(TAG, "Trimmed path:: " + uri);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
Expand Down Expand Up @@ -94,29 +92,25 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten

private void openTrimActivity(String data) {
if (trimType == 0) {
Intent intent = new Intent(this, ActVideoTrimmer.class);
intent.putExtra(TrimmerConstants.TRIM_VIDEO_URI, data);
intent.putExtra(TrimmerConstants.DESTINATION, "/storage/emulated/0/DCIM/TESTFOLDER");
startActivityForResult(intent, TrimmerConstants.REQ_CODE_VIDEO_TRIMMER);
TrimVideo.activity(data)
.setDestination("/storage/emulated/0/DCIM/TESTFOLDER")
.setAccurateCut(true)
.start(this);
} else if (trimType == 1) {
Intent intent = new Intent(this, ActVideoTrimmer.class);
intent.putExtra(TrimmerConstants.TRIM_VIDEO_URI, data);
intent.putExtra(TrimmerConstants.TRIM_TYPE, 1);
intent.putExtra(TrimmerConstants.FIXED_GAP_DURATION, getEdtValueLong(edtFixedGap));
startActivityForResult(intent, TrimmerConstants.REQ_CODE_VIDEO_TRIMMER);
TrimVideo.activity(data)
.setTrimType(TrimType.FIXED_DURATION)
.setFixedDuration(getEdtValueLong(edtFixedGap))
.start(this);
} else if (trimType == 2) {
Intent intent = new Intent(this, ActVideoTrimmer.class);
intent.putExtra(TrimmerConstants.TRIM_VIDEO_URI, data);
intent.putExtra(TrimmerConstants.TRIM_TYPE, 2);
intent.putExtra(TrimmerConstants.MIN_GAP_DURATION, getEdtValueLong(edtMinGap));
startActivityForResult(intent, TrimmerConstants.REQ_CODE_VIDEO_TRIMMER);
TrimVideo.activity(data)
.setTrimType(TrimType.MIN_DURATION)
.setMinDuration(getEdtValueLong(edtMinGap))
.start(this);
} else {
Intent intent = new Intent(this, ActVideoTrimmer.class);
intent.putExtra(TrimmerConstants.TRIM_VIDEO_URI, data);
intent.putExtra(TrimmerConstants.TRIM_TYPE, 3);
intent.putExtra(TrimmerConstants.MIN_FROM_DURATION, getEdtValueLong(edtMinFrom));
intent.putExtra(TrimmerConstants.MAX_TO_DURATION, getEdtValueLong(edtMAxTo));
startActivityForResult(intent, TrimmerConstants.REQ_CODE_VIDEO_TRIMMER);
TrimVideo.activity(data)
.setTrimType(TrimType.MIN_MAX_DURATION)
.setMinToMax(getEdtValueLong(edtMinFrom), getEdtValueLong(edtMAxTo))
.start(this);
}
}

Expand All @@ -139,39 +133,39 @@ public void onClick(View v) {
}

private void onDefaultTrimClicked() {
trimType=0;
trimType = 0;
if (checkCamStoragePer())
showVideoOptions();
}

private void onFixedTrimClicked() {
trimType=1;
trimType = 1;
if (isEdtTxtEmpty(edtFixedGap))
Toast.makeText(this,"Enter fixed gap duration",Toast.LENGTH_SHORT).show();
else if(checkCamStoragePer())
Toast.makeText(this, "Enter fixed gap duration", Toast.LENGTH_SHORT).show();
else if (checkCamStoragePer())
showVideoOptions();
}

private void onMinGapTrimClicked() {
trimType=2;
trimType = 2;
if (isEdtTxtEmpty(edtMinGap))
Toast.makeText(this,"Enter min gap duration",Toast.LENGTH_SHORT).show();
else if(checkCamStoragePer())
Toast.makeText(this, "Enter min gap duration", Toast.LENGTH_SHORT).show();
else if (checkCamStoragePer())
showVideoOptions();
}


private void onMinToMaxTrimClicked() {
trimType=3;
trimType = 3;
if (isEdtTxtEmpty(edtMinFrom))
Toast.makeText(this,"Enter min gap duration",Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Enter min gap duration", Toast.LENGTH_SHORT).show();
else if (isEdtTxtEmpty(edtMAxTo))
Toast.makeText(this,"Enter max gap duration",Toast.LENGTH_SHORT).show();
else if(checkCamStoragePer())
Toast.makeText(this, "Enter max gap duration", Toast.LENGTH_SHORT).show();
else if (checkCamStoragePer())
showVideoOptions();
}

public void showVideoOptions() {
public void showVideoOptions() {
try {
BottomSheet.Builder builder = getBottomSheet();
builder.sheet(R.menu.menu_video);
Expand Down
98 changes: 47 additions & 51 deletions library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
Expand Down Expand Up @@ -48,7 +47,8 @@
import com.gowtham.library.utils.CustomProgressView;
import com.gowtham.library.utils.FileUtils;
import com.gowtham.library.utils.LogMessage;
import com.gowtham.library.utils.TrimmerConstants;
import com.gowtham.library.utils.TrimVideo;
import com.gowtham.library.utils.TrimVideoOptions;
import com.gowtham.library.utils.TrimmerUtils;

import java.io.File;
Expand Down Expand Up @@ -97,9 +97,7 @@ public class ActVideoTrimmer extends AppCompatActivity {

private long fixedGap, minGap, minFromGap, maxToGap;

private boolean hidePlayerSeek;

private int frameRate, bitRate;
private boolean hidePlayerSeek, isAccurateCut;

private CustomProgressView progressView;

Expand Down Expand Up @@ -135,13 +133,8 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) {
imageFour, imageFive, imageSix, imageSeven, imageEight};
seekHandler = new Handler();
initPlayer();

String uriStr = getIntent().getStringExtra(TrimmerConstants.TRIM_VIDEO_URI);
if (uriStr == null)
throw new NullPointerException("VideoUri can't be null");
else if (checkStoragePermission())
if (checkStoragePermission())
setDataInView();

}

private void setUpToolBar(ActionBar actionBar, String title) {
Expand Down Expand Up @@ -170,55 +163,49 @@ private void initPlayer() {

private void setDataInView() {
try {
uri = Uri.parse(getIntent().getStringExtra(TrimmerConstants.TRIM_VIDEO_URI));
uri = Uri.parse(getIntent().getStringExtra(TrimVideo.TRIM_VIDEO_URI));
uri = Uri.parse(FileUtils.getPath(this, uri));
LogMessage.v("VideoUri:: " + uri);
totalDuration = TrimmerUtils.getDuration(this, uri);
frameRate = TrimmerUtils.getFrameRate(this, uri);
bitRate = TrimmerUtils.getBitRate(this, uri);
trimType = getIntent().getIntExtra(TrimmerConstants.TRIM_TYPE, 0);
fixedGap = getIntent().getLongExtra(TrimmerConstants.FIXED_GAP_DURATION, totalDuration);
minGap = getIntent().getLongExtra(TrimmerConstants.MIN_GAP_DURATION, totalDuration);
minFromGap = getIntent().getLongExtra(TrimmerConstants.MIN_FROM_DURATION, totalDuration);
maxToGap = getIntent().getLongExtra(TrimmerConstants.MAX_TO_DURATION, totalDuration);
destinationPath = getIntent().getStringExtra(TrimmerConstants.DESTINATION);
hidePlayerSeek = getIntent().getBooleanExtra(TrimmerConstants.HIDE_PLAYER_SEEKBAR, false);
validate();
imagePlayPause.setOnClickListener(v ->
onVideoClicked());
playerView.getVideoSurfaceView().setOnClickListener(v ->
onVideoClicked());
validate();
} catch (Exception e) {
e.printStackTrace();
}
}

private void validate() {
if (trimType > 3 || trimType < 0)
throw new IllegalArgumentException("Invalid trim type" + " " + trimType);
else if (fixedGap <= 0)
throw new IllegalArgumentException("Invalid fixedgap duration" + " " + fixedGap);
else if (minGap <= 0)
throw new IllegalArgumentException("Invalid mingap duration" + " " + minGap);
else if (minFromGap <= 0)
throw new IllegalArgumentException("Invalid minFromGap duration" + " " + minFromGap);
else if (maxToGap <= 0)
throw new IllegalArgumentException("Invalid maxToGap duration" + " " + maxToGap);
else if (trimType == 3) {
if (minFromGap == maxToGap)
throw new IllegalArgumentException("Min_from_duration and Max_to_duration are same..you could use Fixed gap");
else if (minFromGap > maxToGap)
throw new IllegalArgumentException("Min_from_duration must be smaller than Max_to_duration" + " " +
TrimmerConstants.MIN_FROM_DURATION + ":" + minFromGap + " " +
TrimmerConstants.MAX_TO_DURATION + ":" + maxToGap);
} else if (destinationPath != null) {
File outputDir = new File(destinationPath);
outputDir.mkdirs();
destinationPath = String.valueOf(outputDir);
if (!outputDir.isDirectory())
throw new IllegalArgumentException("Destination file path error" + " " + destinationPath);
try {
TrimVideoOptions trimVideoOptions = getIntent().getParcelableExtra(TrimVideo.TRIM_VIDEO_OPTION);
assert trimVideoOptions != null;
trimType = TrimmerUtils.getTrimType(trimVideoOptions.trimType);
destinationPath = trimVideoOptions.destination;
hidePlayerSeek = trimVideoOptions.hideSeekBar;
isAccurateCut = trimVideoOptions.accurateCut;
fixedGap = trimVideoOptions.fixedDuration;
fixedGap = fixedGap != 0 ? fixedGap : totalDuration;
minGap = trimVideoOptions.minDuration;
minGap = minGap != 0 ? minGap : totalDuration;
if (trimType == 3) {
minFromGap = trimVideoOptions.minToMax[0];
maxToGap = trimVideoOptions.minToMax[1];
minFromGap = minFromGap != 0 ? minFromGap : totalDuration;
maxToGap = maxToGap != 0 ? maxToGap : totalDuration;
}
if (destinationPath != null) {
File outputDir = new File(destinationPath);
outputDir.mkdirs();
destinationPath = String.valueOf(outputDir);
if (!outputDir.isDirectory())
throw new IllegalArgumentException("Destination file path error" + " " + destinationPath);
}
buildMediaSource(uri);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
buildMediaSource(uri);
}

private void onVideoClicked() {
Expand Down Expand Up @@ -452,16 +439,24 @@ private void validateVideo() {
LogMessage.v("outputPath::" + outputPath);
videoPlayer.setPlayWhenReady(false);
progressView.show();
String[] complexCommand = new String[]{"ffmpeg", "-ss", TrimmerUtils.formatCSeconds(lastMinValue)
, "-i", String.valueOf(uri), "-t",
TrimmerUtils.formatCSeconds(lastMaxValue - lastMinValue),
"-async", "1", "-strict", "-2", "-c", "copy", outputPath};
String[] complexCommand;
if (isAccurateCut) {
complexCommand = new String[]{"ffmpeg", "-ss", TrimmerUtils.formatCSeconds(lastMinValue)
, "-i", String.valueOf(uri), "-t",
TrimmerUtils.formatCSeconds(lastMaxValue - lastMinValue),
"-async", "1", outputPath};
} else {
complexCommand = new String[]{"ffmpeg", "-ss", TrimmerUtils.formatCSeconds(lastMinValue)
, "-i", String.valueOf(uri), "-t",
TrimmerUtils.formatCSeconds(lastMaxValue - lastMinValue),
"-async", "1", "-strict", "-2", "-c", "copy", outputPath};
}
FFmpegCmd.exec(complexCommand, 0, new OnEditorListener() {
@Override
public void onSuccess() {
progressView.dismiss();
Intent intent = new Intent();
intent.putExtra(TrimmerConstants.TRIMMED_VIDEO_PATH, outputPath);
intent.putExtra(TrimVideo.TRIMMED_VIDEO_PATH, outputPath);
setResult(RESULT_OK, intent);
finish();
}
Expand Down Expand Up @@ -491,6 +486,7 @@ private boolean checkStoragePermission() {
} else
return checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE);

}

private boolean checkPermission(String... permissions) {
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/com/gowtham/library/utils/TrimType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gowtham.library.utils;

public enum TrimType {
DEFAULT, FIXED_DURATION, MIN_DURATION, MIN_MAX_DURATION
}
Loading

0 comments on commit 108b78c

Please sign in to comment.