Playing an AVMutableComposition with AVPlayer audio gets out of sync

女生的网名这么多〃 提交于 2020-08-24 07:16:11

问题


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.

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.

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.

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

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


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/12983315/playing-an-avmutablecomposition-with-avplayer-audio-gets-out-of-sync

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!