3

My question is: How to merge video and audio files that has almost the same duration?

I searched and got some answers to this question. However when I try the code they gave, it just does not produce a "non-zero byte" movie.

Could you take a look at it and see where it went wrong?

-(void)putTogether
{
NSLog(@"Starting to put together all the files!");

AVMutableComposition *mixComposition = [AVMutableComposition composition];

NSString *audioPath = @"/Users/admin/Documents/Sound.caf";
NSURL *audioUrl = [NSURL fileURLWithPath:audioPath];
//AVURLAsset *audioasset = [AVURLAsset URLAssetWithURL:audioUrl options:nil];
AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];

NSString *videoPath = @"/Users/admin/Documents/video.mp4";
NSURL *videoUrl = [NSURL fileURLWithPath:videoPath];
//AVURLAsset *videoasset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
AVURLAsset *videoasset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];

NSString *moviepath = @"/Users/admin/Documents/fmovie.mov";
NSURL *movieUrl = [NSURL fileURLWithPath:moviepath];

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

AVMutableCompositionTrack *compositionTrackB = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrackB = [[videoasset tracksWithMediaType:AVMediaTypeVideo] lastObject];
[compositionTrackB insertTimeRange:CMTimeRangeMake(      kCMTimeZero, videoasset.duration)  ofTrack:clipVideoTrackB atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionTrackA = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipAudioTrackA = [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject];
[compositionTrackA insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioasset.duration)  ofTrack:clipAudioTrackA atTime:kCMTimeZero error:nil];

AVAssetExportSession *exporter =[[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
//AVAssetExportSession *exporter =[AVAssetExportSession exportSessionWithAsset:mixComposition presetName:AVAssetExportPresetLowQuality];
NSParameterAssert(exporter!=nil);
exporter.outputFileType=AVFileTypeQuickTimeMovie;
exporter.outputURL=movieUrl;
CMTime start=CMTimeMake(0, 600);
CMTime duration=CMTimeMake(600, 600);
CMTimeRange range=CMTimeRangeMake(start, duration);
exporter.timeRange=range;
[exporter exportAsynchronouslyWithCompletionHandler:nil];
}
martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • thanks @user1192409. this post is so helpful to me. just one question is there anyway to know when the writing is finished.? – KDeogharkar May 08 '12 at 06:55
  • Hi, Can you please help me? I have used this code but not able to get video in merging file. I am able to get AVAssetExportSessionStatusCompleted status for assetSession but not able to see the video. I have checked my video file and it is proper.. also my audio file is proper... but when merging both video is not getting displayed.... Can you tell me what could be the problem?? – DShah Feb 20 '13 at 09:27

1 Answers1

0

(Answered by the OP by an edit in the question. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

I solved my problem.

For the ones who have same problem as I did, here is the simple solution:

a.Delete the following code:

CMTime start=CMTimeMake(0, 600);
CMTime duration=CMTimeMake(600, 600);
CMTimeRange range=CMTimeRangeMake(start, duration);
exporter.timeRange=range;

b.(optional)Rewrite completion handler:

[exporter exportAsynchronouslyWithCompletionHandler:^{ 
    switch ([exporter status]) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [[exporter error] localizedDescription]);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
        default:
            break;
    } 
}];

c.Then wait long enough for the program to complete writing.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129