Я хочу экспортировать фильм с AVAssetWriter
и не могу понять, как включить синхронизацию видео и аудио треков. Экспорт только видео работает отлично, но когда я добавляю звук, получившийся фильм выглядит следующим образом:
Сначала я вижу видео (без звука), затем видео зависает (показывая последний кадр изображения до конца), и через несколько секунд я слышу звук.
Я пробовал некоторые вещи с CMSampleBufferSetOutputPresentationTimeStamp
(вычитая первый CMSampleBufferGetPresentationTimeStamp
из текущего) для аудио, но все это не сработало, и я не думаю, что это правильное направление, поскольку видео и аудио в исходный фильм должен быть синхронизирован в любом случае...
Моя настройка вкратце: я создаю AVAssetReader
и 2 AVAssetReaderTrackOutput
(один для видео, один для звука) и добавляю их в AVAssetReader
, затем создаю AVAssetWriter
и 2 AVAssetWriterInput
( видео и аудио) и добавьте их в AVAssetWriter
... Я начинаю все с:
[assetReader startReading];
[assetWriter startWriting];
[assetWriter startSessionAtSourceTime:kCMTimeZero];
Затем я запускаю 2 очереди для выполнения буфера примера:
dispatch_queue_t queueVideo=dispatch_queue_create("assetVideoWriterQueue", NULL);
[assetWriterVideoInput requestMediaDataWhenReadyOnQueue:queueVideo usingBlock:^
{
while([assetWriterVideoInput isReadyForMoreMediaData])
{
CMSampleBufferRef sampleBuffer=[assetReaderVideoOutput copyNextSampleBuffer];
if(sampleBuffer)
{
[assetWriterVideoInput appendSampleBuffer:sampleBuffer];
CFRelease(sampleBuffer);
} else
{
[assetWriterVideoInput markAsFinished];
dispatch_release(queueVideo);
videoFinished=YES;
break;
}
}
}];
dispatch_queue_t queueAudio=dispatch_queue_create("assetAudioWriterQueue", NULL);
[assetWriterAudioInput requestMediaDataWhenReadyOnQueue:queueAudio usingBlock:^
{
while([assetWriterAudioInput isReadyForMoreMediaData])
{
CMSampleBufferRef sampleBuffer=[assetReaderAudioOutput copyNextSampleBuffer];
if(sampleBuffer)
{
[assetWriterAudioInput appendSampleBuffer:sampleBuffer];
CFRelease(sampleBuffer);
} else
{
[assetWriterAudioInput markAsFinished];
dispatch_release(queueAudio);
audioFinished=YES;
break;
}
}
}];
В основном цикле я жду обе очереди до тех пор, пока они не закончатся:
while(!videoFinished && !audioFinished)
{
sleep(1);
}
[assetWriter finishWriting];
Кроме того, я пытаюсь сохранить полученный файл в библиотеке со следующим кодом...
NSURL *url=[[NSURL alloc] initFileURLWithPath:path];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if([library videoAtPathIsCompatibleWithSavedPhotosAlbum:url])
{
[library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error)
{
if(error)
NSLog(@"error=%@",error.localizedDescription);
else
NSLog(@"completed...");
}];
} else
NSLog(@"error, video not saved...");
[library release];
[url release];
... но я получаю ошибку:
Video /Users/cb/Library/Application Support/iPhone Simulator/4.2/Applications/E9865BF9-D190-4912-9248-66768B1AB635/Documents/export.mp4 cannot be saved to the saved photos album: Error Domain=NSOSStatusErrorDomain Code=-12950 "Movie could not be played." UserInfo=0x5e4fb90 {NSLocalizedDescription=Movie could not be played.}
Код работает без проблем в другой программе. Так что-то не так с фильмом...?