8

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?

eddy
  • 693
  • 1
  • 7
  • 17
  • 3
    are you setting this key "AVURLAssetPreferPreciseDurationAndTimingKey" to [NSNumber numberWithBool:YES] for the asset options when creating a AVURLAsset? – Jesse Black Oct 19 '12 at 22:34
  • Yes, I do set the "AVURLAssetPreferPreciseDurationAndTimingKey" to [NSNumber numberWithBool:YES] – eddy Oct 22 '12 at 17:05
  • By your description, I would guess the problem has to do with the way you are setting the timescales for both your video and audio tracks. But I would have to see your composition code to be able to help. – MiguelB Oct 26 '12 at 22:17
  • Seems to be I have the same problem. I'm trying to combine many video and audio files in one video. Have you found a solution for this problem? – mike-dutka Jun 19 '14 at 11:09
  • were you able to solve this? I'm currently experiencing the same issue and start to think it has something to do with the audio track/file beeing VBR encoded ... CBR files work fine – Tim Specht Aug 18 '14 at 07:29

1 Answers1

2

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Diegum
  • 55
  • 4