Merge video and audio: exported transparent / blank video

I am trying to combine video and audio. If I embed the video in my main package, then the video merge is fine. But if I use any recorded video (from every playback) then the exported video doesn't have video only audio.

I looked at my related question, but I don't have the same problem as mine. Any idea why I am getting a blank video with audio when it works fine with videos in the master kit? Is it because the recorded video has several different properties?

Any help would be greatly appreciated. Thank.

Code to combine audio (m4a audio with AVRecorder) and video (mp4 video from play 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];
         });
     }
     ];
}

      

0


source to share





All Articles