【问题标题】:why no sound when I merge Audio and video using AVMutableCompositionTrack? [closed]为什么我使用 AVMutableCompositionTrack 合并音频和视频时没有声音? [关闭]
【发布时间】:2018-04-19 07:01:58
【问题描述】:

为什么我使用 AVMutableCompositionTrack 合并音频和视频时没有声音?

    NSArray * breakArr = [[[[_dic objectForKey:@"url"] lastObject] objectForKey:@"url"]componentsSeparatedByString:@"/"];
    NSString *pathAudio = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[breakArr lastObject]];

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathAudio] options:nil];

    NSString *pathVideo = self.pathToMovie;
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathVideo] options:nil];

    AVMutableComposition* mixComposition = [AVMutableComposition composition];

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

    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";
    _assetExport.outputFileType = AVFileTypeMPEG4;
    _assetExport.outputURL = exportUrl;

    _assetExport.shouldOptimizeForNetworkUse = YES;

    [_assetExport exportAsynchronouslyWithCompletionHandler:
         ^(void ) {
             // your completion code here
             [[ModelessAlertView instance]closeAlert];
             UISaveVideoAtPathToSavedPhotosAlbum(exportPath, self, @selector(videoWithPatch:didFinishSavingWithError:contextInfo:), nil);
         }

     ];

这里是代码。 当我查看视频时。我只听到音轨的声音。而且我听不到视频声音。如何设置音量?

有人知道吗?非常感谢!

【问题讨论】:

  • 因为您从未将视频音轨添加到您的作品中。

标签: ios objective-c audio video composition


【解决方案1】:

您的代码有效。确保路径上是否存在您的音频。我尝试使用您的代码从捆绑包中加载声音和视频并合并。它按我的预期工作。我从包中加载的内容如下所示。

NSString *pathAudio = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathAudio] options:nil];

NSString *pathVideo = [[NSBundle mainBundle] pathForResource:@"Clip1" ofType:@"mp4"];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathVideo] options:nil];

更新

为了同时合并视频声音和另一个声音,将另一个 AVMutableCompositionTrack 添加到您的 AVMutableComposition。

// add another track for video sound
AVMutableCompositionTrack *videoSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                     preferredTrackID:kCMPersistentTrackID_Invalid];

//insert video sound to the track
[videoSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                         ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                          atTime:kCMTimeZero error:nil];

【讨论】:

  • 对不起,我犯了错误。我只听到音轨的声音。而且我听不到视频声音。
  • 让我检查一下
  • 我更新了我的答案。让我知道它是否有帮助
  • 它有效..谢谢!我爱你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多