从MOV文件iOS获取音频(mp3)

时间:2014-03-11 02:32:14

标签: ios objective-c cocoa-touch

如何从用户相机胶卷中获取的.MOV文件中提取音频(最好是.mp3)?

我在这里提取MOV文件:

- (void) imagePickerController: (UIImagePickerController *) picker
 didFinishPickingMediaWithInfo: (NSDictionary *) info {

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:NO completion:nil];

    if (CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0)
        == kCFCompareEqualTo) {

        NSString *fileName = [[info objectForKey:
                                UIImagePickerControllerMediaURL] path];
        NSData *data = [[NSData alloc] initWithContentsOfFile:moviePath];

        [self dismissViewControllerAnimated:YES completion:nil];


    }
}

如果需要提供更多代码,请告诉我,但我认为这是足够的信息量。

我尝试了一些事情,但绝对无处可去。任何帮助表示赞赏。谢谢

1 个答案:

答案 0 :(得分:0)

我使用以下代码提取m4a文件并编写文件应用程序文档目录

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

AVMutableComposition *newAudioAsset = [AVMutableComposition composition];
AVMutableCompositionTrack *dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
// 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) {
    videoAsset = [AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]];
    //  NSLog(@"track = %@",[videoAsset tracksWithMediaType:AVMediaTypeAudio]);

    NSArray *trackArray = [videoAsset tracksWithMediaType:AVMediaTypeAudio];
    if(!trackArray.count){
        NSLog(@"Track returns empty array for mediatype AVMediaTypeAudio");
        return;
    }

    AVAssetTrack *srcAssetTrack = [trackArray  objectAtIndex:0];

    //Extract time range
    CMTimeRange timeRange = srcAssetTrack.timeRange;
    NSError *err = nil;
    if(NO == [dstCompositionTrack insertTimeRange:timeRange ofTrack:srcAssetTrack atTime:kCMTimeZero error:&err]){
        NSLog(@"Failed to insert audio from the video to mutable avcomposition track");
        return;
    }
    //Export the avcompostion track to destination path
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
    NSString *dstPath = [documentsDirectory stringByAppendingString:@"/sample_audio.m4a"];
    NSURL *dstURL = [NSURL fileURLWithPath:dstPath];


    //Remove if any file already exists
    [[NSFileManager defaultManager] removeItemAtURL:dstURL error:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];
    NSLog(@"support file types= %@", [exportSession supportedFileTypes]);
    exportSession.outputFileType = AVFileTypeAppleM4A;
    exportSession.outputURL = dstURL;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        AVAssetExportSessionStatus status = exportSession.status;

        if(AVAssetExportSessionStatusCompleted != status){
            NSLog(@"Export status not yet completed. Error: %@", exportSession.error.description);
        }
    }];


    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Asset Loaded" message:@"Video Asset Loaded"
    //                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    //        [alert show];
}

}