如何从视频中制作音频文件?在iOS上

时间:2012-06-14 08:38:10

标签: ios audio avfoundation

我正在寻找制作音频文件的方式,如wav,alf等等。来自视频。

简单地说,我想只从视频中获取音频。

我该怎么做?

我尝试了没有视频的AVAssetExportSession,但只是音频,

但这不起作用。

PLS帮助。

这是我的代码

- (void)exportAudioComposition
{
    self.filteredSoundURL = [self saveAudioFileRoot];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:self.assetComposition presetName:AVAssetExportPresetPassthrough];
    exportSession.outputURL = self.filteredSoundURL;
    exportSession.outputFileType = AVFileTypeWAVE;
    exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, self.assetComposition.duration);

    [exportSession exportAsynchronouslyWithCompletionHandler:^
    {
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    }];
    [self doAudioProcessWithFilter:SOUN_TURTLE andAssetURL:self.filteredSoundURL];
}

- (void)setupCompositionWithAudio:(NSURL *)audioURL andVideo:(NSURL *)videoURL
{
    if (self.assetComposition != nil)
    {
        self.assetComposition = nil;
    }

    self.assetComposition = [AVMutableComposition composition];

    if (videoURL != nil) 
    {
        AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
        AVMutableCompositionTrack *compVideoTrack = [self.assetComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

        AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        CMTimeRange videoTimeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
        [compVideoTrack insertTimeRange:videoTimeRange ofTrack:videoTrack atTime:kCMTimeZero error:nil];
    }

    AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:audioURL options:nil];
    AVMutableCompositionTrack *compAudioTrack = [self.assetComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *audioTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
    [compAudioTrack insertTimeRange:audioTimeRange ofTrack:audioTrack atTime:kCMTimeZero error:nil];
}

0 个答案:

没有答案