0

我正在尝试从 3gpp 视频文件中导出音频,但它无法正常工作......有谁知道我可能做错了什么?这是我正在使用的代码:

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");
        }
    }];
}];
4

2 回答 2

2

尝试加载资产

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

代替

[AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];
于 2012-01-31T21:49:13.683 回答
0

由于您的任务将直接操作音频样本缓冲区,您应该使用 AVFoundation 为您提供的第二个变体:配对的 AVAssetReader 和 AVAssetWriter 设置。您会从 Apple 开发人员源代码中找到正确的示例代码,如 AVReaderWriterOSX 中的代码。除了您有不同的 I/O 格式设置可用之外,这也应该适用于 iOS。应提供将音频解压缩为 PCM 并写回未压缩的 .wav 或音频文件的可用性

于 2013-07-30T18:47:25.583 回答