简体   繁体   English

使用AVAssetExportSession从3gp视频文件导出音频

[英]Exporting audio from 3gp video files with AVAssetExportSession

I am trying to export the audio from a 3gpp video file and it is not working... Does anyone know what I may be doing wrong? 我试图从3gpp视频文件中导出音频而它无法工作......有谁知道我可能做错了什么? Here is the code I am using: 这是我正在使用的代码:

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

Try to load the asset with 尝试加载资产

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

instead of 代替

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

As your task will be manipulating the audio sample buffers directly you should use the second variant that AVFoundation will give you: paired AVAssetReader and AVAssetWriter setup. 由于您的任务将直接操作音频样本缓冲区,您应该使用AVFoundation将为您提供的第二个变体:配对的AVAssetReader和AVAssetWriter设置。 You'll find proper sample code as in AVReaderWriterOSX from Apple developer source. 您可以在Apple开发人员源代码的AVReaderWriterOSX中找到正确的示例代码。 This should also work with iOS besides you have different I/O format settings available. 除了您有不同的I / O格式设置外,这也适用于iOS。 The availability to decompress audio as PCM and write back to uncompressed .wav or Audio file should be given 应该给出将音频解压缩为PCM并写回未压缩的.wav或音频文件的可用性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM