[iOSアプリ開発] 撮影した動画をクロップsample code (iOSxswift: crop recoded video to rectangle)

せっかくなので動画クロップ時に書いたコードをのっけときます。

</p>
<p>  func cropVideo(outputFileURL:NSURL){<br />
        let videoAsset: AVAsset = AVAsset(URL: outputFileURL) as AVAsset</p>
<p>        let clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first! as AVAssetTrack</p>
<p>        let composition = AVMutableComposition()<br />
        composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())</p>
<p>        let videoComposition = AVMutableVideoComposition()</p>
<p>        videoComposition.renderSize = CGSizeMake(720, 720)<br />
        videoComposition.frameDuration = CMTimeMake(1, 30)</p>
<p>        let instruction = AVMutableVideoCompositionInstruction()</p>
<p>        instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))</p>
<p>        // rotate to portrait<br />
        let transformer:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)<br />
        let t1 = CGAffineTransformMakeTranslation(720, 0);<br />
        let t2 = CGAffineTransformRotate(t1, CGFloat(M_PI_2));</p>
<p>        transformer.setTransform(t2, atTime: kCMTimeZero)<br />
        instruction.layerInstructions = [transformer]<br />
        videoComposition.instructions = [instruction]</p>
<p>        let directory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String<br />
        let dateFormatter = NSDateFormatter()<br />
        dateFormatter.dateStyle = .LongStyle<br />
        dateFormatter.timeStyle = .ShortStyle<br />
        let date = dateFormatter.stringFromDate(NSDate())<br />
        var savePath = &quot;&quot;<br />
        savePath = &quot;\(directory)/mergedVideo-\(date).mp4&quot;</p>
<p>        let outputURL = NSURL(fileURLWithPath: savePath)<br />
        let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!<br />
        exporter.videoComposition = videoComposition<br />
        exporter.outputURL = outputURL<br />
        exporter.outputFileType = AVFileTypeQuickTimeMovie</p>
<p>        exporter.exportAsynchronouslyWithCompletionHandler({ () -&gt; Void in<br />
            dispatch_async(dispatch_get_main_queue(), {<br />
                self.finalExportCompletion(exporter)<br />
            })<br />
        })<br />
    }</p>
<p>   func finalExportCompletion(session: AVAssetExportSession) {<br />
        let library = ALAssetsLibrary()<br />
        if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(session.outputURL) {<br />
        var completionBlock: ALAssetsLibraryWriteVideoCompletionBlock</p>
<p>        completionBlock = { assetUrl, error in<br />
        if error != nil {<br />
        print(&quot;error writing to disk&quot;)<br />
          }<br />
        }<br />
        library.writeVideoAtPathToSavedPhotosAlbum(session.outputURL, completionBlock: completionBlock)<br />
        }</p>
<p>        }<br />
    }<br />
}<br />