Merge "Fix ThumbnailUtils thumbnail pts calculation"
This commit is contained in:
committed by
Android (Google) Code Review
commit
dfb94f02fc
@@ -800,7 +800,7 @@ public class MediaMetadataRetriever implements AutoCloseable {
|
||||
*/
|
||||
public static final int METADATA_KEY_YEAR = 8;
|
||||
/**
|
||||
* The metadata key to retrieve the playback duration of the data source.
|
||||
* The metadata key to retrieve the playback duration (in ms) of the data source.
|
||||
*/
|
||||
public static final int METADATA_KEY_DURATION = 9;
|
||||
/**
|
||||
|
||||
@@ -356,19 +356,21 @@ public class ThumbnailUtils {
|
||||
return ImageDecoder.decodeBitmap(ImageDecoder.createSource(raw), resizer);
|
||||
}
|
||||
|
||||
// Fall back to middle of video
|
||||
final int width = Integer.parseInt(mmr.extractMetadata(METADATA_KEY_VIDEO_WIDTH));
|
||||
final int height = Integer.parseInt(mmr.extractMetadata(METADATA_KEY_VIDEO_HEIGHT));
|
||||
final long duration = Long.parseLong(mmr.extractMetadata(METADATA_KEY_DURATION));
|
||||
// Fall back to middle of video
|
||||
// Note: METADATA_KEY_DURATION unit is in ms, not us.
|
||||
final long thumbnailTimeUs =
|
||||
Long.parseLong(mmr.extractMetadata(METADATA_KEY_DURATION)) * 1000 / 2;
|
||||
|
||||
// If we're okay with something larger than native format, just
|
||||
// return a frame without up-scaling it
|
||||
if (size.getWidth() > width && size.getHeight() > height) {
|
||||
return Objects.requireNonNull(
|
||||
mmr.getFrameAtTime(duration / 2, OPTION_CLOSEST_SYNC));
|
||||
mmr.getFrameAtTime(thumbnailTimeUs, OPTION_CLOSEST_SYNC));
|
||||
} else {
|
||||
return Objects.requireNonNull(
|
||||
mmr.getScaledFrameAtTime(duration / 2, OPTION_CLOSEST_SYNC,
|
||||
mmr.getScaledFrameAtTime(thumbnailTimeUs, OPTION_CLOSEST_SYNC,
|
||||
size.getWidth(), size.getHeight()));
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
|
||||
Reference in New Issue
Block a user