swift 使用帧方法的慢动作视频与音频- iOS

rta7y2nd  于 2024-01-05  发布在  Swift
关注(0)|答案(3)|浏览(176)

我必须转换视频到慢动作与音频使用帧基地的方法。以下链接是很多帮助是Reverse-AVAsset-Efficient
在解决方案中,我可以改变缓冲区的时间戳,实现慢动作视频。

  1. for sample in videoSamples {
  2. var presentationTime = CMSampleBufferGetPresentationTimeStamp(sample)
  3. //Changing Timestamp to achieve slow-motion
  4. presentationTime.timescale = presentationTime.timescale * 2
  5. let imageBufferRef = CMSampleBufferGetImageBuffer(sample)
  6. while !videoWriterInput.isReadyForMoreMediaData {
  7. Thread.sleep(forTimeInterval: 0.1)
  8. }
  9. pixelBufferAdaptor.append(imageBufferRef!, withPresentationTime: presentationTime)
  10. }

字符串
对于音频慢动作,我试图更改音频样本的时间戳,但没有效果。
是否有任何解决方案音频慢动作。
如果你有解决方案,请随时发布。谢谢。

hwazgwia

hwazgwia1#

你可以使用AVMutableComposition来做一个简单的慢动作。正如@hotpaw2所提到的,有不止一种方法可以减慢你的音频-例如,你可以降低音调或保持恒定。这个解决方案似乎可以保持恒定,我看不出有任何方法可以改变这一点。也许这就是你想要的。也许不是。
您可以使用AVAssetExportSession将慢动作视频写入文件,由于AVMutableCompositionAVAsset的子类(可能令人惊讶),因此即使您尚未导出视频的慢动作版本,也可以使用AVPlayer预览结果。

  1. let asset = AVURLAsset(url: Bundle.main.url(forResource: "video", withExtension: "mp4")! , options : nil)
  2. let srcVideoTrack = asset.tracks(withMediaType: .video).first!
  3. let srcAudioTrack = asset.tracks(withMediaType: .audio).first!
  4. let sloMoComposition = AVMutableComposition()
  5. let sloMoVideoTrack = sloMoComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)!
  6. let sloMoAudioTrack = sloMoComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)!
  7. let assetTimeRange = CMTimeRange(start: .zero, duration: asset.duration)
  8. try! sloMoVideoTrack.insertTimeRange(assetTimeRange, of: srcVideoTrack, at: .zero)
  9. try! sloMoAudioTrack.insertTimeRange(assetTimeRange, of: srcAudioTrack, at: .zero)
  10. let newDuration = CMTimeMultiplyByFloat64(assetTimeRange.duration, multiplier: 2)
  11. sloMoVideoTrack.scaleTimeRange(assetTimeRange, toDuration: newDuration)
  12. sloMoAudioTrack.scaleTimeRange(assetTimeRange, toDuration: newDuration)
  13. // you can play sloMoComposition in an AVPlayer at this point
  14. // Export to a file using AVAssetExportSession
  15. let exportSession = AVAssetExportSession(asset: sloMoComposition, presetName: AVAssetExportPresetPassthrough)!
  16. exportSession.outputFileType = .mp4
  17. exportSession.outputURL = getDocumentsDirectory().appendingPathComponent("slow-mo-\(Date.timeIntervalSinceReferenceDate).mp4")
  18. exportSession.exportAsynchronously {
  19. assert(exportSession.status == .completed)
  20. print("File in \(exportSession.outputURL!)")
  21. }

字符串

展开查看全部
0kjbasz6

0kjbasz62#

根据Gordon Child的回答,一旦你有了他所描述的内容,你就可以通过使用this thread中描述的AudioMix基础设施来更改作曲的音高校正策略,因为AVMutableAudioMixInputParameters有一个可设置的AVAudioTimePitchAlgorithm。我证实了这是可行的。

  1. let audioTracks = composition.tracks(withMediaType: .audio) // AVMutableComposition
  2. var trackMixArray = [AVMutableAudioMixInputParameters]()
  3. for audioTrack in audioTracks {
  4. var trackMix = AVMutableAudioMixInputParameters(track: audioTrack)
  5. trackMix.audioTimePitchAlgorithm = .varispeed
  6. trackMix.setVolume(volume, at: .zero)
  7. }
  8. trackMixArray.append(trackMix)
  9. let audioMix = AVMutableAudioMix()
  10. audioMix.inputParameters = trackMixArray
  11. export.audioMix = audioMix // AVAssetExportSession

字符串

ifsvaxew

ifsvaxew3#

根据你想要的音频听起来像什么,你可以尝试使用手动饲料与音频样本数据的苹果的时间间距音频单位之一,以改变其长度,以匹配您的新时间戳之间的时间。

相关问题