Skip to content

Commit 2690f93

Browse files
authored
Merge pull request #7 from brarcher/cancel
Add cancel button
2 parents a626f55 + 44b6dd3 commit 2690f93

File tree

5 files changed

+109
-142
lines changed

5 files changed

+109
-142
lines changed

app/src/main/java/protect/videotranscoder/FFmpegResponseHandler.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.ProgressDialog;
44
import android.content.Context;
55
import android.util.Log;
6+
import android.widget.ProgressBar;
67

78
import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
89

@@ -11,14 +12,14 @@ public class FFmpegResponseHandler extends ExecuteBinaryResponseHandler
1112
private static final String TAG = "VideoTranscoder";
1213

1314
private final long _durationMs;
14-
private final ProgressDialog _progressDialog;
15+
private final ProgressBar _progressBar;
1516
private final ResultCallbackHandler<Boolean> _resultHandler;
1617

17-
public FFmpegResponseHandler(long durationMs, ProgressDialog progressDialog,
18+
public FFmpegResponseHandler(long durationMs, ProgressBar progressBar,
1819
ResultCallbackHandler<Boolean> resultHandler)
1920
{
2021
_durationMs = durationMs;
21-
_progressDialog = progressDialog;
22+
_progressBar = progressBar;
2223
_resultHandler = resultHandler;
2324
}
2425

@@ -61,23 +62,19 @@ public void onProgress(String s)
6162
if(currentTimeMs != null)
6263
{
6364
int percentComplete = (int)Math.floor((currentTimeMs * 100) / (float)_durationMs);
64-
65-
_progressDialog.setMessage(percentComplete + "%");
65+
_progressBar.setProgress(percentComplete);
6666
}
6767
}
6868

6969
@Override
7070
public void onStart()
7171
{
7272
Log.d(TAG, "Started FFmpeg command");
73-
_progressDialog.setMessage("Processing...");
74-
_progressDialog.show();
7573
}
7674

7775
@Override
7876
public void onFinish()
7977
{
8078
Log.d(TAG, "Finished command");
81-
_progressDialog.dismiss();
8279
}
8380
}

app/src/main/java/protect/videotranscoder/FFmpegUtil.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ public void onFailure(String message)
121121
call(command, handler);
122122
}
123123

124+
/**
125+
* Cancel the current in-progress invocation of FFmpeg
126+
*/
127+
public static void cancelCall()
128+
{
129+
String message;
130+
131+
if(ffmpeg == null || ffmpeg.isFFmpegCommandRunning() == false)
132+
{
133+
message = "Cancel failed, FFmpeg not running";
134+
}
135+
else
136+
{
137+
boolean result = ffmpeg.killRunningProcesses();
138+
message = "Attempt to cancel FFmpeg: " + (result ? "success" : "failed");
139+
}
140+
141+
Log.d(TAG, message);
142+
}
143+
124144
@NonNull
125145
private static String commandToString(String [] cmd)
126146
{

app/src/main/java/protect/videotranscoder/activity/MainActivity.java

Lines changed: 43 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package protect.videotranscoder.activity;
22

33
import android.Manifest;
4-
import android.app.ProgressDialog;
54
import android.content.ContentUris;
65
import android.content.Context;
76
import android.content.DialogInterface;
@@ -27,6 +26,7 @@
2726
import android.widget.AdapterView;
2827
import android.widget.ArrayAdapter;
2928
import android.widget.Button;
29+
import android.widget.ProgressBar;
3030
import android.widget.Spinner;
3131
import android.widget.TextView;
3232
import android.widget.Toast;
@@ -96,7 +96,7 @@ public class MainActivity extends AppCompatActivity
9696
private VideoView videoView;
9797
private CrystalRangeSeekbar rangeSeekBar;
9898
private Timer videoTimer = null;
99-
private ProgressDialog progressDialog;
99+
private ProgressBar progressBar;
100100

101101
private Spinner containerSpinner;
102102
private Spinner videoCodecSpinner;
@@ -109,7 +109,9 @@ public class MainActivity extends AppCompatActivity
109109
private Spinner audioChannelSpinner;
110110

111111
private TextView tvLeft, tvRight;
112+
private Button selectVideoButton;
112113
private Button encodeButton;
114+
private Button cancelButton;
113115
private MediaInfo videoInfo;
114116
private File outputDestination;
115117

@@ -118,17 +120,16 @@ protected void onCreate(Bundle savedInstanceState)
118120
{
119121
super.onCreate(savedInstanceState);
120122
setContentView(R.layout.activity_main);
121-
final Button selectVideo = findViewById(R.id.selectVideo);
123+
selectVideoButton = findViewById(R.id.selectVideo);
122124
encodeButton = findViewById(R.id.encode);
125+
cancelButton = findViewById(R.id.cancel);
123126

124127
tvLeft = findViewById(R.id.tvLeft);
125128
tvRight = findViewById(R.id.tvRight);
126129

127130
videoView = findViewById(R.id.videoView);
128131
rangeSeekBar = findViewById(R.id.rangeSeekBar);
129-
progressDialog = new ProgressDialog(this);
130-
progressDialog.setTitle(null);
131-
progressDialog.setCancelable(false);
132+
progressBar = findViewById(R.id.encodeProgress);
132133
rangeSeekBar.setEnabled(false);
133134

134135
containerSpinner = findViewById(R.id.containerSpinner);
@@ -153,7 +154,7 @@ public void onResult(Boolean result)
153154
}
154155
});
155156

156-
selectVideo.setOnClickListener(new View.OnClickListener()
157+
selectVideoButton.setOnClickListener(new View.OnClickListener()
157158
{
158159
@Override
159160
public void onClick(View v)
@@ -177,6 +178,15 @@ public void onClick(View v)
177178
startEncode();
178179
}
179180
});
181+
182+
cancelButton.setOnClickListener(new View.OnClickListener()
183+
{
184+
@Override
185+
public void onClick(View v)
186+
{
187+
cancelEncode();
188+
}
189+
});
180190
}
181191

182192
private void getPermission()
@@ -321,11 +331,12 @@ private void startEncode()
321331
}
322332

323333
int endTimeSec = rangeSeekBar.getSelectedMaxValue().intValue();
334+
int durationSec = endTimeSec - startTimeSec;
324335
if( (videoInfo.durationMs)/1000 != endTimeSec)
325336
{
326337
// Duration of media file
327338
command.add("-t");
328-
command.add(Integer.toString(endTimeSec - startTimeSec));
339+
command.add(Integer.toString(durationSec));
329340
}
330341

331342
if(container.supportedVideoCodecs.size() > 0)
@@ -379,10 +390,28 @@ private void startEncode()
379390
// Output file
380391
command.add(destination.getAbsolutePath());
381392

382-
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
393+
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationSec*1000, progressBar, _transcodeResultHandler);
383394
FFmpegUtil.call(command.toArray(new String[command.size()]), handler);
384395

385396
stopVideoPlayback();
397+
398+
selectVideoButton.setVisibility(View.GONE);
399+
encodeButton.setVisibility(View.GONE);
400+
cancelButton.setVisibility(View.VISIBLE);
401+
progressBar.setVisibility(View.VISIBLE);
402+
}
403+
404+
private void cancelEncode()
405+
{
406+
FFmpegUtil.cancelCall();
407+
408+
_transcodeResultHandler.onResult(false);
409+
410+
boolean result = outputDestination.delete();
411+
if(result == false)
412+
{
413+
Log.d(TAG, "Failed to remove after encode cancel: " + outputDestination.getAbsolutePath());
414+
}
386415
}
387416

388417
private void stopVideoPlayback()
@@ -715,107 +744,13 @@ public void onClick(DialogInterface dialog, int which)
715744
.show();
716745

717746
startVideoPlayback();
718-
}
719-
};
720747

721-
/**
722-
* Command for cutting video
723-
*/
724-
private void executeCutVideoCommand(int startMs, int endMs)
725-
{
726-
File moviesDir = Environment.getExternalStoragePublicDirectory(
727-
Environment.DIRECTORY_MOVIES
728-
);
729-
730-
String filePrefix = "cut_video";
731-
String fileExtn = ".mp4";
732-
String yourRealPath = videoInfo.file.getAbsolutePath();
733-
File dest = new File(moviesDir, filePrefix + fileExtn);
734-
int fileNo = 0;
735-
while (dest.exists())
736-
{
737-
fileNo++;
738-
dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
748+
selectVideoButton.setVisibility(View.VISIBLE);
749+
encodeButton.setVisibility(View.VISIBLE);
750+
cancelButton.setVisibility(View.GONE);
751+
progressBar.setVisibility(View.GONE);
739752
}
740-
741-
Log.d(TAG, "startTrim: src: " + yourRealPath);
742-
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
743-
Log.d(TAG, "startTrim: startMs: " + startMs);
744-
Log.d(TAG, "startTrim: endMs: " + endMs);
745-
outputDestination = dest;
746-
747-
final String[] complexCommand = {"-ss", "" + startMs / 1000, "-y", "-i", yourRealPath, "-t", "" + (endMs - startMs) / 1000,"-vcodec", "mpeg4", "-b:v", "2097152", "-b:a", "48000", "-ac", "2", "-ar", "22050", dest.getAbsolutePath()};
748-
749-
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
750-
FFmpegUtil.call(complexCommand, handler);
751-
752-
stopVideoPlayback();
753-
}
754-
755-
/**
756-
* Command for compressing video
757-
*/
758-
private void executeCompressCommand()
759-
{
760-
File moviesDir = Environment.getExternalStoragePublicDirectory(
761-
Environment.DIRECTORY_MOVIES
762-
);
763-
764-
String filePrefix = "compress_video";
765-
String fileExtn = ".mp4";
766-
String yourRealPath = videoInfo.file.getAbsolutePath();
767-
768-
769-
File dest = new File(moviesDir, filePrefix + fileExtn);
770-
int fileNo = 0;
771-
while (dest.exists())
772-
{
773-
fileNo++;
774-
dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
775-
}
776-
777-
Log.d(TAG, "startTrim: src: " + yourRealPath);
778-
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
779-
outputDestination = dest;
780-
String[] complexCommand = {"-y", "-i", yourRealPath, "-s", "160x120", "-r", "25", "-vcodec", "mpeg4", "-b:v", "150k", "-b:a", "48000", "-ac", "2", "-ar", "22050", dest.getAbsolutePath()};
781-
782-
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
783-
FFmpegUtil.call(complexCommand, handler);
784-
785-
stopVideoPlayback();
786-
}
787-
788-
/**
789-
* Command for extracting audio from video
790-
*/
791-
private void extractAudioVideo()
792-
{
793-
File moviesDir = Environment.getExternalStoragePublicDirectory(
794-
Environment.DIRECTORY_MUSIC
795-
);
796-
797-
String filePrefix = "extract_audio";
798-
String fileExtn = ".mp3";
799-
String yourRealPath = videoInfo.file.getAbsolutePath();
800-
File dest = new File(moviesDir, filePrefix + fileExtn);
801-
802-
int fileNo = 0;
803-
while (dest.exists())
804-
{
805-
fileNo++;
806-
dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
807-
}
808-
Log.d(TAG, "startTrim: src: " + yourRealPath);
809-
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
810-
outputDestination = dest;
811-
812-
String[] complexCommand = {"-y", "-i", yourRealPath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", dest.getAbsolutePath()};
813-
814-
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
815-
FFmpegUtil.call(complexCommand, handler);
816-
817-
stopVideoPlayback();
818-
}
753+
};
819754

820755
/**
821756
* Get a file path from a Uri. This will get the the path for Storage Access

0 commit comments

Comments
 (0)