可以使用MediaExtractor
找到配置文件代码。
根据API文档,似乎编码配置文件代码仅在API级别24(Nougat)及以上版本中可用。
以下是我编写的提取配置文件代码的方法,可以根据需要进行修改。
@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();
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);
if (videoMediaFormat != null && videoMediaFormat.containsKey(MediaFormat.KEY_PROFILE)) {
videoProfileCode = videoMediaFormat.getInteger(MediaFormat.KEY_PROFILE);
} else {
Log.w(TAG, "Video profile code is not supported by current API level.");
}
mediaExtractor.release();
mediaExtractor = null;
return videoProfileCode;
}
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";
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 7.1.1
设备中获取的视频轨道的MediaFormat
快照。(注意:较低级别的API将返回较少的属性)
![MediaFormat Hash Map - Snapshot](https://istack.dev59.com/T8u68.webp)
其他参考资料: