如何将视频裁剪为特定大小?
Posted
技术标签:
【中文标题】如何将视频裁剪为特定大小?【英文标题】:How to crop video to a particular size? 【发布时间】:2015-07-31 11:05:46 【问题描述】:我已按照此链接裁剪和调整视频大小:
Swift: Crop and Export Video
我想裁剪视频并将其大小调整为 612*612。 我的代码如下:
let outputPath : NSString = NSString(format: "%@%@", NSTemporaryDirectory(), "output.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, 60)
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(clipVideoTrack.naturalSize.height, 0)
var t2: CGAffineTransform = CGAffineTransformRotate(t1, CGFloat(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(), "output2.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
let outputURL:NSURL = exporter.outputURL;
self.videoURL = outputURL
let asset:AVURLAsset = AVURLAsset(URL: outputURL, options: nil)
)
我的视频尺寸为 612*612,但内容很奇怪。可能是什么问题?
【问题讨论】:
为什么要旋转视频剪辑:var t2: CGAffineTransform = CGAffineTransformRotate(t1, CGFloat(M_PI_2))
@MarcusAlexanderInzinger 我遵循了这里给出的解决方案:***.com/questions/27660975/…
好吧...如果你试试这个会发生什么: var t2: CGAffineTransform = CGAffineTransformRotate(t1, 0) ?
@MarcusAlexanderInzinger 它的右侧移动,当前位于左侧。但是黑色背景的尺寸仍然错误
你能发个截图吗?
【参考方案1】:
解决方法: 我已将 AVCaptureSession 的值设置为 AVCaptureSessionPresetiFrame960x540 我将其值更改为 AVCaptureSessionPreset1280x720 解决了我的问题。
【讨论】:
以上是关于如何将视频裁剪为特定大小?的主要内容,如果未能解决你的问题,请参考以下文章
使用 Imagemagick 将大图像裁剪为特定的纵横比,然后调整它们的大小
如何使用 Cordova/Ionic 将图像裁剪为特定纵横比?