iosavfoundationavassetexportsession

AVAssetTrack's preferredTransform sometimes seems to be wrong. How can this be fixed?


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?


Solution

  • 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
      }
    }