AVURLAsset naturalSize 错误大小 错误方向

最近使用IPHONE录制4K视频后读取相关信息出现错误,导致转MP4时音频错误播放1秒即停止播放。

经过排查,原为视频大小取值错误导致转MP4异常。


+ (CGSize)videoSizeInAssets:(AVURLAsset *)asset
{
    AVAssetTrack *videoTrack = nil;
    if ([[asset tracksWithMediaType:AVMediaTypeVideo] count] != 0)
    {
        videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    }
    
    CGSize naturalSize = [videoTrack naturalSize];
    CGAffineTransform transform = videoTrack.preferredTransform;
    // Workaround radar 31928389, see https://github.com/rs/SDAVAssetExportSession/pull/70 for more info
    if (transform.ty == -560) {
        transform.ty = 0;
    }

    if (transform.tx == -560) {
        transform.tx = 0;
    }

    CGFloat videoAngleInDegree  = atan2(transform.b, transform.a) * 180 / M_PI;
    if (videoAngleInDegree == 90 || videoAngleInDegree == -90) 
    {
        CGFloat width = naturalSize.width;
        naturalSize.width = naturalSize.height;
        naturalSize.height = width;
    }
    
    return naturalSize;
}

使用方法:

NSDictionary *dic = @{AVURLAssetPreferPreciseDurationAndTimingKey:@(YES)};
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:movUrl options:dic];
    
CGSize vs = [SDAVAssetExportSession videoSizeInAssets:avAsset];

你可能感兴趣的:(ios)