0

オーディオ ファイルとビデオ ファイルをマージしようとしています。

そして、私は以下のコードでそれを行いました。

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

私の目的 ビデオファイルの音声を変更したいです。同じ揺れのために、ビデオファイルのオーディオトラックを抽出し、そのピッチと継続時間を変更しました。両方のファイルをマージしました。このようにして、ビデオファイルの音声を変更できます。

今の問題は、オーディオの長さを変更すると、ビデオがそれに一致しないことです。

そのため、オーディオファイルに従ってビデオの継続時間を変更したいと考えています。

とにかくそれをすることはありますか。

ありがとう

4

1 に答える 1

0

mixComposition にinsertEmptyTimeRange:メソッドを使用するようにしてください。この新しい TimeRange は、オーディオ アセットの余分な時間になります。

于 2012-12-05T10:30:37.467 に答える