bugfix: catalog metadata fallback for video was skipped

This commit is contained in:
Thibault Deckers 2020-06-14 14:47:39 +09:00
parent 297da41c64
commit 1dba550c3e

View file

@ -73,7 +73,11 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
private static final String XMP_GENERIC_LANG = "";
private static final String XMP_SPECIFIC_LANG = "en-US";
private static final Pattern VIDEO_LOCATION_PATTERN = Pattern.compile("([+-][.0-9]+)([+-][.0-9]+)/?");
// Pattern to extract latitude & longitude from a video location tag (cf ISO 6709)
// Examples:
// "+37.5090+127.0243/" (Samsung)
// "+51.3328-000.7053+113.474/" (Apple)
private static final Pattern VIDEO_LOCATION_PATTERN = Pattern.compile("([+-][.0-9]+)([+-][.0-9]+).*");
private Context context;
@ -146,7 +150,7 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
Log.w(LOG_TAG, "failed to get video metadata by ImageMetadataReader for uri=" + uri, e);
}
Map<String, String> videoDir = getVideoMetadataByRetriever(uri);
Map<String, String> videoDir = getVideoAllMetadataByMediaMetadataRetriever(uri);
if (!videoDir.isEmpty()) {
metadataMap.put("Video", videoDir);
}
@ -158,7 +162,7 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
}
}
private Map<String, String> getVideoMetadataByRetriever(String uri) {
private Map<String, String> getVideoAllMetadataByMediaMetadataRetriever(String uri) {
Map<String, String> dirMap = new HashMap<>();
MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, Uri.parse(uri));
try {
@ -190,10 +194,25 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
String mimeType = call.argument("mimeType");
String uri = call.argument("uri");
Map<String, Object> metadataMap = new HashMap<>(getCatalogMetadataByImageMetadataReader(uri, mimeType));
if (isVideo(mimeType)) {
metadataMap.putAll(getVideoCatalogMetadataByMediaMetadataRetriever(uri));
}
if (metadataMap.isEmpty()) {
result.error("getCatalogMetadata-failure", "failed to get catalog metadata for uri=" + uri, null);
} else {
result.success(metadataMap);
}
}
private Map<String, Object> getCatalogMetadataByImageMetadataReader(String uri, String mimeType) {
Map<String, Object> metadataMap = new HashMap<>();
// as of metadata-extractor 2.14.0, MP2T files are not supported
if (MimeTypes.MP2T.equals(mimeType)) return metadataMap;
try (InputStream is = StorageUtils.openInputStream(context, Uri.parse(uri))) {
if (!MimeTypes.MP2T.equals(mimeType)) {
Metadata metadata = ImageMetadataReader.readMetadata(is);
// EXIF
@ -247,9 +266,14 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
}
}
}
} catch (Exception | NoClassDefFoundError e) {
Log.w(LOG_TAG, "failed to get catalog metadata by ImageMetadataReader for uri=" + uri, e);
}
return metadataMap;
}
if (isVideo(mimeType)) {
private Map<String, Object> getVideoCatalogMetadataByMediaMetadataRetriever(String uri) {
Map<String, Object> metadataMap = new HashMap<>();
MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, Uri.parse(uri));
try {
String dateString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
@ -267,15 +291,18 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
metadataMap.put(KEY_VIDEO_ROTATION, Integer.parseInt(rotationString));
}
if (locationString != null) {
Log.d(LOG_TAG, "TLAD locationString=" + locationString);
Matcher locationMatcher = VIDEO_LOCATION_PATTERN.matcher(locationString);
if (locationMatcher.find() && locationMatcher.groupCount() == 2) {
if (locationMatcher.find() && locationMatcher.groupCount() >= 2) {
String latitudeString = locationMatcher.group(1);
String longitudeString = locationMatcher.group(2);
Log.d(LOG_TAG, "TLAD latitudeString=" + latitudeString + ", longitudeString=" + longitudeString);
if (latitudeString != null && longitudeString != null) {
try {
double latitude = Double.parseDouble(latitudeString);
double longitude = Double.parseDouble(longitudeString);
if (latitude != 0 && longitude != 0) {
Log.d(LOG_TAG, "TLAD latitude=" + latitude + ", longitude=" + longitude);
metadataMap.put(KEY_LATITUDE, latitude);
metadataMap.put(KEY_LONGITUDE, longitude);
}
@ -286,16 +313,12 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
}
}
} catch (Exception e) {
result.error("getCatalogMetadata-exception", "failed to get video metadata for uri=" + uri, e.getMessage());
Log.w(LOG_TAG, "failed to get catalog metadata by MediaMetadataRetriever for uri=" + uri, e);
} finally {
// cannot rely on `MediaMetadataRetriever` being `AutoCloseable` on older APIs
retriever.release();
}
}
result.success(metadataMap);
} catch (Exception | NoClassDefFoundError e) {
result.error("getCatalogMetadata-exception", "failed to get metadata for uri=" + uri, e.getMessage());
}
return metadataMap;
}
private void getOverlayMetadata(MethodCall call, MethodChannel.Result result) {