AVAssetExportSession outputfile [英] AVAssetExportSession outputfile

查看:2866
本文介绍了AVAssetExportSession outputfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AVAssetExportSession输出文件应该如何显示?我试图压缩来自ALAsset项目的视频,它不工作。我猜输出文件与它有关系。

How should the AVAssetExportSession output file look like? I'm trying to compress a video from an ALAsset item and it doesn't work. I'm guessing the output file has something to do with it.

这里是我使用的代码:

NSString *destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie"];
[self convertVideoToLowQualityWithInputURL:asset.defaultRepresentation.url outputURL:[NSURL URLWithString:destinationPath]];

- (void)convertVideoToLowQualityWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL {

    if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithContentsOfURL:outputURL encoding: 0 error:Nil]]) [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];

    AVURLAsset *assetAV = [AVURLAsset URLAssetWithURL:inputURL options:nil];

    NSLog(@"url string from asset: %@", assetAV.URL);
    NSLog(@"output url: %@", [outputURL absoluteString]);

    [[NSFileManager defaultManager] createFileAtPath:[outputURL path] contents:nil attributes:nil];

    NSLog(@"duration: %lld", assetAV.duration.value); //it logs a valid value, so it's all good so far

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:assetAV presetName:AVAssetExportPresetLowQuality];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"success");
        } else {
            NSLog(@"error: %@", [exportSession error]);
            //error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x2023b720 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x2023bb70 "The operation couldn’t be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780)}
        }
    }];
}

有人可以帮助我吗?

UPDATE:

找到解决方案。因为我认为问题是输出文件,所以这里是生成一个有效的代码:

Found the solution. As I thought the problem was the output file so here is the code for generating a valid one:

            NSUInteger count = 0;
            NSString *filePath = nil;
            do {
                NSString *extension = ( NSString *)UTTypeCopyPreferredTagWithClass(( CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension);
                NSString *fileNameNoExtension = [[asset.defaultRepresentation.url URLByDeletingPathExtension] lastPathComponent];
                NSString *fileName = [NSString stringWithFormat:@"%@-%@-%u",fileNameNoExtension , AVAssetExportPresetLowQuality, count];
                filePath = NSTemporaryDirectory();
                filePath = [filePath stringByAppendingPathComponent:fileName];
                filePath = [filePath stringByAppendingPathExtension:extension];
                count++;

            } while ([[NSFileManager defaultManager] fileExistsAtPath:filePath]);

            NSURL *outputURL = [NSURL fileURLWithPath:filePath];


推荐答案

您的问题是[NSURL URLWithString:destinationPath]那。你需要像在解决方案中一样使用[NSURL fileURLWithPath:xxx]。发布这个只是为了澄清这是唯一的问题。

Your issue is [NSURL URLWithString:destinationPath] and only that. You need to use [NSURL fileURLWithPath:xxx] like you did in your solution. Posting this just to clarify that that is the only problem.

这篇关于AVAssetExportSession outputfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆