2012-01-31 130 views
0

我想从3gpp视频文件导出音频,它不工作......有谁知道我可能做错了什么?这里是我使用的代码:用AVAssetExportSession从3gp视频文件导出音频

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"newFile.m4a"]; 
NSString *tempFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"oldFile.3gp"]; 

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil]; 
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^ { 

    //HERE IS THE PROBLEM. THE ARRAY OF TRACKS IS EMPTY FOR SOME REASON. 
    AVAssetTrack* audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

    AVMutableComposition* audioComposition = [AVMutableComposition composition]; 
    AVMutableCompositionTrack* audioCompositionTrack = [audioComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [audioCompositionTrack insertTimeRange:[audioTrack timeRange] ofTrack:audioTrack atTime:CMTimeMake(0, 1) error:nil]; 


    AVAssetExportSession *exprortSession = [AVAssetExportSession exportSessionWithAsset:audioComposition presetName:AVAssetExportPresetAppleM4A]; 
    NSURL *toFileURL = [NSURL URLWithString:filePath]; 
    exprortSession.outputURL = toFileURL; 
    exprortSession.outputFileType = @"com.apple.m4a-audio"; 

    NSLog(@"exportAsynchronouslyWithCompletionHandler will start"); 

    [exprortSession exportAsynchronouslyWithCompletionHandler: ^(void) { 

     if (exprortSession.status == AVAssetExportSessionStatusCompleted) { 
      NSLog(@"Export success"); 
     } 
     else { 
      NSLog(@"Export failed"); 
     } 
    }]; 
}]; 

回答

1

尝试加载资产与

[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempFilePath] options:nil]; 

,而不是

[AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil]; 
+0

是的。我不敢相信我看过那个。也在输出文件中。谢谢。 – Zigglzworth 2012-02-01 13:29:52

0

当你的任务是直接操纵的音频采样缓冲区,你应该使用AVFoundation会给你的第二个变体:配对的AVAssetReader和AVAssetWriter设置。您可以在Apple开发人员资源的AVReaderWriterOSX中找到适当的示例代码。除了您可以使用不同的I/O格式设置外,这也适用于iOS。可以解压音频为PCM并写回未压缩的.wav或音频文件。