0

레이어가있는 애니메이션을 만들고 해당 애니메이션이 포함 된 비디오를 내보내려고합니다. 그래서 AVAssetExportSession을 사용하지만, 내보내기에는 시간이 오래 걸립니다.AVVideoCompositionCoreAnimationTool 및 AVAssetExportSession을 사용하여 비디오 만들기 SLOW

아마도 다른 것을 사용할 수 있습니까? 정말 도움이 필요해!

let videoURL = NSURL.init(fileURLWithPath: "/Users/Downloads/img_2040.mp4") 
    let audioURL = NSURL.init(fileURLWithPath: "/Users/Downloads/music_10sm.m4a") 

    let videoAsset = AVURLAsset.init(url: videoURL as URL) 
    let audioAsset = AVURLAsset.init(url: audioURL as URL) 

    let mixComposition = AVMutableComposition.init() 
    let compositionVideoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid) 
    // let mixCompositionAudio = AVMutableComposition.init() 
    let compositionAudioTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid) 
    // AVAssetTrack video of originalVideo 
    let originalVideoAsset = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first 
    let originalAudioAsset = audioAsset.tracks(withMediaType: AVMediaTypeAudio).first 

    do { 
     try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: originalVideoAsset!, at: kCMTimeZero) 
     compositionVideoTrack.preferredTransform = (videoAsset.tracks(withMediaType: AVMediaTypeVideo).first?.preferredTransform)! 

     try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioAsset.duration), of: originalAudioAsset!, at: kCMTimeZero) 
     compositionAudioTrack.preferredTransform = (audioAsset.tracks(withMediaType: AVMediaTypeAudio).first?.preferredTransform)! 

     let videoSize = originalVideoAsset?.naturalSize 
     let parentLayer = CALayer() 
     let videoLayer = CALayer() 
     parentLayer.bounds = CGRect(x: 0, y: 0, width: (videoSize?.width)!, height: (videoSize?.height)!) 
     parentLayer.position = CGPoint(x: (videoSize?.width)!/2, y: (videoSize?.height)!/2) 
     videoLayer.bounds = CGRect(x: 0, y: 0, width: (videoSize?.width)!, height: (videoSize?.height)!) 
     videoLayer.position = CGPoint(x: (videoSize?.width)!/2 + 20, y: (videoSize?.height)!/2) 
     let layerTest = CALayer() 
     layerTest.bounds = CGRect(x: 0, y: 0, width: 100, height: 100) 
     layerTest.backgroundColor = UIColor.green.cgColor 

     parentLayer.addSublayer(videoLayer) 
     parentLayer.insertSublayer(layerTest, below: videoLayer) 

     // My layer with animations 
     let cubeLayer = cubeAnimation(videoSize: containerLayer.frame.size, isVideo: true) 
     containerLayer.addSublayer(cubeLayer) 

     parentLayer.addSublayer(containerLayer) 
     parentLayer.isGeometryFlipped = true 

     let videoComposition = AVMutableVideoComposition.init() 
     videoComposition.renderSize = videoSize! 
     videoComposition.frameDuration = CMTimeMake(1, 30) 
     videoComposition.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: videoLayer, in: parentLayer) 

     // Instruction 
     let instruction = AVMutableVideoCompositionInstruction.init() 
     instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration) // TEST CAMBIAR ESTA DURATION 
     // Video 
     let videoTrack = mixComposition.tracks(withMediaType: AVMediaTypeVideo).first 
     let layerInstructions = AVMutableVideoCompositionLayerInstruction.init(assetTrack: videoTrack!) 

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

     let assetExport = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) 
     assetExport?.videoComposition = videoComposition 

     let exportPath = "/Users/CarolinaAitcin/Downloads/Test_ScrollBest91.mp4" 
     let exportUrl = URL.init(fileURLWithPath: exportPath) 

     assetExport?.outputFileType = AVFileTypeQuickTimeMovie 
     assetExport?.outputURL = exportUrl 
     assetExport?.shouldOptimizeForNetworkUse = true 

     assetExport?.exportAsynchronously { 
      print("Finish video") 
      print(NSDate()) 
     } 

     Timer.schedule(repeatInterval: 1, handler: { (runTime) in 
      let progress = assetExport?.progress 
      print(progress) 
     }) 
    } catch { 
     print("we have problem") 
    } 
+0

"정말 긴 시간"을 정의하십시오. CPU (사용중인 Mac 또는 iPhone)는 내보내기 시간에 영향을줍니다. 애니메이션의 수와 스타일도 내보내기 시간을 늘립니다. 엄지 손가락으로 보면, 1 : 1 (60 초 비디오가 내보내려면 60 초 소요) 이하의 모든 항목이 적합합니다. 크기 문제! 640x360은 1280x720보다 빠를 것입니다. AVAssetExportSession 대신 AVAssetWriter로 이동하거나 https://github.com/rs/SDAVAssetExportSession을 사용하면 Frame 및 BitRates도 사용자 정의 할 수 있으므로 쓰기 및 재생 성능에 모두 영향을줍니다. –

+0

견고한 벤치 마크를 얻으려면 애니메이션이나 변형없이 비디오를로드하고 내보내십시오. 이것은 장치가 비디오에서 (읽기 및 쓰기) "할 수있는"최소한의 작업입니다. 다양한 길이의 동영상을 몇 가지 시도해보고이를 실적을 측정하고 평가할 기준으로 삼으십시오. 희망이 도움이 행운을 빕니다! –

답변

0

기기에서 내보내기를 테스트 할 때 시간이 많이 줄어 들었지만 20 초만 걸립니다. 시뮬레이터에서 약 2.5 분이 소요됩니다.

관련 문제