Ruchir Shah
Ruchir Shah

Reputation: 896

Merge audio and video/image to create a movie file

I want to merge an Audion CAF file and a video/image UIImage to create a movie file (in .mov format).

Say that my audio is 30 seconds long and I have a UIImage; I want to create a .mov file such that the UIImage is displayed the entire time the audio is playing.

I found this reference: How to add audio to video file on iphone SDK

Can anyone tell me, is it helpful in my case, since the length of my audio and image/video is different?

Thanks in advance.

Upvotes: 2

Views: 5409

Answers (3)

Sishu
Sishu

Reputation: 1558

use this i found this somewhere in net, i don't remember,....

NSString  *fileNamePath = @"audio.caf";
    NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths  objectAtIndex:0];
    NSString  *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath];
    NSURL *audioUrl = [NSURL fileURLWithPath:oldappSettingsPath];   
    NSString  *fileNamePath1 = @"output.mp4";
    NSArray   *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString  *documentsDirectory1 = [paths1  objectAtIndex:0];
    NSString  *oldappSettingsPath1 = [documentsDirectory1 stringByAppendingPathComponent:fileNamePath1];
    NSLog(@"oldpath=%@",oldappSettingsPath);
    NSURL *videoUrl = [NSURL fileURLWithPath:oldappSettingsPath1];
    if (avPlayer.duration >0.00000)
    {
        NSLog(@"SOMEDATA     IS THERE ");
        AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
        AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];

        AVMutableComposition* mixComposition = [AVMutableComposition composition];

        NSLog(@"audio =%@",audioAsset);
        AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

        AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];


        AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];   

        NSString* videoName = @"export.mov";

        NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
        NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];

        if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
        {
            [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
        }

        _assetExport.outputFileType = @"com.apple.quicktime-movie";
        NSLog(@"file type %@",_assetExport.outputFileType);
        _assetExport.outputURL = exportUrl;
        _assetExport.shouldOptimizeForNetworkUse = YES;



        [_assetExport exportAsynchronouslyWithCompletionHandler:
         ^(void ) 
        {      

            NSString  *fileNamePath = @"sound_record.mov";
            NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths  objectAtIndex:0];
            NSString  *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath];


            //             if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) {
//                 
//                 NSFileManager *fileManager = [NSFileManager defaultManager];  
//                 [fileManager removeItemAtPath: oldappSettingsPath error:NULL];
//                 
//             }
             NSURL *documentDirectoryURL = [NSURL fileURLWithPath:oldappSettingsPath];
             [[NSFileManager defaultManager] copyItemAtURL:exportUrl toURL:documentDirectoryURL error:nil];
             [audioAsset release];
             [videoAsset release];
             [_assetExport release];
         }       
         ];

Upvotes: 0

Alex Chugunov
Alex Chugunov

Reputation: 748

Yes, you should use AVMutableComposition. To create the video track from your UIImage use AVAssetWriter.

Upvotes: 1

Helge Becker
Helge Becker

Reputation: 3253

Quicktime Pro can do this. Done that for my own app. You create the movie from the images. Quicktime offers to read a sequence of images and creates a movie from it. He ask for the FPS during import as well.

The audio track can then simply merged, pasted somewhere or scaled to a selected range of the movie.

Upvotes: 0

Related Questions