从视频文件中删除音频 [英] Remove audio from a video file
问题描述
我正在尝试从手机资料库中的MOV类型视频中删除音轨.我知道我可以在播放时使音频静音,但是我计划上传用户视频,将音频取出并减小文件大小是很有意义的.
I'm trying to remove the audio track from a MOV type video in my phone's library. I know I can mute the audio on playback, but I plan to upload user videos and it would just make sense to take the audio out and reduce file size.
我已尝试从
I've tried converting obj-c code from THIS ANSWER to swift, but I either messed up with conversion or it just doesn't take out the audio from the file.
任何帮助将不胜感激.
推荐答案
最受好评的答案对我不起作用+我在视频轮播方面遇到问题.我最终通过添加参数muteSound: Bool
修改了我的视频压缩方法.
The top upvoted answer didn't work for me + I had issues with video rotation. I ended up modifying the video compression method I had by adding a parameter muteSound: Bool
.
我使用比特率2300000
来使视频看起来不错,但其大小是原始视频的3-4倍.
I use a bitrate of 2300000
to keep the video looking good but 3-4 times smaller in size than original.
我将保持开放性,并在大多数情况下将标记为正确的问题标记为正确.也许有人可以提出更简单的建议.
I'll keep the question open and mark most upvoted one as correct. Maybe someone can come up with something simpler.
func compressVideo(inputURL: NSURL, outputURL: NSURL, bitRate: Int, muteSound: Bool, onDone: () -> ()) {
let videoAsset = AVURLAsset(URL: inputURL, options: nil)
let videoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
let videoSize = videoTrack.naturalSize
let videoWriterCompressionSettings = [
AVVideoAverageBitRateKey: bitRate
]
let videoWriterSettings:[String : AnyObject] = [
AVVideoCodecKey : AVVideoCodecH264,
AVVideoCompressionPropertiesKey : videoWriterCompressionSettings,
AVVideoWidthKey : Int(videoSize.width),
AVVideoHeightKey : Int(videoSize.height)
]
let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoWriterSettings)
videoWriterInput.expectsMediaDataInRealTime = true
videoWriterInput.transform = videoTrack.preferredTransform
let videoWriter = try! AVAssetWriter(URL: outputURL, fileType: AVFileTypeQuickTimeMovie)
videoWriter.addInput(videoWriterInput)
let videoReaderSettings:[String : AnyObject] = [
kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
]
let videoReaderOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: videoReaderSettings)
let videoReader = try! AVAssetReader(asset: videoAsset)
videoReader.addOutput(videoReaderOutput)
let audioWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeAudio, outputSettings: nil)
audioWriterInput.expectsMediaDataInRealTime = false
videoWriter.addInput(audioWriterInput)
let audioTrack = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0]
let audioReaderOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings: nil)
let audioReader = try! AVAssetReader(asset: videoAsset)
if muteSound == false {
audioReader.addOutput(audioReaderOutput)
}
videoWriter.startWriting()
videoReader.startReading()
videoWriter.startSessionAtSourceTime(kCMTimeZero)
let processingQueue = dispatch_queue_create("processingQueue1", nil)
videoWriterInput.requestMediaDataWhenReadyOnQueue(processingQueue, usingBlock: {() -> Void in
while videoWriterInput.readyForMoreMediaData {
let sampleBuffer:CMSampleBuffer? = videoReaderOutput.copyNextSampleBuffer()
if videoReader.status == .Reading && sampleBuffer != nil {
videoWriterInput.appendSampleBuffer(sampleBuffer!)
} else {
videoWriterInput.markAsFinished()
if videoReader.status == .Completed {
if muteSound {
// return without sound
videoWriter.finishWritingWithCompletionHandler({() -> Void in
onDone()
})
} else {
audioReader.startReading()
videoWriter.startSessionAtSourceTime(kCMTimeZero)
let processingQueue = dispatch_queue_create("processingQueue2", nil)
audioWriterInput.requestMediaDataWhenReadyOnQueue(processingQueue, usingBlock: {() -> Void in
while audioWriterInput.readyForMoreMediaData {
let sampleBuffer:CMSampleBufferRef? = audioReaderOutput.copyNextSampleBuffer()
if audioReader.status == .Reading && sampleBuffer != nil {
audioWriterInput.appendSampleBuffer(sampleBuffer!)
} else {
audioWriterInput.markAsFinished()
if audioReader.status == .Completed {
videoWriter.finishWritingWithCompletionHandler({() -> Void in
onDone()
})
}
}
}
})
}
}
}
}
})
}
这篇关于从视频文件中删除音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!