问题
This has been bugging me for the last day, I used to use this method in ObjC to crop videos into square, it seems to be the only method i've found in a few years that worked but after recently trying to crop using it in Swift & iOS 8 it doesn't seem to crop the video at all, Hopefully somebody can help?
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)
}
}
回答1:
So it turns out that if you try setting the exporter's outputUrl to the same as the asset your editing like I did it doesn't edit it, so stupid! so for future referencing, outputUrl should be set to a new unique one
回答2:
I can't give you exact answer because i use C# (Xamarin) for iOS programming. But i use setCropRectangle for cropping video. In C# it looks like this:
layerInstruction.SetCrop (new CGRect (_cropMove, 0, _width / 2, height), transformTime);
Look at the documentation: setCropRectangle
来源:https://stackoverflow.com/questions/29156053/cropping-avasset-video-with-avfoundation-not-working-ios-8