播放带有AVPlayer音频的AVMutableComposition不同步 [英] Playing an AVMutableComposition with AVPlayer audio gets out of sync

查看:105
本文介绍了播放带有AVPlayer音频的AVMutableComposition不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AVMutableComposition,其中包含2条音频轨道和1条视频轨道.我正在使用合成将.mov文件中的大约40个不同的视频剪辑字符串化,将每个剪辑的视频内容放入合成的视频轨道中,并将音频放入音频轨道中.我用于音乐的第二条音轨. 我还为标题图形提供了一个同步层.

I have an AVMutableComposition with 2 audio tracks and one video track. I'm using the composition to string about 40 different video clips from .mov files, putting the video content of each clip in the video track of my composition and the audio in the audio track. The second audio track I use for music. I also have a synchronized layer for titles graphics.

当我使用AVPlayer播放此作品时,音频会慢慢失去同步.大约需要4分钟才能开始引起注意.似乎如果我只把一小段较长的剪辑串在一起,问题就不那么明显了,而是当有许多剪辑的较短的剪辑(在我的测试中为40个左右)时,它真的变得很糟糕.

When I play this composition using an AVPlayer, the audio slowly gets out of sync. It takes about 4 minutes to start becoming noticeable. It seems like if I only string together a handfull of longer clips the problem is not as apparent, it is when there are many clips shorter clips (~40 in my test) that it gets really bad.

暂停和播放不会重新同步音频,但是搜索会重新同步.换句话说,如果我将视频播放到末尾,那么即使我暂停播放并一直播放,到了最后,口型同步也会明显关闭,但是,如果我想在末尾播放一段时间,则音频会恢复同步.

Pausing and Playing doesn't re-sync the audio, however seeking does. In other words, if I let the video play to the end, towards the end the lip sync gets noticeably off even if I pause and play throughout, however, if I seek to a time towards the end the audio gets back in sync.

我目前的hacky解决方案是每分钟左右搜寻一次currentTime + 1帧.这会由于搜索操作的延迟而在视频中造成令人不快的跳跃,因此不是一个好的解决方案.

My hacky solution for now is to seek to the currentTime + 1 frame every minute or so. This creates an unpleasant jump in the video caused by a lag in the seek operation, so not a good solution.

使用ExportSession导出不会出现此问题,音频在输出影片中保持同步.

Exporting with an ExportSession doesn't present this problem, audio remains in sync in the output movie.

我想知道AVPlayer中新的masterClock属性是否可以解决此问题,如果可以,如何使用它?

I'm wondering if the new masterClock property in the AVPlayer is the answer to this, and if it is, how is it used?

推荐答案

我遇到了相同的问题,并通过以下方式指定了时间刻度,从而解决了许多其他音频和视频问题:

I had the same issue and fixed it, among many other audio and video things, by specifying times timescales in the following manner:

CMTime(seconds: my_seconds, preferredTimescale: CMTimeScale(600))

之前,我的时间范围是CMTimeScale(NSEC_PER_SEC).在以不同的帧频组成剪辑时,这让我感到紧张不安,再加上Eddy在此处提到的音频不同步.

Before, my time scale was CMTimeScale(NSEC_PER_SEC). That caused me jittery when composing clips at a different frame rate, plus this audio out-of-sync that Eddy mentions here.

尽管看起来像一个魔术数字,但600是24、30、60和120的公倍数.这是出于不同目的的常用帧速率.共同的倍数可以避免在编写多个剪辑时四舍五入的问题.

In spite of looking like a magic number, 600 is a common multiple of 24, 30, 60 and 120. These are usual frame rates for different purposes. The common multiple avoids dragging around rounding problems when composing multiple clips.

这篇关于播放带有AVPlayer音频的AVMutableComposition不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆