AVAssetTrack 的 preferredTransform 有时似乎是错误的。如何解决这个问题?

The*_*heo 4 avfoundation ios avassetexportsession

我正在使用AVAssetExportSession在 iOS 应用程序中导出视频。为了以正确的方向呈现视频,我使用AVAssetTrack的是preferredTransform. 对于一些源视频,这个属性似乎有一个错误的值,结果视频出现偏移或全黑。我怎样才能解决这个问题?

The*_*heo 6

preferredTransform是一个CGAffineTransform. 的属性abcd是反射和旋转矩阵的级联,并且属性txty描述了翻译。在我观察到的所有不正确的情况下,preferredTransform反射/旋转部分似乎是正确的,只有平移部分包含错误的值。一个可靠的修复似乎是检查abcd(8案件总共,每个对应于在壳体UIImageOrientation)和更新txty相应地:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我设法在 iOS 14 上重现它,如下所示:使用相机应用程序以纵向录制视频。然后,使用照片应用程序将视频帧裁剪为较小的矩形。生成的视频显示了不正确的preferredTransform。我不了解技术细节,所以我不知道这实际上是照片应用程序还是“AVFoundation”中的错误(是视频文件的“preferredTransform”部分还是由“AVFoundation”创建的,基于其他视频属性?)。我提交了错误 FB8762584,该错误已标记为“已识别的潜在修复”。 (3认同)