AVFoundation-更改帧速率

时间:2019-01-16 20:10:39

标签: avfoundation frame-rate avassetwriter transcoding avassetreader

我想将几个不同帧频的视频合并在一起。理想的结果是具有一些典型的fps,例如24、30、60等。但是,当我使用AVAssetWriterAVAssetReader并简单地将每个X帧附加到视频输入时,这是一种不好的做法正如我在某处阅读的内容),这会导致FPS异常。

我还尝试使用以下方法重新计时CMSampleBuffer:

fileprivate func setTimeStamp(_ sample: CMSampleBuffer, newTime: CMTime) -> CMSampleBuffer {
    var count: CMItemCount = 0
    CMSampleBufferGetSampleTimingInfoArray(sample, entryCount: 0, arrayToFill: nil, entriesNeededOut: &count);
    var info = [CMSampleTimingInfo](repeating: CMSampleTimingInfo(duration: CMTimeMake(value: 0, timescale: 0), presentationTimeStamp: CMTimeMake(value: 0, timescale: 0), decodeTimeStamp: CMTimeMake(value: 0, timescale: 0)), count: count)
    CMSampleBufferGetSampleTimingInfoArray(sample, entryCount: count, arrayToFill: &info, entriesNeededOut: &count);

    for i in 0..<count {
        info[i].decodeTimeStamp = newTime
        info[i].presentationTimeStamp = newTime
    }

    var out: CMSampleBuffer?
    CMSampleBufferCreateCopyWithNewTiming(allocator: nil, sampleBuffer: sample, sampleTimingEntryCount: count, sampleTimingArray: &info, sampleBufferOut: &out);
    return out!
}

但是这导致音频不同步。

现在,我很确定这不是应该怎么做。可能有一个我看不到的简单解决方案?

0 个答案:

没有答案