使用AVFoundation裁剪AVAsset视频不适用于iOS 8

时间:2015-03-19 22:19:19

标签: ios swift avfoundation

这一直困扰着我的最后一天,我曾经在ObjC中使用这种方法将视频裁剪成方形,这似乎是我在几年内找到的唯一方法,但是在最近尝试之后在Swift&中使用它进行裁剪iOS 8它似乎根本没有裁剪视频,希望有人可以提供帮助吗?

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    if error != nil {
        println("Error Outputting recording")
    } else {
        self.writeVideoToAssetsLibrary(self.outputUrl!.copy() as NSURL)
    }
}

func writeVideoToAssetsLibrary(videoUrl: NSURL) {
    var videoAsset: AVAsset = AVAsset.assetWithURL(videoUrl) as AVAsset

    var clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first as AVAssetTrack

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

    var videoComposition = AVMutableVideoComposition()

    videoComposition.renderSize = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height)
    videoComposition.frameDuration = CMTimeMake(1, 30)


    var transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

    var instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))

    var transform1: CGAffineTransform = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) / 2)
    var transform2 = CGAffineTransformRotate(transform1, CGFloat(M_PI_2))
    var finalTransform = transform2


    transformer.setTransform(finalTransform, atTime: kCMTimeZero)

    instruction.layerInstructions = [transformer]
    videoComposition.instructions = [instruction]

    var exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)
    exporter.videoComposition = videoComposition
    exporter.outputURL = videoUrl
    exporter.outputFileType = AVFileTypeQuickTimeMovie

    exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
        dispatch_async(dispatch_get_main_queue(), {
            self.handleExportCompletion(exporter)
        })
    })
}

func handleExportCompletion(session: AVAssetExportSession) {
    var library = ALAssetsLibrary()

    if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(session.outputURL) {
        var completionBlock: ALAssetsLibraryWriteVideoCompletionBlock

        completionBlock = { assetUrl, error in
            if error != nil {
                println("error writing to disk")
            } else {

            }
        }

        library.writeVideoAtPathToSavedPhotosAlbum(outputUrl, completionBlock: completionBlock)
    }
}

2 个答案:

答案 0 :(得分:4)

所以事实证明,如果你尝试将导出器的outputUrl设置为与我编辑的资产相同,那么它就不会编辑它,所以很愚蠢!因此,对于将来的引用,outputUrl应设置为新的唯一

答案 1 :(得分:-1)

我无法给出确切的答案,因为我使用C#(Xamarin)进行iOS编程。但我使用setCropRectangle来裁剪视频。 在C#中它看起来像这样:

layerInstruction.SetCrop (new CGRect (_cropMove, 0, _width / 2, height), transformTime);

查看文档:{​​{3}}