我想将拍摄的视频从横向旋转到肖像,我的代码如下:
if self.isDeviceRotatedToLandscape
{
let outputPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output2.mov")
let outputURL : NSURL = NSURL(fileURLWithPath: outputPath as String)!
let fileManager : NSFileManager = NSFileManager.defaultManager()
if(fileManager.fileExistsAtPath(outputPath as String))
{
let asset : AVURLAsset = AVURLAsset(URL: outputURL, options: nil)
if let clipVideoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as? AVAssetTrack
{
var FirstlayerInstruction:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
if let FirstAssetTrack:AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as? AVAssetTrack
{
let FirstAssetTrack_ = UIImageOrientation.Up
var isFirstAssetPortrait_ = false
var FirstAssetScaleToFitRatio:CGFloat = 1.0
var FirstAssetScaleFactor:CGAffineTransform = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio, FirstAssetScaleToFitRatio)
FirstlayerInstruction.setTransform(CGAffineTransformConcat(CGAffineTransformConcat(FirstAssetTrack.preferredTransform,FirstAssetScaleFactor), CGAffineTransformMakeTranslation(0, 160)), atTime: kCMTimeZero)
FirstlayerInstruction.setOpacity(0.0, atTime: asset.duration)
var MainInstruction:AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
MainInstruction.layerInstructions = [FirstlayerInstruction]
var MainCompositionInst: AVMutableVideoComposition = AVMutableVideoComposition()
MainCompositionInst.instructions = [MainInstruction]
MainCompositionInst.frameDuration = CMTimeMake(1, 30)
MainCompositionInst.renderSize = CGSizeMake(612,612)
let exportPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output1212.mov")
var exportUrl: NSURL = NSURL.fileURLWithPath(exportPath as! String)!
let fileManager : NSFileManager = NSFileManager.defaultManager()
if(fileManager.fileExistsAtPath(exportPath as String))
{
var error:NSError? = nil
if(fileManager.removeItemAtPath(exportPath as String, error: &error))
{
//Error - handle if requried
}
}
var exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exporter.videoComposition = MainCompositionInst
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.outputURL = exportUrl
exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
print("hi")
UISaveVideoAtPathToSavedPhotosAlbum(exportPath as String, self, nil, nil)
})
}
}
}
}
但视频甚至没有创建并保存到照片应用。我不是没有错误的地方。请帮忙解决这个问题,我很困难,需要尽快解决这个问题
答案 0 :(得分:2)
实际上问题出现在CGAffineTransform中,需要根据左侧或右侧的景观设置它并且它按预期工作。 代码如下:
if isDeviceRotatedToLandscape
{
let outputPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output2.mov")
let outputURL : NSURL = NSURL(fileURLWithPath: outputPath as String)!
let fileManager : NSFileManager = NSFileManager.defaultManager()
if(fileManager.fileExistsAtPath(outputPath as String))
{
let asset : AVURLAsset = AVURLAsset(URL: outputURL, options: nil)
if let clipVideoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as? AVAssetTrack
{
var videoComposition: AVMutableVideoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTimeMake(1, 30)
print(clipVideoTrack.naturalSize.height)
videoComposition.renderSize = CGSizeMake(612,612)
var instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))
var transformer: AVMutableVideoCompositionLayerInstruction =
AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
var t1: CGAffineTransform = CGAffineTransformMakeTranslation(isDeviceLandscapeLeft ? 0 : 612, isDeviceLandscapeLeft ? 612 : 0)
var t2: CGAffineTransform?
t2 = CGAffineTransformRotate(t1, CGFloat(isDeviceLandscapeLeft ? -M_PI_2 : M_PI_2))
var finalTransform: CGAffineTransform = t2!
transformer.setTransform(finalTransform, atTime: kCMTimeZero)
instruction.layerInstructions = NSArray(object: transformer) as [AnyObject]
videoComposition.instructions = NSArray(object: instruction) as [AnyObject]
let exportPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output22.mov")
var exportUrl: NSURL = NSURL.fileURLWithPath(exportPath as! String)!
if(fileManager.fileExistsAtPath(exportPath as String))
{
var error:NSError? = nil
if(fileManager.removeItemAtPath(exportPath as String, error: &error))
{
//Error - handle if requried
}
}
var exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exporter.videoComposition = videoComposition
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.outputURL = exportUrl
exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
dispatch_async(dispatch_get_main_queue()) {
() -> Void in
UISaveVideoAtPathToSavedPhotosAlbum(exportPath as String, self, nil, nil)
}
})
}
}
}