无法通过copyNextSampleBuffer()获取样本缓冲区

时间:2019-06-28 20:17:58

标签: swift avfoundation avassetwriter avasset avassetreader

我正在尝试使用AVAssetReader和AVAssetWriter处理AVAsset视频的每一帧。但是,我收到一个奇怪的错误,好像我已经完成了要执行的错误一样。这是错误:

  

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'-[AVAssetReaderTrackOutput copyNextSampleBuffer] 在将该输出添加到AVAssetReader实例(使用-addOutput :)并在其上调用-startReading之前,无法复制下一个示例缓冲区资产阅读器”

我正在使用Xcode 11 beta 2 ...这可能引起问题吗?

将非常感谢您的任何想法/帮助。

    //MAKE WRITER
    var writer: AVAssetWriter
    do {
        writer = try AVAssetWriter(outputURL: url, fileType: .mov)
    } catch {
        Helper.problem(error.localizedDescription, #line)
        return
    }

    //MAKE READER
    var reader: AVAssetReader
    do {
        reader = try AVAssetReader(asset: composition)
    } catch {
        Helper.problem(error.localizedDescription, #line)
        return
    }

    //READER OUTPUTS
    guard let videoAssetTrack = composition.tracks(withMediaType: .video).first else { return }
    let videoTrackOutput = AVAssetReaderTrackOutput(track: videoAssetTrack, outputSettings: [String(kCVPixelBufferPixelFormatTypeKey): NSNumber(value: kCVPixelFormatType_32BGRA)])
    videoTrackOutput.alwaysCopiesSampleData = false

    guard let audioAssetTrack = composition.tracks(withMediaType: .audio).first else { return }
    let audioTrackOutput = AVAssetReaderTrackOutput(track: audioAssetTrack, outputSettings: nil)
    audioTrackOutput.alwaysCopiesSampleData = false


    //WRITER INPUTS
    let audioWriterInput = AVAssetWriterInput(mediaType: .audio, outputSettings: nil)
    audioWriterInput.expectsMediaDataInRealTime = true

    let videoWriterInput = AVAssetWriterInput(mediaType: .video, outputSettings: [AVVideoCodecKey: AVVideoCodecType.hevc, AVVideoWidthKey: 1080, AVVideoHeightKey: 1920])
    videoWriterInput.expectsMediaDataInRealTime = true


    if writer.canAdd(videoWriterInput) && writer.canAdd(audioWriterInput) && reader.canAdd(videoTrackOutput) && reader.canAdd(audioTrackOutput) {
        writer.add(videoWriterInput)
        writer.add(audioWriterInput)
        reader.add(videoTrackOutput)
        reader.add(audioTrackOutput)


        writer.startWriting()
        reader.startReading()
        writer.startSession(atSourceTime: .zero)


        let mediaQueue = DispatchQueue(label: "mediaInputQueue")

        let dispatchGroup = DispatchGroup()

        dispatchGroup.enter()
        audioWriterInput.requestMediaDataWhenReady(on: mediaQueue) {
            while audioWriterInput.isReadyForMoreMediaData {
                let nextSampleBuffer = audioTrackOutput.copyNextSampleBuffer()

                if let nextSampleBuffer = nextSampleBuffer {
                    audioWriterInput.append(nextSampleBuffer)
                } else {
                    audioWriterInput.markAsFinished()
                    dispatchGroup.leave()
                    break
                }
            }
        }

        dispatchGroup.enter()
        videoWriterInput.requestMediaDataWhenReady(on: mediaQueue) {
            while videoWriterInput.isReadyForMoreMediaData {
                let nextSampleBuffer = videoTrackOutput.copyNextSampleBuffer()

                if let nextSampleBuffer = nextSampleBuffer {
                    videoWriterInput.append(nextSampleBuffer)
                } else {
                    videoWriterInput.markAsFinished()
                    dispatchGroup.leave()
                    break
                }
            }
        }



        dispatchGroup.notify(queue: mediaQueue) {
            print("finished writing!")
            completion(url)
        }
    }

0 个答案:

没有答案