iOS - 在设备上修剪mp4

时间:2015-11-20 16:09:19

标签: ios swift avfoundation avassetreader

我以为我可能会使用AVAssetReader/Writer来修剪MP4,但是当我尝试阅读音频时(尚未尝试过视频),我明白了这一点:

AVAssetReaderOutput does not currently support compressed output

有没有办法通过AVAssetReader读取磁盘上的MP4?

let asset: AVAsset = AVAsset(URL: NSURL(fileURLWithPath: filePath))
        let audioTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeAudio)[0]
        let audioTrackOutput: AVAssetReaderOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings:
        [AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC)])

        if let reader = try? AVAssetReader(asset: asset) {
            if reader.canAddOutput(audioTrackOutput) {
                reader.addOutput(audioTrackOutput)
            }
            reader.startReading()

            var done = false
            while (!done) {
                let sampleBuffer: CMSampleBufferRef? = audioTrackOutput.copyNextSampleBuffer()
                if (sampleBuffer != nil) {
                    print("buffer")
                    //process
                } else {
                    if (reader.status == AVAssetReaderStatus.Failed) {
                        print("Error: ")
                        print(reader.error)
                    } else {
                        done = true
                    }
                }
            }
        }

0 个答案:

没有答案