Как написать фильм с видео и аудио с помощью AVAssetWriter?
Я хочу экспортировать фильм с 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.}
Код работает без проблем в другой программе. Так что-то не так с фильмом...?
Ответы
Ответ 1
-(void)mergeAudioVideo
{
NSString *videoOutputPath=[_documentsDirectory stringByAppendingPathComponent:@"dummy_video.mp4"];
NSString *outputFilePath = [_documentsDirectory stringByAppendingPathComponent:@"final_video.mp4"];
if ([[NSFileManager defaultManager]fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager]removeItemAtPath:outputFilePath error:nil];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
NSString *filePath = [_documentsDirectory stringByAppendingPathComponent:@"newFile.m4a"];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSURL *audio_inputFileUrl = [NSURL fileURLWithPath:filePath];
NSURL *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
if (_assetExport.status == AVAssetExportSessionStatusCompleted) {
//Write Code Here to Continue
}
else {
//Write Fail Code here
}
}
];
}
Вы можете использовать этот код для объединения аудио и видео.
Ответ 2
Он сглаживает этот ресурсWriterAudioInput игнорирует время буфера выборки для записи звука.
Сделайте это.
1) Запись видеодорожки.
2) По завершении отметьте его законченным, т.е. [videoWriterInput markAsFinished];
3) do [assetWriter startSessionAtSourceTime: timeRangeStart];
3) создать экземпляр звукового считывателя и начать запись звука.