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

The following examples show how to use android.media.MediaMetadataRetriever#setDataSource() . 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: MediaExtractorPlugin.java    From media-for-mobile with Apache License 2.0 6 votes vote down vote up
@Override
public int getRotation() {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    if (path != null) {
        retriever.setDataSource(path);
    } else if (fileDescriptor != null) {
        retriever.setDataSource(fileDescriptor);
    } else if (uri != null) {
        retriever.setDataSource(context, android.net.Uri.parse(uri.getString()));
    } else {
        throw new IllegalStateException("File not set");
    }
    String rotation = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
    retriever.release();
    return Integer.parseInt(rotation);
}
 
Example 2
Source File: VideoUtils.java    From tysq-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取本地视频长度
 *
 * @param videoPath 视频路径
 * @return 视频时长,如果获取异常则返回-1
 */
public static int getLocalVideoDuration(String videoPath) {
    int duration;

    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(videoPath);
        String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        duration = Integer.parseInt(durationStr);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }

    return duration;
}
 
Example 3
Source File: AudioCoverUtil.java    From MediaLoader with Apache License 2.0 6 votes vote down vote up
/**
 * get audio cover
 * @param filePath
 * @return
 */
public static Bitmap createAlbumArt(String filePath) {
    Bitmap bitmap = null;
    //能够获取多媒体文件元数据的类
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(filePath); //设置数据源
        byte[] bytes = retriever.getEmbeddedPicture(); //得到字节型数据
        if(bytes!=null){
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); //转换为图片
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return bitmap;
}
 
Example 4
Source File: AndroidMessenger.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
public void sendVideo(Peer peer, String fullFilePath, String fileName) {
    try {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(fullFilePath);
        int duration = (int) (Long.parseLong(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) / 1000L);
        Bitmap img = retriever.getFrameAtTime(0);
        int width = img.getWidth();
        int height = img.getHeight();
        Bitmap smallThumb = ImageHelper.scaleFit(img, 90, 90);
        byte[] smallThumbData = ImageHelper.save(smallThumb);

        FastThumb thumb = new FastThumb(smallThumb.getWidth(), smallThumb.getHeight(), smallThumbData);

        sendVideo(peer, fileName, width, height, duration, thumb, fullFilePath);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: VideoTimelinePlayView.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void setVideoPath(String path, float left, float right) {
    destroy();
    mediaMetadataRetriever = new MediaMetadataRetriever();
    progressLeft = left;
    progressRight = right;
    try {
        mediaMetadataRetriever.setDataSource(path);
        String duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        videoLength = Long.parseLong(duration);
    } catch (Exception e) {
        FileLog.e(e);
    }
    invalidate();
}
 
Example 6
Source File: PlaybackOverlayFragment.java    From android-tv-leanback with Apache License 2.0 5 votes vote down vote up
private int getDuration() {
    Log.d(TAG, "getDuration()");
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        Log.d(TAG, "mVideo.getContentUrl()=" + mVideo.getContentUrl());
        mmr.setDataSource(mVideo.getContentUrl(), new HashMap<String, String>());
    } else {
        mmr.setDataSource(mVideo.getContentUrl());
    }
    String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    mDuration = Long.parseLong(time);
    Log.d(TAG, "mDuration=" + mDuration);

    return (int) mDuration;
}
 
Example 7
Source File: VideoTimelineView.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void setVideoPath(String path) {
    destroy();
    mediaMetadataRetriever = new MediaMetadataRetriever();
    progressLeft = 0.0f;
    progressRight = 1.0f;
    try {
        mediaMetadataRetriever.setDataSource(path);
        String duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        videoLength = Long.parseLong(duration);
    } catch (Exception e) {
        FileLog.e(e);
    }
    invalidate();
}
 
Example 8
Source File: MediaStoreUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取本地视频时长
 * @param uri Video Uri content://
 * @return 本地视频时长
 */
public static long getVideoDuration(final Uri uri) {
    if (uri == null) return 0;
    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(DevUtils.getContext(), uri);
        return Long.parseLong(mmr.extractMetadata
                (MediaMetadataRetriever.METADATA_KEY_DURATION));
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getVideoDuration");
        return 0;
    }
}
 
Example 9
Source File: MediaUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
/**
 * get Local video width or height
 *
 * @return
 */
public static int[] getVideoSizeForUri(Context context, Uri uri) {
    int[] size = new int[2];
    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(context, uri);
        size[0] = ValueOf.toInt(mmr.extractMetadata
                (MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
        size[1] = ValueOf.toInt(mmr.extractMetadata
                (MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}
 
Example 10
Source File: MediaUtil.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
public static boolean createVideoThumbnailIfNeeded(Context context, Uri dataUri, Uri thumbnailUri, ThumbnailSize retWh) {
  boolean success = false;
  try {
    File thumbnailFile = new File(thumbnailUri.getPath());
    File dataFile = new File(dataUri.getPath());
    if (!thumbnailFile.exists() || dataFile.lastModified()>thumbnailFile.lastModified()) {
      Bitmap bitmap = null;

      MediaMetadataRetriever retriever = new MediaMetadataRetriever();
      retriever.setDataSource(context, dataUri);
      bitmap = retriever.getFrameAtTime(-1);
      if (retWh!=null) {
        retWh.width = bitmap.getWidth();
        retWh.height = bitmap.getHeight();
      }
      retriever.release();

      if (bitmap != null) {
        FileOutputStream out = new FileOutputStream(thumbnailFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        success = true;
      }
    }
  }
  catch (Exception e) {
    e.printStackTrace();
  }
  return success;
}
 
Example 11
Source File: MediaUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
/**
 * get Local video duration
 *
 * @return
 */
private static long getLocalDuration(Context context, Uri uri) {
    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(context, uri);
        return Long.parseLong(mmr.extractMetadata
                (MediaMetadataRetriever.METADATA_KEY_DURATION));
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}
 
Example 12
Source File: VideoTimelinePlayView.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public void setVideoPath(String path) {
    destroy();
    mediaMetadataRetriever = new MediaMetadataRetriever();
    progressLeft = 0.0f;
    progressRight = 1.0f;
    try {
        mediaMetadataRetriever.setDataSource(path);
        String duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        videoLength = Long.parseLong(duration);
    } catch (Exception e) {
        FileLog.e(e);
    }
    invalidate();
}
 
Example 13
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 14
Source File: FileBackend.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile {
    MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    try {
        mediaMetadataRetriever.setDataSource(context, uri);
    } catch (RuntimeException e) {
        throw new NotAVideoFile(e);
    }
    return getVideoDimensions(mediaMetadataRetriever);
}
 
Example 15
Source File: Utils.java    From alltv with MIT License 5 votes vote down vote up
public static long getDuration(String videoUrl) {
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        mmr.setDataSource(videoUrl, new HashMap<String, String>());
    } else {
        mmr.setDataSource(videoUrl);
    }
    return Long.parseLong(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
}
 
Example 16
Source File: InMemoryTranscoder.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param upperSizeLimit A upper size to transcode to. The actual output size can be up to 10% smaller.
 */
public InMemoryTranscoder(@NonNull Context context, @NonNull MediaDataSource dataSource, @Nullable Options options, long upperSizeLimit) throws IOException, VideoSourceException {
  this.context    = context;
  this.dataSource = dataSource;
  this.options    = options;

  final MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
  try {
    mediaMetadataRetriever.setDataSource(dataSource);
  } catch (RuntimeException e) {
    Log.w(TAG, "Unable to read datasource", e);
    throw new VideoSourceException("Unable to read datasource", e);
  }

  long upperSizeLimitWithMargin = (long) (upperSizeLimit / 1.1);

  this.inSize             = dataSource.getSize();
  this.duration           = getDuration(mediaMetadataRetriever);
  this.inputBitRate       = bitRate(inSize, duration);
  this.targetVideoBitRate = getTargetVideoBitRate(upperSizeLimitWithMargin, duration);
  this.upperSizeLimit     = upperSizeLimit;

  this.transcodeRequired = inputBitRate >= targetVideoBitRate * 1.2 || inSize > upperSizeLimit || containsLocation(mediaMetadataRetriever) || options != null;
  if (!transcodeRequired) {
    Log.i(TAG, "Video is within 20% of target bitrate, below the size limit, contained no location metadata or custom options.");
  }

  this.fileSizeEstimate   = (targetVideoBitRate + AUDIO_BITRATE) * duration / 8000;
  this.memoryFileEstimate = (long) (fileSizeEstimate * 1.1);
  this.outputFormat       = targetVideoBitRate < LOW_RES_TARGET_VIDEO_BITRATE
                            ? LOW_RES_OUTPUT_FORMAT
                            : OUTPUT_FORMAT;
}
 
Example 17
Source File: ViewHolderAudio.java    From explorer with Apache License 2.0 4 votes vote down vote up
@Override
protected void bindIcon(File file, Boolean selected) {

    try {

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();

        retriever.setDataSource(file.getPath());

        Glide.with(context).load(retriever.getEmbeddedPicture()).into(image);
    }
    catch (Exception e) {

        image.setImageResource(R.drawable.ic_audio);
    }
}
 
Example 18
Source File: InfoUtils.java    From Gallery-example with GNU General Public License v3.0 4 votes vote down vote up
static String fileResolution(String url, int which) {

        String[] VIDEO_EXTENSIONS = {"mp4", "avi", "mpg", "mkv", "webm", "flv",
                "wmv", "mov", "qt", "m4p", "m4v", "mpeg", "mp2",
                "m2v", "3gp", "3g2", "f4v", "f4p", "f4a", "f4b"};

        String resolution = "";

        if (stringContainsItemFromList(url, VIDEO_EXTENSIONS)) {
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(url);

            int videoWidth = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
            int videoHeight = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));

            switch (which) {
                case 0:
                    resolution = String.valueOf(videoWidth);
                    break;
                case 1:
                    resolution = String.valueOf(videoHeight);
                    break;
            }

            retriever.release();

        } else {

            Uri uri = Uri.parse(url);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options);
            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;

            switch (which) {
                case 0:
                    resolution = String.valueOf(imageWidth);
                    break;
                case 1:
                    resolution = String.valueOf(imageHeight);
                    break;
            }

        }

        return resolution;
    }
 
Example 19
Source File: VideoSelectPreference.java    From VIA-AI with MIT License 4 votes vote down vote up
public void setVideoPath(String path) {
    boolean isValid = false;
    boolean isFileExist = false;
    mVideoPathValue = path;

    do {
        if(path == null || path == "") break;

        isFileExist = isFileExist(path);
        if(isFileExist) {
            try {
                Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);

                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(path);
                mWidth = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
                mHeight = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
                this.setSummary("    Video Path : " + path + " < " + mWidth + " x " + mHeight + " >");
                persistString((String) path);
                if (thumb != null) {
                    setPreviewBitmap(thumb);
                } else {
                    setPreviewResource(mContext.getResources().getDrawable(R.drawable.icon_unknown_video_fmt));
                }
                isValid = true;
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    } while(false);

    if(!isValid) {
        if(!isFileExist) {
            this.setSummary("    File not exist or permission denied. Click to select a new \".mp4 \" file...");
        }
        else {
            this.setSummary("    Fail to parse bitmap in this file. Click to select a new \".mp4 \" file...");
        }
        persistString("");
        if(isEnabled()) {
            this.setBackgroundColor(Color.rgb(170, 30, 30));
        }
        else {
            this.setBackgroundColor(Color.TRANSPARENT);
        }
    }
    else {
        this.setBackgroundColor(Color.TRANSPARENT);
    }

    if(mView != null) {
        super.onBindView(mView);    // use base class's oBindView to avoid recursive.
    }
}
 
Example 20
Source File: FileUtils.java    From explorer with Apache License 2.0 3 votes vote down vote up
public static String getArtist(File file) {

        try {

            MediaMetadataRetriever retriever = new MediaMetadataRetriever();

            retriever.setDataSource(file.getPath());

            return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        }
        catch (Exception e) {

            return null;
        }
    }