android - 当元数据旋转不正确时如何获取视频旋转角度?

标签 android video ffmpeg vlc android-mediacodec

我有一个视频文件,我从以下代码中获取其分辨率(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,宽度和高度将被调换。

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

关于android - 当元数据旋转不正确时如何获取视频旋转角度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041799/

相关文章:

java - 如何清理生成的数据绑定(bind)文件?

android - Firebase异常 : [ DEVELOPMENT_MODE_MISMATCH: Non-development mode Verification Proof given in development mode request

AndroidStudio aws django : HttpUrlConnection not working

ffmpeg - 使用 ffmpeg 平移和缩放多个图像

c# - 在 C# 应用程序中显示 tcp 视频流(来自 FFPLAY/FFMPEG)

FFMPEG 保存多个帧并用作电影

android - 文本在 Android 操作系统中的确切位置呈现?

css - 使用 CSS 在固定高度和固定宽度布局中保持视频的纵横比

image - 如何在 MATLAB 中从视频中选择特定帧?

css - HTML5 视频容器比视频稍大