2016-09-21 2 views
1

「サークル」で動画を切り取り保存します。これはこれまで私が行ってきたことです。しかし問題は、ビデオを矩形でのみ切り抜くことです。私は円でそれを作ってみたい。これどうやってするの?上記の私の質問を想定しスウィフトを使用して動画をサークルに切り出して書き出します

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

関連する問題