使用 AVFoundation 裁剪 AVAsset 视频不起作用 iOS 8
Cropping AVAsset video with AVFoundation not working iOS 8
这最后一天一直困扰着我,我曾经在 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)
}
}
我无法给你确切的答案,因为我使用 C# (Xamarin) 进行 iOS 编程。但我使用 setCropRectangle 来裁剪视频。
在 C# 中,它看起来像这样:
layerInstruction.SetCrop (new CGRect (_cropMove, 0, _width / 2, height), transformTime);
查看文档:setCropRectangle
事实证明,如果您尝试将导出器的 outputUrl 设置为与资产相同,您像我一样进行编辑,它不会编辑它,太愚蠢了!所以为了将来的参考,outputUrl 应该设置为一个新的唯一的
这最后一天一直困扰着我,我曾经在 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)
}
}
我无法给你确切的答案,因为我使用 C# (Xamarin) 进行 iOS 编程。但我使用 setCropRectangle 来裁剪视频。 在 C# 中,它看起来像这样:
layerInstruction.SetCrop (new CGRect (_cropMove, 0, _width / 2, height), transformTime);
查看文档:setCropRectangle
事实证明,如果您尝试将导出器的 outputUrl 设置为与资产相同,您像我一样进行编辑,它不会编辑它,太愚蠢了!所以为了将来的参考,outputUrl 应该设置为一个新的唯一的