Skip to content

Commit 1c45c00

Browse files
committed
Fetch media info after video selected
1 parent 9bea0bd commit 1c45c00

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

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

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.view.View;
2626
import android.webkit.WebView;
2727
import android.widget.TextView;
28+
import android.widget.Toast;
2829
import android.widget.VideoView;
2930

3031
import com.crystal.crystalrangeseekbar.interfaces.OnRangeSeekbarChangeListener;
@@ -47,6 +48,9 @@
4748
import protect.videotranscoder.FFmpegUtil;
4849
import protect.videotranscoder.R;
4950
import protect.videotranscoder.ResultCallbackHandler;
51+
import protect.videotranscoder.media.AudioCodec;
52+
import protect.videotranscoder.media.MediaInfo;
53+
import protect.videotranscoder.media.VideoCodec;
5054

5155
public class MainActivity extends AppCompatActivity
5256
{
@@ -87,11 +91,10 @@ public class MainActivity extends AppCompatActivity
8791
private CrystalRangeSeekbar rangeSeekBar;
8892
private Timer videoTimer = null;
8993
private ProgressDialog progressDialog;
90-
private Uri selectedVideoUri;
9194

9295
private TextView tvLeft, tvRight;
93-
private String filePath;
94-
private int durationMs;
96+
private MediaInfo videoInfo;
97+
private File outputDestination;
9598

9699
@Override
97100
protected void onCreate(Bundle savedInstanceState)
@@ -272,6 +275,22 @@ protected void onResume()
272275
startVideoPlayback();
273276
}
274277

278+
private void populateOptionDefaults()
279+
{
280+
for(int id : BASIC_SETTINGS_IDS)
281+
{
282+
findViewById(id).setVisibility(View.VISIBLE);
283+
}
284+
for(int id : VIDEO_SETTINGS_IDS)
285+
{
286+
findViewById(id).setVisibility(View.VISIBLE);
287+
}
288+
for(int id : AUDIO_SETTINGS_IDS)
289+
{
290+
findViewById(id).setVisibility(View.VISIBLE);
291+
}
292+
}
293+
275294
@Override
276295
protected void onActivityResult(int requestCode, int resultCode, Intent data)
277296
{
@@ -280,30 +299,24 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data)
280299
{
281300
if (requestCode == REQUEST_TAKE_GALLERY_VIDEO)
282301
{
283-
for(int id : BASIC_SETTINGS_IDS)
284-
{
285-
findViewById(id).setVisibility(View.VISIBLE);
286-
}
287-
for(int id : VIDEO_SETTINGS_IDS)
288-
{
289-
findViewById(id).setVisibility(View.VISIBLE);
290-
}
291-
for(int id : AUDIO_SETTINGS_IDS)
302+
Uri selectedVideoUri = data.getData();
303+
String videoPath = getPath(MainActivity.this, selectedVideoUri);
304+
305+
if(videoPath == null)
292306
{
293-
findViewById(id).setVisibility(View.VISIBLE);
307+
Toast.makeText(this, R.string.fileLocationLookupFailed, Toast.LENGTH_LONG).show();
308+
return;
294309
}
295310

296-
selectedVideoUri = data.getData();
297311
videoView.setVideoURI(selectedVideoUri);
298312
videoView.start();
299313

300314
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
301315
{
302-
303316
@Override
304317
public void onPrepared(MediaPlayer mp)
305318
{
306-
durationMs = mp.getDuration();
319+
int durationMs = mp.getDuration();
307320
tvLeft.setVisibility(View.VISIBLE);
308321
tvLeft.setText("00:00:00");
309322
tvRight.setVisibility(View.VISIBLE);
@@ -328,6 +341,27 @@ public void valueChanged(Number minValue, Number maxValue)
328341
});
329342
}
330343
});
344+
345+
final File videoFile = new File(videoPath);
346+
347+
FFmpegUtil.getMediaDetails(new File(videoPath), new ResultCallbackHandler<MediaInfo>()
348+
{
349+
@Override
350+
public void onResult(MediaInfo result)
351+
{
352+
if(result == null)
353+
{
354+
// Could not query the file, fill in what we know.
355+
result = new MediaInfo(videoFile, 0, VideoCodec.MPEG4.name(), "640x480",
356+
"800", "25", AudioCodec.MP3.name(),
357+
"44100", "128", 2);
358+
}
359+
360+
videoInfo = result;
361+
362+
populateOptionDefaults();
363+
}
364+
});
331365
}
332366
}
333367
}
@@ -358,7 +392,6 @@ public void onClick(DialogInterface dialog, int which)
358392
})
359393
.create()
360394
.show();
361-
362395
}
363396

364397
private ResultCallbackHandler<Boolean> _transcodeResultHandler = new ResultCallbackHandler<Boolean>()
@@ -370,7 +403,7 @@ public void onResult(Boolean result)
370403

371404
if(result)
372405
{
373-
message = getResources().getString(R.string.transcodeSuccess, filePath);
406+
message = getResources().getString(R.string.transcodeSuccess, outputDestination.getAbsolutePath());
374407
}
375408
else
376409
{
@@ -404,7 +437,7 @@ private void executeCutVideoCommand(int startMs, int endMs)
404437

405438
String filePrefix = "cut_video";
406439
String fileExtn = ".mp4";
407-
String yourRealPath = getPath(MainActivity.this, selectedVideoUri);
440+
String yourRealPath = videoInfo.file.getAbsolutePath();
408441
File dest = new File(moviesDir, filePrefix + fileExtn);
409442
int fileNo = 0;
410443
while (dest.exists())
@@ -417,11 +450,11 @@ private void executeCutVideoCommand(int startMs, int endMs)
417450
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
418451
Log.d(TAG, "startTrim: startMs: " + startMs);
419452
Log.d(TAG, "startTrim: endMs: " + endMs);
420-
filePath = dest.getAbsolutePath();
453+
outputDestination = dest;
421454

422-
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", filePath};
455+
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()};
423456

424-
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationMs, progressDialog, _transcodeResultHandler);
457+
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
425458
FFmpegUtil.call(complexCommand, handler);
426459

427460
stopVideoPlayback();
@@ -438,7 +471,7 @@ private void executeCompressCommand()
438471

439472
String filePrefix = "compress_video";
440473
String fileExtn = ".mp4";
441-
String yourRealPath = getPath(MainActivity.this, selectedVideoUri);
474+
String yourRealPath = videoInfo.file.getAbsolutePath();
442475

443476

444477
File dest = new File(moviesDir, filePrefix + fileExtn);
@@ -451,10 +484,10 @@ private void executeCompressCommand()
451484

452485
Log.d(TAG, "startTrim: src: " + yourRealPath);
453486
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
454-
filePath = dest.getAbsolutePath();
455-
String[] complexCommand = {"-y", "-i", yourRealPath, "-s", "160x120", "-r", "25", "-vcodec", "mpeg4", "-b:v", "150k", "-b:a", "48000", "-ac", "2", "-ar", "22050", filePath};
487+
outputDestination = dest;
488+
String[] complexCommand = {"-y", "-i", yourRealPath, "-s", "160x120", "-r", "25", "-vcodec", "mpeg4", "-b:v", "150k", "-b:a", "48000", "-ac", "2", "-ar", "22050", dest.getAbsolutePath()};
456489

457-
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationMs, progressDialog, _transcodeResultHandler);
490+
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
458491
FFmpegUtil.call(complexCommand, handler);
459492

460493
stopVideoPlayback();
@@ -471,7 +504,7 @@ private void extractAudioVideo()
471504

472505
String filePrefix = "extract_audio";
473506
String fileExtn = ".mp3";
474-
String yourRealPath = getPath(MainActivity.this, selectedVideoUri);
507+
String yourRealPath = videoInfo.file.getAbsolutePath();
475508
File dest = new File(moviesDir, filePrefix + fileExtn);
476509

477510
int fileNo = 0;
@@ -482,11 +515,11 @@ private void extractAudioVideo()
482515
}
483516
Log.d(TAG, "startTrim: src: " + yourRealPath);
484517
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
485-
filePath = dest.getAbsolutePath();
518+
outputDestination = dest;
486519

487-
String[] complexCommand = {"-y", "-i", yourRealPath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", filePath};
520+
String[] complexCommand = {"-y", "-i", yourRealPath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", dest.getAbsolutePath()};
488521

489-
FFmpegResponseHandler handler = new FFmpegResponseHandler(durationMs, progressDialog, _transcodeResultHandler);
522+
FFmpegResponseHandler handler = new FFmpegResponseHandler(videoInfo.durationMs, progressDialog, _transcodeResultHandler);
490523
FFmpegUtil.call(complexCommand, handler);
491524

492525
stopVideoPlayback();

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<string name="writePermissionExplanation">This application needs access to storage in order to read and write media files. Without this access, the application cannot transcode.</string>
2222
<string name="permissionRequestAgain">Request again</string>
2323

24+
<string name="fileLocationLookupFailed">Failed to resolve the location of the media file</string>
25+
2426
<string name="about">About</string>
2527
<string name="app_copyright_fmt">Copyright 2018-<xliff:g>%d</xliff:g> Branden Archer</string>
2628
<string name="app_license">Licensed under the GPLv2.</string>

0 commit comments

Comments
 (0)