AVAssetWriter AVVideoExpectedSourceFrameRateKey(帧速率)被忽略 [英] AVAssetWriter AVVideoExpectedSourceFrameRateKey (frame rate) ignored
问题描述
我和我的团队正在尝试通过更改视频帧速率将视频文件重新编码为更gify的感觉。我们使用以下属性 AVAssetWriterInput
:
Me and my team are trying to re-encode a video file to a more "gify" feeling by changing the video frame rate. We are using the following properties for the AVAssetWriterInput
:
let videoSettings:[String:Any] = [
AVVideoCodecKey: AVVideoCodecH264,
AVVideoHeightKey: videoTrack.naturalSize.height,
AVVideoWidthKey: videoTrack.naturalSize.width,
AVVideoCompressionPropertiesKey: [AVVideoExpectedSourceFrameRateKey: NSNumber(value: 12)]
]
但输出视频继续以正常帧率播放(使用 AVPlayer
播放)。
But the output video keep playing in the normal frame rate (played using AVPlayer
).
降低视频帧率的正确方法是什么? (例如12)。
任何正确方向的帮助都会被高度重视。我们卡住了。
祝您好运,
Roi
推荐答案
您可以控制每个样品的时间使用 CMSampleBufferCreateCopyWithNewTiming
直接附加到 AVAssetWriterInput
。
You can control the timing of each sample you append to your AVAssetWriterInput
directly with CMSampleBufferCreateCopyWithNewTiming
.
你需要调整您提供的 CMSampleTimingInfo
中的时间。
使用 CMSampleBufferGetOutputSampleTimingInfoArray
检索当前时间信息,然后查看每个样本的持续时间并计算正确的持续时间以获得每秒12帧并调整表示和解码时间戳到匹配这个新的持续时间
然后您制作副本并将其提供给作者的输入。
You need to adjust the timing in the CMSampleTimingInfo
you provide.
Retrieve current timing info with CMSampleBufferGetOutputSampleTimingInfoArray
and just go over the duration of each sample and calculate the correct duration to get 12 frames per second and adjust presentation and decode timestamps to match this new duration.
You then make your copy and feed it to your writer's input.
假设你有 existingSampleBuffer
:
CMSampleBufferRef sampleBufferToWrite = NULL;
CMSampleTimingInfo sampleTimingInfo = {0};
CMSampleBufferGetSampleTimingInfo(existingSampleBuffer, 0, &sampleTimingInfo);
// modify duration & presentationTimeStamp
sampleTimingInfo.duration = CMTimeMake(1, 12) // or whatever frame rate you desire
sampleTimingInfo.presentationTimeStamp = CMTimeAdd(previousPresentationTimeStamp, sampleTimingInfo.duration);
previousPresentationTimeStamp = sampleTimingInfo.presentationTimeStamp; // should be initialised before passing here the first time
OSStatus status = CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, existingSampleBuffer, 1, &sampleTimingInfo, &sampleBufferToWrite);
if (status == noErr) {
// you can write sampleBufferToWrite
}
我在这段代码中做了一些假设:
I'm making some assumptions in this code:
- SampleBuffer只包含一个样本
- SampleBuffer包含未压缩的视频(否则,您还需要处理decodeTimeStamp)
这篇关于AVAssetWriter AVVideoExpectedSourceFrameRateKey(帧速率)被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!