Merging two m4v Movie Files Using AVMutableComposition - Videos Will Not Merge

佐手、 提交于 2019-12-04 04:57:52

问题


I am using the below code to try and merge two m4v files stored in the documents folder :

CMTime insertionPoint = kCMTimeZero;
NSError * error = nil;

AVMutableComposition *composition = [AVMutableComposition composition];

AVURLAsset* asset = [AVURLAsset URLAssetWithURL: [assetURLArray objectForKey:kIntroVideo] options:nil];

if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) 
                          ofAsset:asset 
                           atTime:insertionPoint 
                            error:&error]) 
{
    NSLog(@"error: %@",error);
}

insertionPoint = CMTimeAdd(insertionPoint, asset.duration);

AVURLAsset* asset2 = [AVURLAsset URLAssetWithURL: [assetURLArray objectForKey:kMainVideo] options:nil];

if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset2.duration) 
                          ofAsset:asset2 
                           atTime:insertionPoint 
                            error:&error]) 
{
    NSLog(@"error: %@",error);
}

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

NSString *exportVideoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/FinishedVideo.m4v"];

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch (exportSession.status) {
        case AVAssetExportSessionStatusFailed:{
            NSLog (@"FAIL");
            break;
        }
        case AVAssetExportSessionStatusCompleted: {
            NSLog (@"SUCCESS");
}
};
 }]; 
}

The problem is that the two videos will not merge properly. The total merged movie duration is correct, however the video never transitions to the second movie and continues to display the last frame of the first movie for its duration. Oddly I can hear the audio for the second video playing in the background.

Does anyone have any ideas what is wrong ?

EDIT - The odd thing is is that if I merge two clips of exactly the same length it works.

EDIT - Have tried changing file extension to .mov with same problem.


回答1:


You havent set the composition to the exportSession.

After the line:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

Add this line

exportSession.videoComposition = composition;

This should solve your problem.




回答2:


Ok - so I eventually got this working by using individual AVMutableComposition tracks and then setting a mutablecomposition for audio and one for video.



来源:https://stackoverflow.com/questions/10285007/merging-two-m4v-movie-files-using-avmutablecomposition-videos-will-not-merge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!