gpt4 book ai didi

ios - 在iOS中上传之前如何压缩视频数据

转载 作者:行者123 更新时间:2023-12-01 17:12:45 25 4
gpt4 key购买 nike

我在压缩视频数据中有问题(视频数据是从ALASSET获取的)。我想先压缩视频数据,然后再上传到服务器。我发现下面的函数转换为低质量,但输出是NSURL而不是NSDATA。上传前如何压缩视频的NSData。

这是我的上传功能:

ALAsset *alasset = [allVideos objectAtIndex:i];
ALAssetRepresentation *rep = [alasset defaultRepresentation];
NSString * videoName = [rep filename];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

这是转换功能:
    - (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler
{
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
handler(exportSession);
[exportSession release];
}];
}

最佳答案

下面的代码对我来说是完美的:

asset = [[AssetItem alloc] initWithURL:movieURL];
NSLog(@"Start");
asset.exportSession.shouldOptimizeForNetworkUse = TRUE;
asset.preset = AVAssetExportPresetMediumQuality;
asset.exportSession.outputFileType = @"public.mpeg-4";
[asset exportAssetWithCompletionHandler:^(NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if (error != nil)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription]
message:[error localizedFailureReason] delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alertView show];
}
});
}];

关于ios - 在iOS中上传之前如何压缩视频数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138517/

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