1
  • Hello i am working on extract only audio from video file.
  • That is working in < iOS 8.0 version, but it is not working in >= iOS 8.0 version.
  • how to extract audio in >= iOS 8.0 version ?
glennsl
  • 28,186
  • 12
  • 57
  • 75

1 Answers1

6

Use AVAssetExportSession to convert video file to audio. You may use this method.

- (void)convertVideoToAudioWithInputURL:(NSURL*)inputURL
                                   outputURL:(NSURL*)outputURL
                                     handler:(void (^)(AVAssetExportSession*))handler
{
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    self.exportSession = [[AVAssetExportSession alloc] initWithAsset:asset
                                                          presetName: AVAssetExportPresetPassthrough];
    self.exportSession.outputURL = outputURL;
    self.exportSession.outputFileType = AVFileTypeAppleM4A; //For audio file
    self.exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, [asset duration]);

        [self.exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
            handler(self.exportSession);
        }];
}

Handle the file at outputUrl for further use. :)

jarora
  • 5,384
  • 2
  • 34
  • 46
  • if my `inputURL` was linked to a Video and i wanted to extract the mp3 of the video and download it to the phone, how this will be possible ? will this method consume data as if it was a video download ? please i need help with thiss issue THANKS – user3783005 Nov 14 '15 at 03:49
  • @user3783005 for the method to be able to extract audio from any chunk of video data, it needs to have that chuck of video at hand. The processing of that video chunk can not be done at the videos source. So yes, `exportAsynchronouslyWithCompletionHandler` downloads / fetches a chunk of the video at a time, processes that , and appends to the output file. Straight answer, yes - you will use up data as if you were downloading the video. – pnizzle Oct 30 '18 at 01:24