有没有办法使用标准Android API获取MPEG-4视频文件profile-code?
示例个人资料代码包括:baseline
,main
,high
等。
我不想在Android应用中包含ffmpeg
二进制文件,只是为了获取此信息。
我在MediaCodecInfo.CodecProfileLevel课程中找到了个人资料列表的参考。任何人都可以确认这是否是我应该使用的那个?
以下是取自 MX Player 应用的视频信息的示例屏幕截图
ffmpeg
二进制文件,所以我可以肯定地说他们使用它来提取profile-code
。 答案 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将返回较少的属性)
其他参考资料: