问题描述
我必须使用基于帧的方法将视频转换为带音频的慢动作.以下链接是很多帮助,这是反向-AVAsset-高效
I have to convert video to slow motion with audio using frame base approach.Following links were lot of help which areReverse-AVAsset-Efficient
在解决方案中,我可以更改缓冲区的时间戳并获得慢动作视频.
In solution I can change the timestamp of the buffer and achieve slow motion video.
for sample in videoSamples {
var presentationTime = CMSampleBufferGetPresentationTimeStamp(sample)
//Changing Timestamp to achieve slow-motion
presentationTime.timescale = presentationTime.timescale * 2
let imageBufferRef = CMSampleBufferGetImageBuffer(sample)
while !videoWriterInput.isReadyForMoreMediaData {
Thread.sleep(forTimeInterval: 0.1)
}
pixelBufferAdaptor.append(imageBufferRef!, withPresentationTime: presentationTime)
}
对于音频慢动作,我尝试更改音频样本的时间戳,但没有任何效果.
for audio slow motion I tried to change the timestamp of audio sample but it has no effect.
是否有用于音频慢动作的解决方案.
Is there any solution for Audio slow-motion.
如果您有Objective-C解决方案,请随时发布.谢谢.
If you have Objective-C solution, feel free to post it.Thanks.
推荐答案
您可以使用 AVMutableComposition
轻松慢动作.正如@ hotpaw2所提到的,有多种方法可以降低音频速度-例如,您可以降低音调或使其保持恒定.这种解决方案似乎可以保持不变,我看不出有任何方法可以改变它.也许这就是您想要的.也许不会.
You can do an easy slow motion using AVMutableComposition
. As @hotpaw2 mentioned, there is more than one way to slow down your audio - for example you could lower the pitch or keep it constant. This solution seems to keep it constant and I don't see any way to change that. Maybe that's what you want. Maybe not.
您可以使用 AVAssetExportSession
将慢速视频写到文件中,并且 AVMutableComposition
是 AVAsset
的子类(也许令人惊讶),即使您没有导出慢动作版本,也可以使用 AVPlayer
预览结果.的视频.
You can use AVAssetExportSession
to write the slow video out to a file, and sinceAVMutableComposition
is (perhaps surprisingly) a subclass of AVAsset
, you can preview the results using AVPlayer
even if you haven't exported the slow motion version of the video.
let asset = AVURLAsset(url: Bundle.main.url(forResource: "video", withExtension: "mp4")! , options : nil)
let srcVideoTrack = asset.tracks(withMediaType: .video).first!
let srcAudioTrack = asset.tracks(withMediaType: .audio).first!
let sloMoComposition = AVMutableComposition()
let sloMoVideoTrack = sloMoComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)!
let sloMoAudioTrack = sloMoComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)!
let assetTimeRange = CMTimeRange(start: .zero, duration: asset.duration)
try! sloMoVideoTrack.insertTimeRange(assetTimeRange, of: srcVideoTrack, at: .zero)
try! sloMoAudioTrack.insertTimeRange(assetTimeRange, of: srcAudioTrack, at: .zero)
let newDuration = CMTimeMultiplyByFloat64(assetTimeRange.duration, multiplier: 2)
sloMoVideoTrack.scaleTimeRange(assetTimeRange, toDuration: newDuration)
sloMoAudioTrack.scaleTimeRange(assetTimeRange, toDuration: newDuration)
// you can play sloMoComposition in an AVPlayer at this point
// Export to a file using AVAssetExportSession
let exportSession = AVAssetExportSession(asset: sloMoComposition, presetName: AVAssetExportPresetPassthrough)!
exportSession.outputFileType = .mp4
exportSession.outputURL = getDocumentsDirectory().appendingPathComponent("slow-mo-\(Date.timeIntervalSinceReferenceDate).mp4")
exportSession.exportAsynchronously {
assert(exportSession.status == .completed)
print("File in \(exportSession.outputURL!)")
}
这篇关于使用帧方法的带音频慢动作视频-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!