gpt4 book ai didi

ios - AVAssetExportSession 修剪和下载

转载 作者:可可西里 更新时间:2023-11-01 05:43:44 25 4
gpt4 key购买 nike

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

代码:

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

错误:

Export failed: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x635a820 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x1ff4240 "The operation couldn’t be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780)}

你们看到代码中有什么问题了吗? AVExportSession 访问在线视频是否有限制?

最佳答案

我已经设法使用 AVFoundation 修剪远程视频。这是用 Swift 编写的示例代码:

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

关于ios - AVAssetExportSession 修剪和下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31383655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com