本文介绍了AVAssetTrack的preferredTransform有时似乎是错误的.如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVAssetExportSession 在iOS应用中导出视频.为了以正确的方向呈现视频,我使用了 AVAssetTrack preferredTransform .对于某些源视频,此属性似乎具有错误的值,并且视频在结果中显示为偏移或完全黑色.我该如何解决?

I'm using AVAssetExportSession to export videos in an iOS app. To render the videos in their correct orientation, I'm using AVAssetTrack's preferredTransform. For some source videos, this property seems to have a wrong value, and the video appears offset or completely black in the result. How can I fix this?

推荐答案

preferredTransform CGAffineTransform .属性 a b c d 是反射矩阵和旋转矩阵的串联,而属性 tx ty 描述翻译.在我用不正确的 preferredTransform 观察到的所有情况下,反射/旋转部分似乎都是正确的,只有平移部分包含错误的值.一个可靠的解决方法似乎是检查 a b c d (总共8种情况,每种情况对应于 UIImageOrientation 中的一个案例),并相应地更新 tx ty :

The preferredTransform is a CGAffineTransform. The properties a, b, c, d are concatenations of reflection and rotation matrices, and the properties tx and ty describe a translation. In all cases that I observed with an incorrect preferredTransform, the reflection/rotation part appeared to be correct, and only the translation part contained wrong values. A reliable fix seems to be to inspect a, b, c, d (eight cases in total, each corresponding to a case in UIImageOrientation) and update tx and ty accordingly:

extension AVAssetTrack {
  var fixedPreferredTransform: CGAffineTransform {
    var t = preferredTransform
    switch(t.a, t.b, t.c, t.d) {
    case (1, 0, 0, 1):
      t.tx = 0
      t.ty = 0
    case (1, 0, 0, -1):
      t.tx = 0
      t.ty = naturalSize.height
    case (-1, 0, 0, 1):
      t.tx = naturalSize.width
      t.ty = 0
    case (-1, 0, 0, -1):
      t.tx = naturalSize.width
      t.ty = naturalSize.height
    case (0, -1, 1, 0):
      t.tx = 0
      t.ty = naturalSize.width
    case (0, 1, -1, 0):
      t.tx = naturalSize.height
      t.ty = 0
    case (0, 1, 1, 0):
      t.tx = 0
      t.ty = 0
    case (0, -1, -1, 0):
      t.tx = naturalSize.height
      t.ty = naturalSize.width
    default:
      break
    }
    return t
  }
}

这篇关于AVAssetTrack的preferredTransform有时似乎是错误的.如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 16:53