2017-03-24 8 views
3

Swift 3とiOS 10.xでビデオを四角に切り取る際に問題があります。私の作物ルーチンを実行した後、私は自分の写真ライブラリにビデオを保存し、それは元のものと同じに見えます。iOSでスクエアにビデオを切り抜く[Swift 3]

私はリファレンスとして、次のポストを使用しています:以下のようCropping AVAsset video with AVFoundation not working iOS 8

func suqareCropVideo(inputURL: NSURL, completion: @escaping (_ outputURL : NSURL?) ->()) 
{ 
    let videoAsset: AVAsset = AVAsset(url: inputURL as URL) 
    let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first! as AVAssetTrack 

    let composition = AVMutableComposition() 
    composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID()) 

    let videoComposition = AVMutableVideoComposition() 
    videoComposition.renderSize = CGSize(width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.height) 
    videoComposition.frameDuration = CMTimeMake(1, 30) 

    let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack) 

    let instruction = AVMutableVideoCompositionInstruction() 
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30)) 


    let transform1: CGAffineTransform = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height, y: (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height)/2) 
    let transform2 = transform1.rotated(by: .pi/2) 
    let finalTransform = transform2 


    transformer.setTransform(finalTransform, at: kCMTimeZero) 

    instruction.layerInstructions = [transformer] 
    videoComposition.instructions = [instruction] 

    // Export 
    let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)! 
    print ("random id = \(NSUUID().uuidString)") 

    let croppedOutputFileUrl = URL(fileURLWithPath: getOutputPath(NSUUID().uuidString)) // CREATE RANDOM FILE NAME HERE 
    exportSession.outputURL = croppedOutputFileUrl 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie 

    exportSession.exportAsynchronously() { handler -> Void in 
     if exportSession.status == .completed { 
      print("Export complete") 
      DispatchQueue.main.async(execute: { 
       completion(croppedOutputFileUrl as NSURL) 
      }) 
      return 
     } else if exportSession.status == .failed { 
      print("Export failed - \(String(describing: exportSession.error))") 
     } 

     completion(nil) 
     return 
    } 
} 

使用法:

if videoURL != nil { 
     print ("crop video") 
     suqareCropVideo(inputURL: self.videoURL, completion: { (outputURL) ->() in 
      print ("compressed url = \(String(describing: outputURL))") 

      // Save video to photo library 
      PHPhotoLibrary.shared().performChanges({ 
       PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL:outputURL! as URL) 
      }) { saved, error in 
       if saved { 
        print ("save successful") 
       } 
       else { 
        print ("save failed") 
       } 
      } 
     }) 
    } 
+0

これを達成できましたか? –

答えて

2

をあなたが実際にそうしてみてください輸出

に映像合成を設定していません

exportSession.videoComposition = videoComposition 
01エクスポートを開始する前に

+1

ありがとう@Agreensh。私は逃しました! – CTRLALTDELx64

関連する問題