帮助我理解 AVAssetWriter 中的 CMTime

发布于 2024-10-24 16:31:50 字数 253 浏览 1 评论 0原文

我很难理解如何使用 AVAssetWriter 将 30fps 的运动 JPEG 流转换为视频文件。我没有得到的部分是 [适配器appendPixelBuffer:buffer withPresentationTimeresentTime]方法。

如果我想输出 30fps mpeg4 视频,如何计算 withPresentationTime 值?

视频源是实时传输 30fps 运动 JPEG 的摄像机。

欣赏任何想法。

谢谢

I'm having a hard time understanding how to convert a stream of motion JPEG at 30fps using the AVAssetWriter to a video file. The part I'm not getting is the
[adaptor appendPixelBuffer:buffer withPresentationTimeresentTime] method.

How do I calculate the withPresentationTime value if I want to output 30fps mpeg4 video?

The video source is a camera that streams 30fps motion JPEG in real time.

Appreciate any idea.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-10-31 16:31:50

您将需要使用 CMTimeMake 生成 CMTime 结构。您需要将每一帧的时间增加 1/30 秒。

这是一个示意图:

CMTime time = CMTimeMake(0, 30); // (time, time_scale)

for(each image) {
  [adaptor appendPixelBuffer:buffer withPresentationTime:time]
  time.value += 1; 
}

时间设置如图所示,最小时间分辨率为 1/30 秒。时间 / time_scale = 1 秒。我不确定 H.264 是否有具体要求。 AVFoundation 在捕获时使用 1000000000(1,000,000,000 或 10 亿)的时间尺度(根据我的经验)。

更新:

只是为了回顾。从 CMTime 结构来看:

CMTimeValue value;  /*! @field value The value of the CMTime. value/timescale = seconds. */
CMTimeScale timescale;  /*! @field timescale The timescale of the CMTime. value/timescale = seconds.  */

时基在整个视频中保持不变。假设当前值为 10,时间刻度为 30。当前时间(以秒为单位)为 10/30 = 0.33333 秒。影片第 40 帧的时间值为 40/30 = 1.33333。因此,第 40 帧应在影片的 1.3333 秒处渲染。

我不确定这个时基是否适合 H.264 视频。我对规格不熟悉。我知道在捕获视频时,视频帧的呈现时间基数是 1000000000。从技术上讲,这应该不重要。时间是一个有理数——1000000000 / 1000000000 = 1 秒,30 / 30 = 1 秒。

You will need to generate a CMTime structure using CMTimeMake. You will need to increment the time by 1/30 of a second for each frame.

Here is a sketch:

CMTime time = CMTimeMake(0, 30); // (time, time_scale)

for(each image) {
  [adaptor appendPixelBuffer:buffer withPresentationTime:time]
  time.value += 1; 
}

With the time setup as shown,the smallest time resolution is 1/30 of a second. time / time_scale = 1 second. I am not certain if there is a specific requirement for H.264. AVFoundation uses a time scale of 1000000000 (1,000,000,000 or 1 billion) when capturing (in my experience).

Update:

Just to review. From the CMTime struct:

CMTimeValue value;  /*! @field value The value of the CMTime. value/timescale = seconds. */
CMTimeScale timescale;  /*! @field timescale The timescale of the CMTime. value/timescale = seconds.  */

The timebase would stay the same throughout the video. Let say you have a current value of 10 with a time scale of 30. The current time in seconds is 10/30 = 0.33333 seconds. The time value for the 40th frame of your movie is 40/30 = 1.33333. So the 40th frame should render at 1.3333 seconds into the movie.

I am not sure if this time base is appropriate for an H.264 video. I am not familiar with the spec. I know when capturing video the presentation time base for video frames is 1000000000. Technically it should not matter. The time is a rational number -- 1000000000 / 1000000000 = 1 second and 30 / 30 = 1 second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文