Skip to content

Commit ead8b99

Browse files
authored
Merge pull request #128 from brarcher/invalid-files-handling
Invalid files handling
2 parents fca7e7a + cb9d28c commit ead8b99

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ public static void probeGetOutput(@NonNull final String [] command,
154154
@Override
155155
public void onSuccess(String message)
156156
{
157+
// On some systems some warnings are emitted right before the json, such as:
158+
// WARNING: linker: /data/data/protect.videoeditor/files/ffprobe: unused DT entry: type 0x6ffffffe arg 0x22c0
159+
// Skip past this to the start of the json, if any exists.
160+
161+
int jsonStartIndex = message.indexOf('{');
162+
if(jsonStartIndex != -1)
163+
{
164+
message = message.substring(jsonStartIndex);
165+
}
166+
157167
resultHandler.onResult(message);
158168
}
159169

@@ -499,7 +509,7 @@ static MediaInfo parseMediaInfo(File mediaFile, String mediaDetailsJsonStr)
499509
}
500510
catch (IOException e)
501511
{
502-
Log.w(TAG, "Failed to read media details for file : " + mediaFile.getAbsolutePath() + "\n" + mediaDetailsJsonStr);
512+
Log.w(TAG, "Failed to read media details for file : " + mediaFile.getAbsolutePath() + "\n" + mediaDetailsJsonStr, e);
503513
}
504514

505515
if(totalBitrateK != null)
@@ -525,8 +535,14 @@ static MediaInfo parseMediaInfo(File mediaFile, String mediaDetailsJsonStr)
525535
}
526536
}
527537

528-
MediaInfo info = new MediaInfo(mediaFile, durationMs, container, videoCodec, videoResolution,
529-
videoBitrateK, videoFramerate, audioCodec, audioSampleRate, audioBitrateK, audioChannels);
538+
MediaInfo info = null;
539+
540+
if(container != null && (videoCodec != null || audioCodec != null))
541+
{
542+
info = new MediaInfo(mediaFile, durationMs, container, videoCodec, videoResolution,
543+
videoBitrateK, videoFramerate, audioCodec, audioSampleRate, audioBitrateK, audioChannels);
544+
}
545+
530546
return info;
531547
}
532548

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ protected void onResume()
974974
{
975975
super.onResume();
976976

977-
if(isEncoding() == false)
977+
if(isEncoding() == false && videoInfo != null)
978978
{
979979
startVideoPlayback(null);
980980
}
@@ -1032,6 +1032,28 @@ private void setSpinnerSelection(Spinner spinner, Object value)
10321032
}
10331033
}
10341034

1035+
private void hideAllOptions()
1036+
{
1037+
for(int id : BASIC_SETTINGS_IDS)
1038+
{
1039+
findViewById(id).setVisibility(View.GONE);
1040+
}
1041+
for(int id : VIDEO_SETTINGS_IDS)
1042+
{
1043+
findViewById(id).setVisibility(View.GONE);
1044+
}
1045+
for(int id : AUDIO_SETTINGS_IDS)
1046+
{
1047+
findViewById(id).setVisibility(View.GONE);
1048+
}
1049+
1050+
encodeButton.setVisibility(View.GONE);
1051+
startJumpBack.setVisibility(View.GONE);
1052+
startJumpForward.setVisibility(View.GONE);
1053+
endJumpBack.setVisibility(View.GONE);
1054+
endJumpForward.setVisibility(View.GONE);
1055+
}
1056+
10351057
private void populateOptionDefaults()
10361058
{
10371059
for(int id : BASIC_SETTINGS_IDS)
@@ -1277,9 +1299,6 @@ public void onNothingSelected(AdapterView<?> parentView)
12771299

12781300
private void setSelectMediaFile(String path, String overrideBaseName)
12791301
{
1280-
videoView.setVideoPath(path);
1281-
videoView.start();
1282-
12831302
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
12841303
{
12851304
@Override
@@ -1371,26 +1390,28 @@ public void onClick(View v)
13711390
}
13721391
});
13731392

1374-
final File videoFile = new File(path);
1375-
13761393
FFmpegUtil.getMediaDetails(new File(path), new ResultCallbackHandler<MediaInfo>()
13771394
{
13781395
@Override
13791396
public void onResult(MediaInfo result)
13801397
{
1381-
if(result == null)
1382-
{
1383-
Log.d(TAG, "Failed to query media file, filling in defaults");
1384-
// Could not query the file, fill in what we know.
1385-
result = new MediaInfo(videoFile, 0, MediaContainer.MP4, VideoCodec.MPEG4, "640x480",
1386-
800, "25", AudioCodec.MP3,
1387-
44100, 128, 2);
1388-
}
1389-
13901398
videoInfo = result;
1391-
videoInfo.setFileBaseName(overrideBaseName);
13921399

1393-
populateOptionDefaults();
1400+
if(result != null)
1401+
{
1402+
String uri = Uri.fromFile(new File(path)).toString();
1403+
videoView.setVideoPath(uri);
1404+
videoView.start();
1405+
1406+
videoInfo.setFileBaseName(overrideBaseName);
1407+
populateOptionDefaults();
1408+
}
1409+
else
1410+
{
1411+
Toast.makeText(MainActivity.this, R.string.invalidMediaFile, Toast.LENGTH_LONG).show();
1412+
hideAllOptions();
1413+
stopVideoPlayback();
1414+
}
13941415
}
13951416
});
13961417
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<string name="couldNotFindFileSubmsg">could not find input file</string>
3939
<string name="fieldMissingError"><xliff:g id="field_name">%s</xliff:g> missing</string>
4040
<string name="fieldMissingOrInvalidError"><xliff:g id="field_name">%s</xliff:g> missing or invalid</string>
41+
<string name="invalidMediaFile">The selected media file is invalid or not supported</string>
4142

4243
<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>
4344
<string name="permissionRequestAgain">Request again</string>

app/src/test/shell/test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import collections
77
import time
8+
from datetime import datetime
89

910
ASSETS = os.path.dirname(os.path.realpath(__file__)) + os.sep + "assets"
1011

@@ -18,11 +19,8 @@ def adb(args):
1819
if result != 0:
1920
raise Exception("adb failed with " + str(result) + ": " + str(cmd))
2021

21-
def clearLogcat():
22-
adb(["shell", "logcat", "-c"])
23-
24-
def logcat():
25-
p = subprocess.Popen(["adb", "logcat", "-d"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
22+
def logcat(sinceTime):
23+
p = subprocess.Popen(["adb", "logcat", "-t", sinceTime], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2624
stdout, stderr = p.communicate()
2725
rc = p.wait()
2826
if rc != 0:
@@ -40,12 +38,12 @@ def pushAsset(filename):
4038

4139
def encodeVideoTest(test, output):
4240
pushAsset(test.filename)
43-
clearLogcat()
41+
startTime = datetime.now().strftime('%m-%d %H:%M:%S.000')
4442
encodeVideo(test.filename, output, test.mediaContainer, test.videoCodec, test.videoBitrateK, test.resolution, test.fps, test.audioCodec, test.audioSampleRate, test.audioBitrateK, test.audioChannel)
4543

4644
logs = None
4745
for count in range(1, 300):
48-
logs = logcat()
46+
logs = logcat(startTime)
4947
if "Encode result" in logs:
5048
break
5149
time.sleep(1)
@@ -57,6 +55,7 @@ def encodeVideoTest(test, output):
5755
raise Exception("Failed to encode file: " + test.filename)
5856

5957
pullFile(output)
58+
removeFile(output)
6059

6160
def encodeVideo(filename, output, mediaContainer, videoCodec, videoBitrateK, resolution, fps, audioCodec, audioSampleRate, audioBitrateK, audioChannel):
6261
args = []
@@ -121,12 +120,12 @@ def encodeVideo(filename, output, mediaContainer, videoCodec, videoBitrateK, res
121120

122121
def encodeAudioTest(test, output):
123122
pushAsset(test.filename)
124-
clearLogcat()
123+
startTime = datetime.now().strftime('%m-%d %H:%M:%S.000')
125124
encodeAudio(test.filename, output, test.mediaContainer, test.audioCodec, test.audioSampleRate, test.audioBitrateK, test.audioChannel)
126125

127126
logs = None
128127
for count in range(1, 300):
129-
logs = logcat()
128+
logs = logcat(startTime)
130129
if "Encode result" in logs:
131130
break
132131
time.sleep(1)
@@ -138,6 +137,7 @@ def encodeAudioTest(test, output):
138137
raise Exception("Failed to encode file: " + test.filename)
139138

140139
pullFile(output)
140+
removeFile(output)
141141

142142
def encodeAudio(filename, output, mediaContainer, audioCodec, audioSampleRate, audioBitrateK, audioChannel):
143143
args = []

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.1.3'
9+
classpath 'com.android.tools.build:gradle:3.1.4'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

0 commit comments

Comments
 (0)