我正在尝试调整大小并裁剪AVAsset并将其导出到OS X上的
Swift中的电影文件中.
我正在使用AVAssetExportSession导出AVAsset,如果未将videoComposition设置为AVAssetExportSession,它可以正常工作.以下代码导出资产没有问题,但由于videoComposition行被注释掉,因此它不会调整大小和裁剪.
self.asset = AVAsset( URL: validURL )! if let exportSession = AVAssetExportSession( asset: self.asset,presetName: AVAssetExportPresetAppleProRes422LPCM ) { exportSession.outputURL = exportURL exportSession.outputFileType = AVFileTypeQuickTimeMovie // exportSession.videoComposition = self.videoComposition // *** problematic line exportSession.timeRange = CMTimeRangeMake( kCMTimeZero,self.asset.duration ) exportSession.exportAsynchronouslyWithCompletionHandler( { () -> Void in print( "[export completed]" ) }) }
但是如果我将videoComposition设置为AVAssetExportSession以通过删除注释来调整大小和裁剪资源,则表达的文件只有调整大小和裁剪的视频的第一帧,而第二帧和后面的帧都是黑帧,而音频导出没有问题.
我设置的videoComposition如下. (在此示例中,渲染大小和仿射变换是固定的,以简化代码)
var videoComposition: AVVideoComposition { //--------------- // composition //--------------- let composition = AVMutableVideoComposition() composition.renderSize = NSMakeSize( 720,480 ) // fixed size in this example composition.frameDuration = self.asset.duration //--------------- // instruction //--------------- let instruction = AVMutableVideoCompositionInstruction() instruction.timeRange = CMTimeRangeMake( kCMTimeZero,self.asset.duration ) //------------------------- // transform instruction //------------------------- let videoTracks = self.asset.tracksWithMediaType( AVMediaTypeVideo ) let assetTrack = videoTracks[0] let layerInstruction = AVMutableVideoCompositionLayerInstruction( assetTrack: assetTrack ) let transform = CGAffineTransformMake( 1.5,// fixed transform in this example 0.0,0.0,2.0,-100.0,-100.0 ) layerInstruction.setTransformRampFromStartTransform( transform,toEndTransform: transform,timeRange: CMTimeRangeMake( kCMTimeZero,self.asset.duration ) ) instruction.layerInstructions = [ layerInstruction ] composition.instructions = [ instruction ] return composition }
如何避免第二帧和后一帧是黑色并成功导出所有调整大小和裁剪的帧?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。