1

Example

Hi,

Struggling to rotate this video to show in the proper orientation and fill the entire screen.

I cannot get the avasset with videocompisition but cannot get it to work correctly.

    let videoAsset: AVAsset = AVAsset(URL: outputFileURL) as AVAsset

    let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack

    let newHeight = CGFloat(clipVideoTrack.naturalSize.height/3*4)

    let composition = AVMutableComposition()
    composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    let videoComposition = AVMutableVideoComposition()
    var videoSize = CGSize()
    videoSize = clipVideoTrack.naturalSize
    videoComposition.renderSize = videoSize
    videoComposition.frameDuration = CMTimeMake(1, 30)

    let instruction = AVMutableVideoCompositionInstruction()

    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))

    // rotate to portrait
    let transformer:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
    let t1 = CGAffineTransformMakeTranslation(0, 0);
    let t2 = CGAffineTransformRotate(t1, CGFloat(M_PI_2));

    transformer.setTransform(t2, atTime: kCMTimeZero)
    instruction.layerInstructions = [transformer]
    videoComposition.instructions = [instruction]

    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
    let date = NSDate()
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    let outputPath = "\(documentsPath)/\(formatter.stringFromDate(date)).mp4"
    let outputURL = NSURL(fileURLWithPath: outputPath)

    let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
    exporter.videoComposition = videoComposition

    exporter.outputURL = outputURL
    exporter.outputFileType = AVFileTypeQuickTimeMovie

    exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
        dispatch_async(dispatch_get_main_queue(), {
            self.handleExportCompletion(exporter)
        })
    })
Bean91
  • 11
  • 1
  • 1
  • 7
  • Please provide the code you are using. It's difficult to debug an issue if we don't know how it has been implemented. – Scriptable Feb 02 '16 at 14:03
  • Any ideas anyone ? Struggling massively with this one and cannot find much at all online for this. – Bean91 Feb 03 '16 at 08:55
  • what is the result of the code above? could you provide a sample video that needs the rotation doing and I'll have a look later today – Scriptable Feb 03 '16 at 10:29
  • 2 Links one is an image of the how the video exports and one is the video itself. https://www.dropbox.com/s/q8be3oqnwri4tme/IMG_0400.PNG?dl=0 https://www.dropbox.com/s/iv5zot2onwqewv7/IMG_0401.mp4?dl=0 Thank you. – Bean91 Feb 03 '16 at 11:00
  • Cheers, I'll take a look soon as possible – Scriptable Feb 03 '16 at 11:18
  • Dropping links to images etc that are transient frustrated future readers here. For all the nuanced criticism that SO editors toss out to those who post questions, this seems ignored. – drew.. Feb 09 '18 at 18:19

1 Answers1

0

Solved the rotation converting from the code below:

AVMutableVideoComposition rotated video captured in portrait mode

Now having issues with exporting in question below if anyone knows: https://stackoverflow.com/questions/35233766/avasset-failing-to-export

kalpesh
  • 1,285
  • 1
  • 17
  • 30
Bean91
  • 11
  • 1
  • 1
  • 7
  • 1
    Anyone have actual answers to this? Seems I can't find anything in terms of just rotating a video, but I remember I used to do it with an AVAsset with rotateBy – Chewie The Chorkie Aug 06 '18 at 18:05
  • Chewie The Chorkie, did you ever find a solution to this, I would like to rotate a video and well. Thanks for any help. – LilMoke Oct 12 '19 at 15:10