3

我正在寻找从视频中制作 wav、alf 等音频文件的方法。

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

我怎样才能做到?

我试图 AVAssetExportSession 没有视频,但只有音频,

但这是行不通的。

请帮忙。

这是我的代码

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

0 回答 0