Java Code Examples for android.media.MediaMetadataRetriever#getFrameAtTime()

The following examples show how to use android.media.MediaMetadataRetriever#getFrameAtTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttachmentDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private ThumbnailData generateVideoThumbnail(AttachmentId attachmentId, long timeUs) throws IOException {
  if (Build.VERSION.SDK_INT < 23) {
    Log.w(TAG, "Video thumbnails not supported...");
    return null;
  }

  try (MediaDataSource dataSource = mediaDataSourceFor(attachmentId)) {
    if (dataSource == null) return null;

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    MediaMetadataRetrieverUtil.setDataSource(retriever, dataSource);

    Bitmap bitmap = retriever.getFrameAtTime(timeUs);

    Log.i(TAG, "Generated video thumbnail...");
    return bitmap != null ? new ThumbnailData(bitmap) : null;
  }
}
 
Example 2
Source File: LocalVideoThumbnailProducer.java    From fresco with MIT License 6 votes vote down vote up
@Nullable
private static Bitmap createThumbnailFromContentProvider(
    ContentResolver contentResolver, Uri uri) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
    try {
      ParcelFileDescriptor videoFile = contentResolver.openFileDescriptor(uri, "r");
      MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
      mediaMetadataRetriever.setDataSource(videoFile.getFileDescriptor());
      return mediaMetadataRetriever.getFrameAtTime(-1);
    } catch (FileNotFoundException e) {
      return null;
    }
  } else {
    return null;
  }
}
 
Example 3
Source File: ExtractPicturesWorker.java    From GifAssistant with Apache License 2.0 6 votes vote down vote up
public static Bitmap extractBitmap(String videoPath, int second) {
	if (TextUtils.isEmpty(videoPath)) {
		logd("extractBitmap empty video path");
		return null;
	}

	MediaMetadataRetriever retriever = new MediaMetadataRetriever();
	retriever.setDataSource(videoPath);
	// 取得视频的长度(单位为毫秒)
	String time = retriever
			.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
	// 取得视频的长度(单位为秒)
	int total = Integer.valueOf(time) / 1000;

	if (second < 0 || second > total) {
		loge("unavalible second(" + second + "), total(" + total + ")");
		return null;
	}

	Bitmap bitmap = retriever.getFrameAtTime(second * 1000 * 1000,
			MediaMetadataRetriever.OPTION_CLOSEST_SYNC);

	return bitmap;
}
 
Example 4
Source File: Util.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static int[] getVideoDimensions(String path) throws FileNotFoundException {
    File file = new File(path);
    if (!file.exists()) {
        throw new FileNotFoundException();
    }

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {
        retriever.setDataSource(path);
        Bitmap bitmap = retriever.getFrameAtTime();

        int[] dimensions = new int[2];

        if (bitmap != null) {
            dimensions[0] = bitmap.getWidth() > 0 ? bitmap.getWidth() : 1;
            dimensions[1] = bitmap.getHeight() > 0 ? bitmap.getHeight() : 1;
        }
        return dimensions;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new int[]{1, 1};
}
 
Example 5
Source File: VideoThumbnailFetcher.java    From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public InputStream loadData(Priority priority) throws Exception {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        Log.d("VideoThumbnailFetcher", "model.path= " + model.path);
        retriever.setDataSource(model.path, new HashMap<String, String>());
        Bitmap bitmap = retriever.getFrameAtTime();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] picture = stream.toByteArray();
        if (picture != null) {
            return new ByteArrayInputStream(picture);
        } else {
            return fallback(model.path);
        }
    } finally {
        retriever.release();
    }
}
 
Example 6
Source File: BitmapExtractor.java    From styT with Apache License 2.0 6 votes vote down vote up
public List<Bitmap> createBitmaps(String path)
   {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(path);
double inc = US_OF_S / fps;

for (double i = begin * US_OF_S; i < end * US_OF_S; i += inc)
{
    Bitmap frame = mmr.getFrameAtTime((long) i, MediaMetadataRetriever.OPTION_CLOSEST);
    if (frame != null)
    {
	bitmaps.add(scale(frame));
    }
}

return bitmaps;
   }
 
Example 7
Source File: FilmstripItemUtils.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the thumbnail of a video.
 *
 * @param path The path to the video file.
 * @return {@code null} if the loading failed.
 */
public static Bitmap loadVideoThumbnail(String path)
{
    Bitmap bitmap = null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try
    {
        retriever.setDataSource(path);
        byte[] data = retriever.getEmbeddedPicture();
        if (data != null)
        {
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        }
        if (bitmap == null)
        {
            bitmap = retriever.getFrameAtTime();
        }
    } catch (IllegalArgumentException e)
    {
        Log.e(TAG, "MediaMetadataRetriever.setDataSource() fail:" + e.getMessage());
    }
    retriever.release();
    return bitmap;
}
 
Example 8
Source File: FileBackend.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private static Dimensions getVideoDimensionsOfFrame(MediaMetadataRetriever mediaMetadataRetriever) {
    Bitmap bitmap = null;
    try {
        bitmap = mediaMetadataRetriever.getFrameAtTime();
        return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
    } catch (Exception e) {
        return null;
    } finally {
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}
 
Example 9
Source File: FileBackend.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap getVideoPreview(final File file, final int size) {
    final MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    Bitmap frame;
    try {
        metadataRetriever.setDataSource(file.getAbsolutePath());
        frame = metadataRetriever.getFrameAtTime(0);
        metadataRetriever.release();
        frame = resize(frame, size);
    } catch (IOException | RuntimeException e) {
        frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        frame.eraseColor(0xff000000);
    }
    drawOverlay(frame, paintOverlayBlack(frame) ? R.drawable.play_video_black : R.drawable.play_video_white, 0.75f);
    return frame;
}
 
Example 10
Source File: FileTools.java    From AndroidDownload with Apache License 2.0 5 votes vote down vote up
public static Bitmap getVideoThumbnail(String videoPath) {
    if(!new File(videoPath).exists()) return null;
    MediaMetadataRetriever media=new MediaMetadataRetriever();
    media.setDataSource(videoPath);
    Bitmap bitmap = media.getFrameAtTime();
    return bitmap;
}
 
Example 11
Source File: FileBackend.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap cropCenterSquareVideo(Uri uri, int size) {
    MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    Bitmap frame;
    try {
        metadataRetriever.setDataSource(mXmppConnectionService, uri);
        frame = metadataRetriever.getFrameAtTime(0);
        metadataRetriever.release();
        return cropCenterSquare(frame, size);
    } catch (Exception e) {
        frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        frame.eraseColor(0xff000000);
        return frame;
    }
}
 
Example 12
Source File: FileBackend.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private static Dimensions getVideoDimensionsOfFrame(MediaMetadataRetriever mediaMetadataRetriever) {
    Bitmap bitmap = null;
    try {
        bitmap = mediaMetadataRetriever.getFrameAtTime();
        return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
    } catch (Exception e) {
        return null;
    } finally {
        if (bitmap != null) {
            bitmap.recycle();
            ;
        }
    }
}
 
Example 13
Source File: UriUtils.java    From cloudinary_android with MIT License 5 votes vote down vote up
/**
 * Get the first frame of a video
 * @param context Android context.
 * @param uri Uri of the video file.
 * @return First frame of the video.
 */
public static Bitmap getVideoThumbnail(Context context, Uri uri) {
    MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(context, uri);

    return  mediaMetadataRetriever.getFrameAtTime(1); // TODO: Get first frame or the default?
}
 
Example 14
Source File: FileBackend.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap cropCenterSquareVideo(Uri uri, int size) {
    MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    Bitmap frame;
    try {
        metadataRetriever.setDataSource(mXmppConnectionService, uri);
        frame = metadataRetriever.getFrameAtTime(0);
        metadataRetriever.release();
        return cropCenterSquare(frame, size);
    } catch (Exception e) {
        frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        frame.eraseColor(0xff000000);
        return frame;
    }
}
 
Example 15
Source File: FermiPlayerUtils.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public static Bitmap createVideoThumbnail(String filePath, int offsetMillSecond) {
    filePath = filePath.replace("file://", "");
    if (VERSION.SDK_INT < 14) {
        return createVideoThumbnail(filePath);
    }
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(filePath);
    return retriever.getFrameAtTime((long) (offsetMillSecond * 1000), 2);
}
 
Example 16
Source File: AttachmentUploadJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private @Nullable String getVideoBlurHash(@NonNull Attachment attachment) throws IOException {
  if (attachment.getThumbnailUri() != null) {
    return BlurHashEncoder.encode(PartAuthority.getAttachmentStream(context, attachment.getThumbnailUri()));
  }

  if (attachment.getBlurHash() != null) return attachment.getBlurHash().getHash();

  if (Build.VERSION.SDK_INT < 23) {
    Log.w(TAG, "Video thumbnails not supported...");
    return null;
  }

  try (MediaDataSource dataSource = DatabaseFactory.getAttachmentDatabase(context).mediaDataSourceFor(attachmentId)) {
    if (dataSource == null) return null;

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    MediaMetadataRetrieverUtil.setDataSource(retriever, dataSource);

    Bitmap bitmap = retriever.getFrameAtTime(1000);

    if (bitmap != null) {
      Bitmap thumb = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
      bitmap.recycle();

      Log.i(TAG, "Generated video thumbnail...");
      String hash = BlurHashEncoder.encode(thumb);
      thumb.recycle();

      return hash;
    } else {
      return null;
    }
  }
}
 
Example 17
Source File: PBitmapUtils.java    From YImagePicker with Apache License 2.0 4 votes vote down vote up
/**
 * 获取视频封面
 */
public static Bitmap getVideoThumb(String path) {
    MediaMetadataRetriever media = new MediaMetadataRetriever();
    media.setDataSource(path);
    return media.getFrameAtTime();
}
 
Example 18
Source File: SystemUtil.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
public static Bitmap getVideoThumbnail(String videoPath, int width, int height,
                                       int kind) {
    MediaMetadataRetriever media = new MediaMetadataRetriever();
    media.setDataSource(videoPath);
    return media.getFrameAtTime();
}
 
Example 19
Source File: VideoThumbnailUtil.java    From MediaLoader with Apache License 2.0 4 votes vote down vote up
/**
 * get video thumb from path
 * @param path
 * @return
 */
public static Bitmap getVideoThumb(String path) {
    MediaMetadataRetriever media = new MediaMetadataRetriever();
    media.setDataSource(path);
    return media.getFrameAtTime();
}
 
Example 20
Source File: BitmapUtil.java    From Android with MIT License 4 votes vote down vote up
/**
 * Get video thumbnails
 * @param filepath
 * @return
 */
public static Bitmap thumbVideo(String filepath) {
    MediaMetadataRetriever media = new MediaMetadataRetriever();
    media.setDataSource(filepath);
    return media.getFrameAtTime();
}