Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS video to audio file conversion [duplicate]

I managed it to download a youtube video using NSUrlConnection and save it to the device. Now I want to convert this (I guess .mp4) file to an .mp3 audio file. Does anyone know a solution for this problem? Maybe there's a way to only download the audio from the video? This would save a lot of time.

like image 235
Valle Avatar asked Dec 25 '13 19:12

Valle


People also ask

Can I convert a video file to an audio file on iPhone?

Applications in App Store and Siri Shortcuts can convert a video file to an audio format on iPhone. iPhone users also have the option to use an iTunes feature that extracts MP3 from MP4, MOV or other video files.

How do I separate audio from video IOS?

Detach the audio from a video clipWith your project open, tap a video clip in the timeline to reveal the inspector at the bottom of the screen. Tap the Actions button , then tap Detach. A new blue-colored audio clip appears beneath the video clip.


1 Answers

First of all, you don't want to convert anything, that's slow. Instead you want to extract the audio stream from the mp4 file. You can do this by creating an AVMutableComposition containing only the audio track of the original file and then exporting the composition with an AVAssetExportSession. This is currently m4a centric. If you want to handle both m4a and mp3 output, check the audio track type, make sure to set the right file extension and choose between AVFileTypeMPEGLayer3 or AVFileTypeAppleM4A in the export session.

    NSURL*      dstURL = [NSURL fileURLWithPath:dstPath];
    [[NSFileManager defaultManager] removeItemAtURL:dstURL error:nil];

    AVMutableComposition*   newAudioAsset = [AVMutableComposition composition];

    AVMutableCompositionTrack*  dstCompositionTrack;
    dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];


    AVAsset*    srcAsset = [AVURLAsset URLAssetWithURL:srcURL options:nil];
    AVAssetTrack*   srcTrack = [[srcAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];


    CMTimeRange timeRange = srcTrack.timeRange;

    NSError*    error;

    if(NO == [dstCompositionTrack insertTimeRange:timeRange ofTrack:srcTrack atTime:kCMTimeZero error:&error]) {
        NSLog(@"track insert failed: %@\n", error);
        return;
    }


    AVAssetExportSession*   exportSesh = [[AVAssetExportSession alloc] initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];

    exportSesh.outputFileType = AVFileTypeAppleM4A;
    exportSesh.outputURL = dstURL;

    [exportSesh exportAsynchronouslyWithCompletionHandler:^{
        AVAssetExportSessionStatus  status = exportSesh.status;
        NSLog(@"exportAsynchronouslyWithCompletionHandler: %i\n", status);

        if(AVAssetExportSessionStatusFailed == status) {
            NSLog(@"FAILURE: %@\n", exportSesh.error);
        } else if(AVAssetExportSessionStatusCompleted == status) {
            NSLog(@"SUCCESS!\n");
        }
    }];
like image 174
Rhythmic Fistman Avatar answered Sep 30 '22 14:09

Rhythmic Fistman