如何增加视频文件的持续时间 - 目标c

时间:2012-08-21 10:01:20

标签: iphone objective-c ios audio media

我正在尝试合并音频和视频文件。

我用下面的代码完成了它。

NSString *audioUrl=videoAudioTrackPath;//[[NSBundle mainBundle] pathForResource:@"chalne" ofType:@"mp3"];
    NSString *videoUrl=videoTempPath;//[[NSBundle mainBundle] pathForResource:@"whenever" ofType:@"mp4"];
    audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:audioUrl] options:nil];
    videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:videoUrl] options:nil];

    mixComposition = [AVMutableComposition composition];

    compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType: AVMediaTypeAudio  preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
                                        ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
                                         atTime:kCMTimeZero error:nil];

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

    _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                                                    presetName:AVAssetExportPresetPassthrough];   

       //NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
    NSString *exportPath = videoChangedPath;//[[NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/"] stringByAppendingPathComponent:@"export.mov"];
    NSLog(@"%@",exportPath);

    NSURL    *exportUrl = [NSURL  fileURLWithPath:exportPath];

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

    _assetExport.outputFileType = @"com.apple.quicktime-movie";
    NSLog(@"file type %@",_assetExport.outputFileType);
    _assetExport.outputURL = exportUrl;
    _assetExport.shouldOptimizeForNetworkUse = YES;

    [_assetExport exportAsynchronouslyWithCompletionHandler: ^(void ) {      
        //[self btnListPressed:btnList];
        //[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(btnListPressed:) userInfo:nil repeats:FALSE];
    }];

我的目的 我想改变视频文件的声音。对于同样的摇动,我提取了视频文件的音轨并改变了它的音高和持续时间。然后合并两个文件。通过这种方式,我可以改变视频文件的声音。

现在的问题是,如果我改变音频的持续时间,那么视频就不会与之匹配。

所以我想根据音频文件更改视频持续时间。

无论如何都是这样做的。

由于

1 个答案:

答案 0 :(得分:0)

您应该尝试将insertEmptyTimeRange:方法用于mixComposition。这个新的TimeRange将是音频资产的额外时间。