Объединение видео и аудио: прозрачное / пустое видео экспортировано
Я пытаюсь объединить видео и аудио. Если я вставлю видео в свой основной пакет, то слияние видео в порядке. Но если я использую любое записанное видео (из каждой игры), тогда в экспортированном видео нет только аудио.
Я посмотрел свой связанный вопрос, но ни у одного из них нет такой же проблемы, как у меня. Любая идея, почему я получаю пустое видео со звуком, когда оно нормально работает с видео в основном комплекте? Это потому, что записанное видео имеет некоторые другие свойства?
Любая помощь будет оценена. Благодарю.
Код для объединения аудио (аудио m4a от AVRecorder) и видео (видео mp4 из everplay unity):
-(void)mergeAndSave//:(NSURL*) video_url
{
//Create AVMutableComposition Object which will hold our multiple AVMutableCompositionTrack or we can say it will hold our video and audio files.
AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];
//Now first load your audio file using AVURLAsset. Make sure you give the correct path of your videos.
//NSURL *audio_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Asteroid_Sound" ofType:@"mp3"]];
_audioAsset = [AVURLAsset URLAssetWithURL:recorder.url options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, _audioAsset.duration);
//Now we are creating the first AVMutableCompositionTrack containing our audio and add it to our AVMutableComposition object.
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[_audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
//Now we will load video file.
//NSURL *video_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Asteroid_Video" ofType:@"mp4"]];
_videoAsset = [AVURLAsset URLAssetWithURL:recorderURL options:nil];
NSLog(@"blublu : %@, %f",recorderURL,CMTimeGetSeconds(_videoAsset.duration) );
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,_audioAsset.duration);
//Now we are creating the second AVMutableCompositionTrack containing our video and add it to our AVMutableComposition object.
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
//decide the path where you want to store the final video created with audio and video merge.
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"FinalVideo%@.mov",[NSDate date]]];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
//Now create an AVAssetExportSession object that will save your final video at specified path.
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"conversion done at path : %@", [NSData dataWithContentsOfFile:outputFilePath]);
[[[CustomAlbum alloc]init] saveVideoWithPath:outputFileUrl];
});
}
];
}