Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0da7cdc1 authored by Chong Zhang's avatar Chong Zhang
Browse files

Fix ThumbnailUtils thumbnail pts calculation

bug: 137113185
test: manual test the clip attached in the bug in Download app.
Change-Id: I824cf1a5c5e08b87e7534a4c2287286134838917
parent 1cac4fad
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
    /**
+6 −4
Original line number Diff line number Diff line
@@ -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) {