我有一个视频文件,我从以下代码中获取其分辨率(3840x2160)和旋转(0):
MediaMetadataRetriever retr = new MediaMetadataRetriever();
retr.setDataSource(mVideoFile);
String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
String rotation = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
但是实际上,视频旋转角度(以度为单位)不正确,它应该是 90、2160x3840 分辨率,所以我的视频总是无法在我的 Android 应用程序中正确显示。
有趣的是,一些第三方视频播放器(例如VLC)可以检测到该视频文件的实际旋转,并且显示也正常,他们是如何做到的?
最佳答案
为时已晚,但可能会对 future 的访问者有所帮助。
以下行将返回视频的旋转。四个可能的值是 0,90,180,270。
rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
如果旋转为 90 或 270,宽度和高度将被调换。
如果旋转为 90 或 270,请滑动宽度和高度。
if(rotation == 90 || rotation == 270)
{
int swipe = width;
width = height;
height = swipe;
}
以下是完整代码。我和MXPlayer比较结果是一样的。
public static String getVideoResolution(File file)
{
try
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(file.getPath());
int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
int rotation = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
}
retriever.release();
if(rotation != 0)
{
if(rotation == 90 || rotation == 270)
{
int swipe = width;
width = height;
height = swipe;
}
}
return width + " x " + height;
}
catch (Exception e)
{
e.printStackTrace();
return "Information not found.";
}
}
以下是设置屏幕方向的代码。
int width;
int height;
int rotation = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
}
retriever.release();
if(rotation != 0)
{
if(rotation == 90 || rotation == 270)
{
int swipe = width;
width = height;
height = swipe;
}
}
this.setRequestedOrientation(width < height ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
catch (Exception e){
e.printStackTrace();
}
在 Build.VERSION_CODES.JELLY_BEAN_MR1 以下版本上运行的设备怎么样?
答:我不知道。但上面的代码适用于大多数设备。
关于android - 当元数据旋转不正确时如何获取视频旋转角度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041799/