11package protect .videotranscoder .activity ;
22
33import android .Manifest ;
4- import android .app .ProgressDialog ;
54import android .content .ContentUris ;
65import android .content .Context ;
76import android .content .DialogInterface ;
2726import android .widget .AdapterView ;
2827import android .widget .ArrayAdapter ;
2928import android .widget .Button ;
29+ import android .widget .ProgressBar ;
3030import android .widget .Spinner ;
3131import android .widget .TextView ;
3232import 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