配置文件代码可以使用MediaExtractor
根据API 文档,编码配置文件代码似乎仅适用于 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
告诉我。
作为参考,这里是MediaFormat
从Nougat 7.1.1
设备拍摄的视频轨道的快照。(注意:较低级别的 API 将返回较少的属性)
其他参考: