我以为我可能会使用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
}
}
}
}