簡體   English   中英

AVAssetExportSession輸出文件

[英]AVAssetExportSession outputfile

AVAssetExportSession輸出文件應該如何? 我正在嘗試從ALAsset項目壓縮視頻,但它不起作用。 我猜輸出文件與它有關。

這是我正在使用的代碼:

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

有人可以幫幫我嗎?

更新:

找到了解決方案。 我認為問題是輸出文件,所以這里是生成有效文件的代碼:

            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]。 發布此信息只是為了澄清這是唯一的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM