导出资产后偶尔出现空白帧 - AVExportSession

时间:2017-02-27 09:14:29

标签: swift session video export avfoundation

这个问题有几个答案,但我的情况有点不同,没有一个答案可以解决我的问题。

目的

我正在尝试将慢动作渐变 添加到我的视频的一部分中(以字母" U ")我用以下代码成功实现了这个目标:

// Description :    1) For a given TimeRange , get the start and end frame.              
//                  2) Divide this TimeRange to 10 segment
//                  3) Scale each segment with a CMTime that
//                     increments in each iteration of the for loop


 func downWithRamp(track : AVMutableCompositionTrack){


let frameTime =         CMTimeMake(1, Int32(track.nominalFrameRate))

let x1 : Double = 50
let x2 : Double = 150
let parts =      (x2 - x1)/10

let x1Time =        CMTimeMultiplyByFloat64(frameTime, x1)
let x2Time =        CMTimeMultiplyByFloat64(frameTime, x2)
let tenthDuration = CMTimeMultiplyByFloat64(CMTimeSubtract(x2Time, x1Time) , 1/10)

var scaleFactor =   1.0

var timing =     CMTimeMultiplyByFloat64(frameTime, Float64(x1))

for x in Swift.stride(from: x1, to: x2, by: parts ){

    print("\(x)th Frame")

    let factor =    CMTimeMultiplyByFloat64( tenthDuration , 1/scaleFactor)       //scale to this time
    print("This range will be scaled by \(CMTimeGetSeconds(factor)) seconds")

    let timeRange =    CMTimeRange(start: timing, duration : tenthDuration)
    print("TimeRange    =  \(CMTimeGetSeconds(timeRange.start)) - \(CMTimeGetSeconds(timeRange.end))secs ")

    track.scaleTimeRange(timeRange , toDuration: factor)
    if x < x1 + (x2 - x1)/2 {                                    
            scaleFactor = scaleFactor + 0.2 }
    else {  scaleFactor = scaleFactor - 0.2 }

    timing = CMTimeAdd(timing, factor)

    print()
}}

ISSUE :播放资源时,AVPlayer会显示所需的结果。但是,当我尝试导出视频时,任意段(缩放)显示为空白。

这是我导出视频的方式

    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL

    let filePath = documentsDirectory.appendingPathComponent("rendered-audio.mp4")
   deleteFile(filePath)

    if let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality){


      //  exportSession.canPerformMultiplePassesOverSourceMediaData = true
        exportSession.outputURL = filePath
        exportSession.shouldOptimizeForNetworkUse = true
        exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration)
        exportSession.outputFileType = AVFileTypeQuickTimeMovie
        exportSession.exportAsynchronously {
            print("finished: \(filePath) :  \(exportSession.status.rawValue) ")

            if exportSession.status.rawValue == 4{

                  print("Export failed -> Reason: \(exportSession.error!.localizedDescription))")
                  print(exportSession.error!)

            }

        }
    }

有些解决方案表明问题在于使用scaleTimeRange(..)时的精确计时,但据我所知,这种技术可以为每个帧生成准确的计时。即使日志记录也没有时间上的不一致。

这可怕的原因可能是什么原因。任何建议都可以挽救生命。谢谢!

0 个答案:

没有答案