首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAssetExportSession Trim &下载

AVAssetExportSession Trim &下载
EN

Stack Overflow用户
提问于 2015-07-13 12:37:34
回答 3查看 1.4K关注 0票数 5

我正在尝试使用AVExportSession来修剪和下载在线视频。

代码:

代码语言:javascript
复制
FileMove *fileMove = (FileMove*)data;
  NSString *url = @"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4";
    NSURL *videoURL = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"VideoURL: %@",videoURL);


    AVAsset *anAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];    
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:anAsset presetName:AVAssetExportPresetLowQuality];
        NSURL *outputURL = [NSURL fileURLWithPath:fileMove.dst];
        NSLog(@"outputURL: %@",outputURL);
        exportSession.outputURL = outputURL;
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        CMTime start = CMTimeMakeWithSeconds(1.0, 600);
        CMTime duration = CMTimeMakeWithSeconds(3.0, 600);
        CMTimeRange range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range;

        if ([[NSFileManager defaultManager] fileExistsAtPath:fileMove.dst])
            [[NSFileManager defaultManager] removeItemAtPath:fileMove.dst error:nil];

        [exportSession exportAsynchronouslyWithCompletionHandler:^{

            switch ([exportSession status]) {
                case AVAssetExportSessionStatusFailed:
                    NSLog(@"Export failed: %@", [[exportSession error]description ]);
                    break;
                case AVAssetExportSessionStatusCancelled:
                    NSLog(@"Export canceled");
                    break;
                default:
                    break;
            }
        }];
    }

错误:

导出失败:错误Domain=AVFoundationErrorDomain代码=-11800“操作无法完成”UserInfo=0x635a820 {NSLocalizedDescription=The操作无法完成,NSUnderlyingError=0x1ff4240“操作无法完成。(OSStatus错误-12780.)",NSLocalizedFailureReason=An未知错误(-12780)}

你们看到密码里有什么问题吗?AVExportSession访问在线视频有什么限制吗?

EN

回答 3

Stack Overflow用户

发布于 2016-04-25 06:50:17

我已经成功地使用AVFoundation修剪了远程视频。下面是用Swift编写的示例代码:

代码语言:javascript
复制
let range: CMTimeRange
let sourceURL: NSURL
let targetFileURL: NSURL
let requiredKeys = [ "exportable", "tracks" ]
let asset = AVAsset(URL: sourceURL)
asset.loadValuesAsynchronouslyForKeys(requiredKeys) {

    // Error handling code here
    precondition(asset.statusOfValueForKey("exportable", error: nil) == .Loaded)
    precondition(asset.statusOfValueForKey("tracks", error: nil) == .Loaded)
    precondition(asset.exportable)


    let composition = AVMutableComposition()
    do {
        try composition.insertTimeRange(range, ofAsset: asset, atTime: kCMTimeZero)
    } catch {
        // Error handling code here
        return
    }

    let finalComposition = composition.copy() as! AVComposition
    guard let export = AVAssetExportSession(asset: finalComposition, presetName: AVAssetExportPresetPassthrough) else {
        // Error handling code here
        return
    }

    export.outputURL = targetFileURL
    export.outputFileType = AVFileTypeMPEG4
    export.exportAsynchronouslyWithCompletionHandler {
        switch export.status {
        case .Completed:
            // Alright!
            break

        case .Cancelled, .Failed:
            // Error handling code here
            break

        default:
            fatalError("Shouldn't be called")
        }
    }
}
票数 4
EN

Stack Overflow用户

发布于 2015-10-17 20:58:13

用于创建AVAsset的URL需要是您可以访问的本地文件。在创建AVAsset like so之前,您需要将其下载到设备上。

票数 1
EN

Stack Overflow用户

发布于 2015-10-17 10:11:31

我认为我们无法输出在线视频参见:Unable to export AVPlayerItem

所以我想,你可以先在本地下载视频,然后修剪,保存和使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31383655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档