Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to extract audio from Video using iOS sdk

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.31k
    Comment on it

    This is how we can extract the audio from Video :

    First import the video from libaray using imagePickerController delegates methods. 

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      // 1 - Get media type
      NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    
      // 2 - Dismiss image picker
      [self dismissViewControllerAnimated:YES completion:nil];
    
      // 3 - Handle video selection
      if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        self.videoAsset = [AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Asset Loaded" message:@"Video Asset Loaded" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
      }
    }

    then we can extract the audio track from Video file using following method:

    -(void)extractAudiofromVideo
    {
       
        AVMutableComposition*   newAudioAsset = [AVMutableComposition composition];
        
        AVMutableCompositionTrack*  dstCompositionTrack;
        dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        
      [dstCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration)
                            ofTrack:[[self.videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
        
        AVAssetExportSession*   exportSesh = [[AVAssetExportSession alloc] initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];
        
        exportSesh.outputFileType = AVFileTypeCoreAudioFormat;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *audioPath = [documentsDirectory stringByAppendingPathComponent:@"soundOneNew.caf"];
        exportSesh.outputURL=[NSURL fileURLWithPath:audioPath];
        
        
        [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");
                
                
                NSError *error;
                
            }
        }];
    }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: