【问题标题】:Slow Motion Video with Audio using frames approach - iOS使用帧方法的带有音频的慢动作视频 - iOS
【发布时间】:2020-04-28 08:09:07
【问题描述】:

我必须使用基于帧的方法将视频转换为带有音频的慢动作。 以下链接有很多帮助 Reverse-AVAsset-Efficient

在解决方案中我可以更改缓冲区的时间戳并实现慢动作视频。

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)
}

对于音频慢动作,我尝试更改音频样本的时间戳,但没有效果。

音频慢动作有什么解决方案。

如果您有 Objective-C 解决方案,请随时发布。 谢谢。

【问题讨论】:

    标签: ios swift avfoundation core-audio


    【解决方案1】:

    您可以使用AVMutableComposition 做一个简单的慢动作。正如@hotpaw2 所提到的,减慢音频速度的方法不止一种——例如,您可以降低音高或保持音高不变。这个解决方案似乎保持不变,我看不到任何改变它的方法。也许这就是你想要的。也许不吧。

    您可以使用AVAssetExportSession 将慢速视频写入文件,因为 AVMutableComposition 是(可能令人惊讶的)AVAsset 的子类,即使您没有导出视频的慢动作版本,也可以使用 AVPlayer 预览结果。

    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!)")
    }
    

    【讨论】:

      【解决方案2】:

      根据您希望音频听起来像什么,您可以尝试使用手动为 Apple 的时间音高音频单元之一提供音频样本数据,以更改其长度以匹配新时间戳之间的时间。

      【讨论】:

      • 我只是想要包含慢动作音频的慢动作/快动作视频。更改视频帧工作正常,但音频不是。
      猜你喜欢
      • 2013-08-04
      • 2019-08-21
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2020-12-30
      相关资源
      最近更新 更多