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

标签 iphone objective-c ios 3gp avassetexportsession

我正在尝试从 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");
        }
    }];
}];

最佳答案

尝试加载 Assets

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

代替

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

关于iphone - 使用 AVAssetExportSession 从 3gp 视频文件导出音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9084179/

相关文章:

ios - 我的应用程序在 WhatsApp 的共享扩展中

ios - 验证登录表单,我正在使用 NSString?

ios - 滑入一个 UIButton 并在达到一定距离时按下另一个 UIButton

iOS AppStore 应用程序预览和 iMovie

ios - SIGABRT 因 SDWebImageCache 而崩溃

ios - 如何在ios中的 View 中显示数组的内容

java - 我可以使用 Java 开发 iPhone 应用程序吗?

iphone - 处理文本字段中的非 ASCII 字符 :shouldChangeCharactersInRange:replacementString:

iphone - TableView coredata 临时对象

ios - 如何使用 NSCoding 在我的应用程序中保存/加载数据?