【问题标题】:How to get video rotation angle when metadata rotation is not right?元数据旋转不正确时如何获取视频旋转角度?
【发布时间】:2017-08-19 21:19:43
【问题描述】:

我有一个视频文件,我从下面的代码中得到它的分辨率(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应用程序中总是无法正确显示。

有趣的是,一些 3rd-party 视频播放器(例如 VLC)可以检测到此视频文件的实际旋转,并且其显示也正常,他们是如何做到的?

【问题讨论】:

  • 不,最后我忽略了这个案子。

标签: android video ffmpeg vlc android-mediacodec


【解决方案1】:

为时已晚,但可能会对未来的访客有所帮助。

以下行将返回视频的旋转。四个可能的值是 0,90,180,270。

rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));

如果旋转为 90 或 270,宽度和高度将被调换。

Android MediaMetadataRetriever wrong video height and width

如果旋转为 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 的版本上运行的设备呢?

回答:我不知道。但上面的代码适用于大多数设备。

以下是最佳答案:https://stackoverflow.com/a/56337044/8199772

【讨论】:

  • 就像我在回答中提到的那样。这不是一个非常可靠的方法。您肯定会遇到视频没有可用元数据的情况。最可靠的方法是从视频中获取Bitmap 并获取Bitmap 的尺寸,如我在此处的答案所示 - stackoverflow.com/a/56337044/8199772
  • 另外,三星设备的方向设置也不同。因此,如果您使用它,您最终将不得不为不同的移动提供商处理轮换。
  • 我提到了你的答案链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 2014-03-17
  • 2023-04-05
  • 2021-11-29
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多