Example usage for android.media MediaMetadataRetriever getFrameAtTime

List of usage examples for android.media MediaMetadataRetriever getFrameAtTime

Introduction

In this page you can find the example usage for android.media MediaMetadataRetriever getFrameAtTime.

Prototype

public Bitmap getFrameAtTime(long timeUs) 

Source Link

Document

Call this method after setDataSource().

Usage

From source file:Main.java

public static Bitmap extractFromMediaMetadataRetriever(String filePath) {
    Bitmap bitmap = null;//from   w w  w . j ava  2 s .com
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(filePath);
        bitmap = retriever.getFrameAtTime(-1);
        return bitmap;
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
        }
    }
    return null;
}

From source file:Main.java

public static Bitmap getVideoScreenshotBitmap(Context context, String videoPath) {
    if (TextUtils.isEmpty(videoPath) || !new File(videoPath).exists()) {
        return null;
    }/*from   www  .  j a va  2  s. c om*/
    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(videoPath);
    bitmap = mediaMetadataRetriever.getFrameAtTime(0);
    return bitmap;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap createVideoThumbnail(String filePath, int kind) {
    Bitmap bitmap = null;// w  w w  . ja v a 2  s .  c o  m
    if (Build.VERSION.SDK_INT < 10) {
        // This peace of code is for compatibility with android 8 and 9.
        return android.media.ThumbnailUtils.createVideoThumbnail(filePath, kind);
    }

    // MediaMetadataRetriever is not available for Android version less than 10
    // but we need to use it in order to get first frame of the video for thumbnail.
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {
        retriever.setDataSource(filePath);
        bitmap = retriever.getFrameAtTime(0);
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
            Log.w("ThumbnailUtils", "MediaMetadataRetriever failed with exception: " + ex);
        }
    }

    if (bitmap == null)
        return null;

    if (kind == Images.Thumbnails.MINI_KIND) {
        // Scale down the bitmap if it's too large.
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int max = Math.max(width, height);
        if (max > 512) {
            float scale = 512f / max;
            int w = Math.round(scale * width);
            int h = Math.round(scale * height);
            bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
        }
    } else if (kind == Images.Thumbnails.MICRO_KIND) {

        bitmap = android.media.ThumbnailUtils.extractThumbnail(bitmap, TARGET_SIZE_MICRO_THUMBNAIL,
                TARGET_SIZE_MICRO_THUMBNAIL, OPTIONS_RECYCLE_INPUT);
    }

    return bitmap;
}

From source file:avreye.mytarotadvisor.utils.Util.java

public static Bitmap getThumbnailfromVideoURL(String videoPath) throws Throwable {
    Bitmap bitmap = null;//from  www.j  a va  2s.c  om

    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime(100);
    } catch (Exception e) {

        e.printStackTrace();
        throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

From source file:com.almalence.googsharing.Thumbnail.java

private static Bitmap createVideoThumbnail(String filePath, FileDescriptor fd, int targetWidth) {
    Bitmap bitmap = null;//w ww.  ja  v  a 2s. c  o  m
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        if (filePath != null) {
            retriever.setDataSource(filePath);
        } else {
            retriever.setDataSource(fd);
        }
        bitmap = retriever.getFrameAtTime(-1);
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
        }
    }
    if (bitmap == null)
        return null;

    // Scale down the bitmap if it is bigger than we need.
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width > targetWidth) {
        float scale = (float) targetWidth / width;
        int w = Math.round(scale * width);
        int h = Math.round(scale * height);
        bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
    }
    return bitmap;
}

From source file:com.netcompss.ffmpeg4android_client.BaseWizard.java

public String createVideoThumbnail(String fDescriptor) {
    Bitmap thumb = null;/*from  www . j  a  va  2 s . co m*/
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(fDescriptor);
        thumb = retriever.getFrameAtTime(-1);
        int width = thumb.getWidth();
        int height = thumb.getHeight();
        int max = Math.max(width, height);
        if (max > 512) {
            float scale = 512f / max;
            int w = Math.round(scale * width);
            int h = Math.round(scale * height);
            thumb = Bitmap.createScaledBitmap(thumb, w, h, true);
        }

        FileOutputStream fos = null;
        String extr = Environment.getExternalStorageDirectory().toString();
        File mFolder = new File(extr + "/CHURCH");
        if (!mFolder.exists()) {
            mFolder.mkdir();
        } else {
            mFolder.delete();
            mFolder.mkdir();
        }

        String s = "churchthumbs.png";

        video_thumbpath = new File(mFolder.getAbsolutePath(), s);
        returnPath = video_thumbpath.getAbsolutePath();

        try {
            fos = new FileOutputStream(video_thumbpath);
            thumb.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
        Log.e("e", "Failed to create video thumbnail for file description: " + fDescriptor.toString());
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
        Log.e("e", "Failed to create video thumbnail for file description: " + fDescriptor.toString());
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
        }
    }
    return returnPath;
}

From source file:com.sebible.cordova.videosnapshot.VideoSnapshot.java

/**
 * Take snapshots of a video file//from w w w.  ja v a  2  s  . c o  m
 *
 * @param source path of the file
 * @param count of snapshots that are gonna be taken
 */
private void snapshot(final JSONObject options) {
    final CallbackContext context = this.callbackContext;
    this.cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            try {
                int count = options.optInt("count", 1);
                int countPerMinute = options.optInt("countPerMinute", 0);
                int quality = options.optInt("quality", 90);
                String source = options.optString("source", "");
                Boolean timestamp = options.optBoolean("timeStamp", true);
                String prefix = options.optString("prefix", "");
                int textSize = options.optInt("textSize", 48);

                if (source.isEmpty()) {
                    throw new Exception("No source provided");
                }

                JSONObject obj = new JSONObject();
                obj.put("result", false);
                JSONArray results = new JSONArray();

                Log.i("snapshot", "Got source: " + source);
                Uri p = Uri.parse(source);
                String filename = p.getLastPathSegment();

                FileInputStream in = new FileInputStream(new File(p.getPath()));
                retriever.setDataSource(in.getFD());
                String tmp = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                long duration = Long.parseLong(tmp);
                if (countPerMinute > 0) {
                    count = (int) (countPerMinute * duration / (60 * 1000));
                }
                if (count < 1) {
                    count = 1;
                }
                long delta = duration / (count + 1); // Start at duration * 1 and ends at duration * count
                if (delta < 1000) { // min 1s
                    delta = 1000;
                }

                Log.i("snapshot", "duration:" + duration + " delta:" + delta);

                File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                for (int i = 1; delta * i < duration && i <= count; i++) {
                    String filename2 = filename.replace('.', '_') + "-snapshot" + i + ".jpg";
                    File dest = new File(storage, filename2);
                    if (!storage.exists() && !storage.mkdirs()) {
                        throw new Exception("Unable to access storage:" + storage.getPath());
                    }
                    FileOutputStream out = new FileOutputStream(dest);
                    Bitmap bm = retriever.getFrameAtTime(i * delta * 1000);
                    if (timestamp) {
                        drawTimestamp(bm, prefix, delta * i, textSize);
                    }
                    bm.compress(Bitmap.CompressFormat.JPEG, quality, out);
                    out.flush();
                    out.close();
                    results.put(dest.getAbsolutePath());
                }

                obj.put("result", true);
                obj.put("snapshots", results);
                context.success(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
                Log.e("snapshot", "Exception:", ex);
                fail("Exception: " + ex.toString());
            } finally {
                try {
                    retriever.release();
                } catch (RuntimeException ex) {
                }
            }
        }
    });
}