2016-09-21 50 views
1

我想在「圈子」中裁剪視頻並將其存儲。這是我迄今爲止所做的。但問題是它只會裁剪矩形中的視頻。我想圈選它。我怎樣才能做到這一點?使用swift裁剪並將視頻導出爲圈子

func cropVideo() { 

    let asset = AVAsset.init(url: URL(fileURLWithPath: Bundle.main.path(forResource: "1", ofType: "mp4")!)) 
    let clipVideoTrack = asset.tracks(withMediaType: AVMediaTypeVideo)[0] 
    let videoComposition = AVMutableVideoComposition() 
    videoComposition.frameDuration = CMTimeMake(1, 30) 
    videoComposition.renderSize = CGSize(width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.height) 
    let instruction = AVMutableVideoCompositionInstruction() 
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30)) 
    let transformer = AVMutableVideoCompositionLayerInstruction.init(assetTrack: clipVideoTrack) 
    let t1 = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height, y: 0) 
    let t2 = t1.rotated(by: CGFloat(M_PI_2)) 
    let finalTransform = t2 
    transformer.setTransform(finalTransform, at: kCMTimeZero) 
    instruction.layerInstructions = [transformer] 
    videoComposition.instructions = [instruction] 


    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] 
    let exportPath = documentsPath.appendingFormat("/CroppedVideo.mp4") 
    let exportUrl = URL(fileURLWithPath: exportPath) 
    print("export url = \(exportUrl)") 


    do { 
     try FileManager.default.removeItem(at: exportUrl) 
    } 
    catch _ { 
    } 

    exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) 
    exporter.videoComposition = videoComposition 
    exporter.outputURL = exportUrl 
    exporter.outputFileType = AVFileTypeQuickTimeMovie 
    exporter.exportAsynchronously(completionHandler: {() -> Void in 
     DispatchQueue.main.async(execute: {() -> Void in 
      self.exportDidFinish(self.exporter) 
     }) 
    }) 
} 

func exportDidFinish(_ session: AVAssetExportSession) { 
    let outputURL = session.outputURL 
    print("outputurl = \(outputURL)") 

} 
+0

目前尚不清楚你在問什麼。你的意思是「我如何讓CGAffineTransform變成一個圓而不是一個矩形?」 –

回答

0

假設上面,我的問題是正確的,關鍵是要使用屏蔽和一個圓形的圖像,而不是使用CGAffineTransform的。我懷疑有一種方法與CGAffineTransform做到這一點,但在此期間...

  • 做一個透明的圓的圖像,中間
  • 將其導入您的項目,並加載它
  • 加您的視頻層,然後再遮掩圖像
  • 出口

這應該得到你想要的東西。關於這個here有一個較老的教程,看看大約一半。

+0

它實際上並沒有修剪視頻而只是掩蓋它。 – Developer

+0

的確,但最終的結果是一樣的,不是嗎?或者你想讓圈外的區域完全「消失」? –

+0

是的。我想要消失。 – Developer