简体   繁体   English

使用AVAssetWriter进行视频压缩

[英]Video compression using AVAssetWriter

I've created a function to compress a video file. 我创建了一个压缩视频文件的功能。 It uses AVAssetWriter and adds inputs and outputs for video and audio tracks. 它使用AVAssetWriter并添加视频和音频轨道的输入和输出。 When it starts writing I'm getting an error when the AVAssetReader for the audio track starts reading, audioReader.startReading() . 当它开始写入时,当音轨的AVAssetReader开始读取时,我得到一个错误audioReader.startReading() Here the error, *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetReader startReading] cannot be called again after reading has already started' . 在这里,错误*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetReader startReading] cannot be called again after reading has already started'

The code: https://gist.github.com/jaumevn/9ba329aaf49c81c57a276fd135f53f20 代码: https//gist.github.com/jaumevn/9ba329aaf49c81c57a276fd135f53f20

Can anyone see what's the problem here? 谁能看到这里的问题吗? Thanks! 谢谢!

Line 77 of your code, you're starting a second AVAssetReader on the same file. 在代码的第77行中,您正在同一文件上启动另一个AVAssetReader。

You don't need to hook up two readers, instead, you should hook up your AVAudioAssetReader as an Output for the existing AVAssetReader. 您不需要连接两个阅读器,而是应将AVAudioAssetReader连接为现有AVAssetReader的输出。

Something like this: 像这样:

let videoReaderSettings : [String : Int] = [kCVPixelBufferPixelFormatTypeKey as String : Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)]
    let videoReaderOutput = AVAssetReaderTrackOutput(track: videoAssetTrack, outputSettings: videoReaderSettings)
    let videoReader = try! AVAssetReader(asset: videoAssetUrl)

    var settings = [String : AnyObject]()
    settings[AVFormatIDKey] = Int(kAudioFormatLinearPCM)
    let audioReaderOutput = AVAssetReaderTrackOutput(track: audioAssetTrack, outputSettings: settings)

    videoReader.addOutput(videoReaderOutput)
    videoReader.addOutput(audioReaderOutput)

    videoWriter.startWriting()
    videoReader.startReading()

Look into using AVCaptureVideoDataOutputSampleBufferDelegate and AVCaptureAudioDataOutputSampleBufferDelegate to capture and process the buffers from the reader. 研究使用AVCaptureVideoDataOutputSampleBufferDelegate和AVCaptureAudioDataOutputSampleBufferDelegate捕获和处理读取器中的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM