合成的视频和音频使用AVMutableComposition(Composing video and

2019-08-06 05:04发布

我有一个奇怪的问题。 在我的应用程序,我结合使用下面的代码多种音频和视频文件。 将得到的视频似乎很好地工作,一旦我下载了它从设备到计算机,并用快速的时间玩,但每当我试图发挥使用任何一个UIWebView或AVPLayer新组成的视频,我只能看到合并后的视频文件的第一部分。

此外,当我试图使用的MPMoviePlayerController发挥它挂在“加载”。

我能听到所有的声音组成。 为了明确这一点,我有两个阵列:1- audioPieces与路径的音频文件[松1,song2,song3]; 2- moviePieces与路径的视频文件[MOVIE1,电影2,MOVIE3]; 合并这些文件后,我只能看到MOVIE1,但我能听到松1 + song2 + song3。 PS歌曲和电影有不同的长度(小于0.2秒的差异)。 任何帮助将不胜感激。 谢谢你在前进,雅努什

-(void)putFilesTogether{
AVMutableComposition *mixComposition = [AVMutableComposition composition]; 
AVMutableCompositionTrack *videoCompositionTrack =[[AVMutableCompositionTrack alloc]init];
AVMutableCompositionTrack *audioCompositionTrack =[[AVMutableCompositionTrack alloc]init];

NSLog(@" movie  %@ audio %@ ", moviePieces, audioPieces);


NSError * error;
for(int i=0;i<moviePieces.count;i++)
{
    NSFileManager * fm = [NSFileManager defaultManager];
    NSString * movieFilePath;
    NSString * audioFilePath;
    movieFilePath = [moviePieces objectAtIndex:i];
    audioFilePath = [audioPieces objectAtIndex:i];


    if(![fm fileExistsAtPath:movieFilePath]){
        NSLog(@"Movie doesn't exist %@ ",movieFilePath);
    }
    else{
        NSLog(@"Movie exist %@ ",movieFilePath);
    }

    if(![fm fileExistsAtPath:audioFilePath]){
        NSLog(@"Audio doesn't exist %@ ",audioFilePath);
    }
    else{
        NSLog(@"Audio exists %@ ",audioFilePath);
    }


   NSURL *videoUrl = [NSURL fileURLWithPath:movieFilePath];
   NSURL *audioUrl = [NSURL fileURLWithPath:audioFilePath];


    AVURLAsset *videoasset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];
     AVAssetTrack *videoAssetTrack= [[videoasset tracksWithMediaType:AVMediaTypeVideo] lastObject];

    AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
    AVAssetTrack *audioAssetTrack= [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject];

    videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    CMTime tempTime = mixComposition.duration;

    [audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioasset.duration) ofTrack:audioAssetTrack atTime:tempTime error:&error];

    [videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoasset.duration) ofTrack:videoAssetTrack atTime:tempTime error:&error];

    if(error)
    {
        NSLog(@"Ups. Something went wrong! %@", [error debugDescription]);
    }
}


NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];

float ran = arc4random()%1000;
NSString * pathToSave = [NSString stringWithFormat:@"Output%@%f.mp4",caldate,ran];
pathToSave =[DOCUMENTS_FOLDER stringByAppendingPathComponent:pathToSave];
NSURL *movieUrl = [NSURL fileURLWithPath:pathToSave];

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

exporter.outputFileType=AVFileTypeQuickTimeMovie;
exporter.outputURL=movieUrl;
exporter.shouldOptimizeForNetworkUse=YES;

CMTimeValue val = mixComposition.duration.value;

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

[exporter exportAsynchronouslyWithCompletionHandler:^{
    switch ([exporter status]) {
        case AVAssetExportSessionStatusFailed:{
            NSLog(@"Export failed: %@ %@", [[exporter error] localizedDescription],[[exporter error]debugDescription]);
            NSString * message = @"Movie wasn't created. Try again later.";
            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message waitUntilDone:NO];
            break;}
        case AVAssetExportSessionStatusCancelled:{ NSLog(@"Export canceled");
            NSString * message1 = @"Movie wasn't created. Try again later.";
            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message1 waitUntilDone:NO];
            break;}
        case AVAssetExportSessionStatusCompleted:
        {
            NSString * message = @"Movie was successfully created.";
            CMTime duration = mixComposition.duration;

            [self saveData:duration ofPath:pathToSave];
            [self cleanFiles];
            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message waitUntilDone:NO];
        }
    }}];

}

Answer 1:

问题奠定了在:

videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

他们需要外移动来进行循环体。



文章来源: Composing video and audio using AVMutableComposition