首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVMutableComposition删除视频音轨

AVMutableComposition删除视频音轨
EN

Stack Overflow用户
提问于 2013-03-11 20:40:51
回答 1查看 1.9K关注 0票数 1

我一直在尝试使用AVMutableComposition混合音频(录制的)和视频(.mp4),这意味着我需要两个文件在混合后并行播放,这是我使用的代码: temporaryRecFile是录制的文件url路径audiosURL是视频文件的url路径

代码语言:javascript
复制
NSURL *audiosURL =[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"dam3.mp4"] ofType:nil]];

        NSLog(@"SOMEDATA    IS THERE ");
        AVURLAsset* audioAsset  = [[AVURLAsset alloc]initWithURL:temporaryRecFile options:nil];
        AVURLAsset* audio2Asset = [[AVURLAsset alloc]initWithURL:audiosURL options:nil];

        AVMutableComposition* mixComposition = [AVMutableComposition composition];

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

        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];


        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 = AVFileTypeQuickTimeMovie;
    //@"com.apple.quicktime-movie";
        NSLog(@"file type %@",_assetExport.outputFileType);
        _assetExport.outputURL = exportUrl;
        _assetExport.shouldOptimizeForNetworkUse = YES;



        [_assetExport exportAsynchronouslyWithCompletionHandler:
         ^(void )
         {
             switch (_assetExport.status)
             {
                 case AVAssetExportSessionStatusCompleted:
                     //   export complete

                     NSLog(@"Export Complete");
                    // From Here I want play movie using MPMoviePlayerController.<<<---------
                     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];
                     [audio2Asset release];
                     [_assetExport release];
                     [self performSelectorOnMainThread:@selector(playVideo:) withObject:documentDirectoryURL waitUntilDone:NO];

                     break;
                 case AVAssetExportSessionStatusFailed:
                     NSLog(@"Export Failed");
                     NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);

                     //                export error (see exportSession.error)
                     break;
                 case AVAssetExportSessionStatusCancelled:
                     NSLog(@"Export Failed");
                     NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);

                     //                export cancelled  
                     break;

             }
         }
         ];

结果是导出的视频播放录制的音频成功,但是视频的音轨被去掉了,所以导出的.mov播放的只是视频的图片,没有声音,为什么会这样呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-29 00:40:01

您必须将视频中的音频添加为单独的音轨。因此,您将有一个视频轨道和两个音频轨道。在您的代码中,您没有来自所添加视频的音频。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15338824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档