在Android上以编程方式获取MPEG-4视频流的配置文件

时间:2017-01-11 20:26:55

标签: android android-mediaplayer mpeg-4 mediaextractor android-mediacodec

有没有办法使用标准Android API获取MPEG-4视频文件profile-code

示例个人资料代码包括:baselinemainhigh等。

我不想在Android应用中包含ffmpeg二进制文件,只是为了获取此信息。

我在MediaCodecInfo.CodecProfileLevel课程中找到了个人资料列表的参考。任何人都可以确认这是否是我应该使用的那个?

以下是取自 MX Player 应用的视频信息的示例屏幕截图 Sample data with video profile

  • 更新#1:我查看了MX Player应用,看起来他们使用ffmpeg二进制文件,所以我可以肯定地说他们使用它来提取profile-code
  • 更新#2:我还查看了ExoPlayer v1 API,他们似乎也没有特定的API来获取此信息。

1 个答案:

答案 0 :(得分:2)

可以使用MediaExtractor

找到个人资料代码

基于API documentation,似乎编码个人资料代码仅可从API级别24(牛轧糖)及以上版本获得。

以下是我编写的用于提取个人资料代码的方法,可随意修改以满足您的需求。

/**
 * Get video profile-code from video file.
 *
 * @param videoFilePath Path of the video file.
 * @return One of predefined AVC profiles from {@link MediaCodecInfo.CodecProfileLevel} when found, or {@code -1} if
 * Android API level does not support extracting profile data.
 */
@TargetApi(21)
public int getVideoEncodingProfile(final String videoFilePath) {
    int videoProfileCode = -1;

    File inputFile = new File(videoFilePath);
    if (!inputFile.canRead()) {
        throw new RuntimeException("Unable to read " + inputFile);
    }

    MediaExtractor mediaExtractor = new MediaExtractor();
    // Initialize MediaExtractor and configure/extract video information
    try {
        mediaExtractor.setDataSource(inputFile.toString());
    } catch (IOException e) {
        Log.e(TAG, "Unable to set MediaExtractor source.", e);
        throw new RuntimeException("Unable to set source.");
    }

    MediaFormat videoMediaFormat = findVideoMediaFormat(mediaExtractor);
    // MediaCodecInfo.CodecProfileLevel of the video track
    if (videoMediaFormat != null && videoMediaFormat.containsKey(MediaFormat.KEY_PROFILE)) {
        videoProfileCode = videoMediaFormat.getInteger(MediaFormat.KEY_PROFILE);
    } else {
        // Current API level does not support encoding profile information.
        Log.w(TAG, "Video profile code is not supported by current API level.");
    }

    mediaExtractor.release();
    mediaExtractor = null;

    return videoProfileCode;
}

/**
 * Find video MediaFormat from MediaExtractor.
 *
 * @param mediaExtractor The MediaExtractor which is used to find video track.
 * @return MediaFormat for video track, or {@code null} when video track is not found.
 */
private MediaFormat findVideoMediaFormat(final MediaExtractor mediaExtractor) {
    MediaFormat videoTrackMediaFormat = null;
    int totalTracks = mediaExtractor.getTrackCount();
    for (int i = 0; i < totalTracks; i++) {
        MediaFormat trackFormat = mediaExtractor.getTrackFormat(i);
        if ((trackFormat.containsKey(MediaFormat.KEY_MIME)
                && trackFormat.getString(MediaFormat.KEY_MIME).contains("video"))
                || (trackFormat.containsKey(MediaFormat.KEY_WIDTH) && trackFormat.containsKey(MediaFormat.KEY_HEIGHT))
                ) {
            videoTrackMediaFormat = trackFormat;
            break;
        }
    }
    return videoTrackMediaFormat;
}

以下是有关如何使用它的示例代码。

String TAG = "DEBUG"; // Define your tag
int profileCode = getVideoEncodingProfile(videoInfo.getLocalVideoPath());

switch (profileCode) {
    case MediaCodecInfo.CodecProfileLevel.AVCProfileBaseline:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileBaseline");
        break;
    case MediaCodecInfo.CodecProfileLevel.AVCProfileMain:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileMain");
        break;
    case MediaCodecInfo.CodecProfileLevel.AVCProfileExtended:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileExtended");
        break;
    case MediaCodecInfo.CodecProfileLevel.AVCProfileHigh:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileHigh");
        break;
    case MediaCodecInfo.CodecProfileLevel.AVCProfileHigh10:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileHigh10");
        break;
    case MediaCodecInfo.CodecProfileLevel.AVCProfileHigh422:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileHigh422");
        break;
    case MediaCodecInfo.CodecProfileLevel.AVCProfileHigh444:
        Log.d(TAG, "getVideoEncodingProfile() returned AVCProfileHigh444");
        break;
    default:
        Log.d(TAG, "getVideoEncodingProfile() returned unsupported profile code or code not found.");

}

希望它有所帮助。如果您有其他方式支持至少 Jelly Bean API级16,请告诉我。

供参考,以下是取自 Nougat MediaFormat设备的视频曲目7.1.1的快照。 (注意:较低级API将返回较少的属性)

MediaFormat Hash Map - Snapshot

其他参考资料: