uni-app IOS uni.compressVideo在指定bitrate、fps的情况下height、width、orientation发生变化

uni-app IOS uni.compressVideo在指定bitrate、fps的情况下height、width、orientation发生变化

示例代码:

uni.chooseVideo({  
    sourceType: ['camera', 'album'],  
    compressed:false,  
    maxDuration:30,  
    success: (res) => {  
        uni.getVideoInfo({  
            src:res.tempFilePath,  
            success(video){  
                console.log(video)  
                //输出内容  
                // {  
                //  "orientation": "right",  
                //  "type": "video/quicktime",  
                //  "duration": 16,  
                //  "size": 30509108,  
                //  "height": 1920,  
                //  "width": 1080,  
                //  "fps": 29.97744,  
                //  "bitrate": 15631,  
                //  "errMsg": "getVideoInfo:ok"  
                // }  
            }  
        })  
        uni.compressVideo({  
            src:res.tempFilePath,  
            bitrate:10000,  
            fps:30,  
            resolution:1,  
            success(e) {  
                uni.getVideoInfo({  
                    src:e.tempFilePath,  
                    success(video){  
                        console.log(video)  
                        //输出内容  
                        // {  
                        //  "orientation": "up",  
                        //  "type": "video/mp4",  
                        //  "duration": 16,  
                        //  "size": 10955605,  
                        //  "height": 1080,  
                        //  "width": 1920,  
                        //  "fps": 29.93562,  
                        //  "bitrate": 5541,  
                        //  "errMsg": "getVideoInfo:ok"  
                        // }  
                    }  
                })  
            },  
        })  
    },  
});

操作步骤:

预期结果:

{  
    "orientation": "right",  
    "type": "video/quicktime",  
    "duration": 16,  
    "size": 30509108,  
    "height": 1920,  
    "width": 1080,  
    "fps": 29.97744,  
    "bitrate": 15631,  
    "errMsg": "getVideoInfo:ok"  
}

实际结果:

{  
    "orientation": "up",  
    "type": "video/mp4",  
    "duration": 16,  
    "size": 10955605,  
    "height": 1080,  
    "width": 1920,  
    "fps": 29.93562,  
    "bitrate": 5541,  
    "errMsg": "getVideoInfo:ok"  
}

bug描述:

IOS uni.compressVideo 在指定 bitrate、fps、和 resolution的情况下 竖屏视屏在压缩后. 视频的height、width、orientation 发生变化 竖屏视频添加黑边


更多关于uni-app IOS uni.compressVideo在指定bitrate、fps的情况下height、width、orientation发生变化的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app IOS uni.compressVideo在指定bitrate、fps的情况下height、width、orientation发生变化的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个iOS平台下uni.compressVideo在处理竖屏视频时的已知问题。当压缩带有旋转元信息(orientation metadata)的竖屏视频时,iOS系统可能会将视频重新编码为横屏格式并添加黑边,导致width/height交换和orientation变为"up"。

问题核心在于iOS的AVFoundation框架处理视频旋转时,会将旋转信息写入元数据而非实际旋转像素。压缩过程中,某些编码设置可能导致旋转元数据丢失或被重置。

临时解决方案:

  1. 压缩后手动修正方向:检测压缩后视频的orientation,如果发生异常,使用uni.createVideoContext()配合CSS旋转进行显示修正。

  2. 使用resolution参数控制:尝试设置resolution为更接近原始视频的比例值(如0.75对应720P),避免使用默认值。

  3. 分平台处理:iOS端可考虑使用条件编译,采用第三方插件或原生模块进行视频压缩。

  4. 压缩前预处理:对于竖屏视频,可先通过canvas绘制等方式进行方向修正,再进行压缩。

代码调整示例:

// 压缩后检测并修正显示
if (video.orientation === 'up' && video.width > video.height) {
  // 实际为竖屏视频被转为横屏
  // 在显示时通过CSS旋转90度
}
回到顶部