gpt4 book ai didi

ios - 在 iOS 中压缩视频

转载 作者:可可西里 更新时间:2023-11-01 06:17:46 25 4
gpt4 key购买 nike

我看过this questionthis question , 两者都无法提供帮助。

我尝试了以下方法:

- (void)compress:(NSURL *)videoPath completionBlock:(void(^)(id data, BOOL result))block{
self.outputFilePath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [NSURL fileURLWithPath:self.outputFilePath];
[self compressVideoWithURL:self.movieURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {

}];
}

- (void)compressVideoWithURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler {

AVURLAsset *asset = [AVURLAsset assetWithURL:self.movieURL];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.fileLengthLimit = 3000000;
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSData *newOutputData = [NSData dataWithContentsOfURL:outputURL];
NSLog(@"Size of New Video(bytes):%d",[newOutputData length]);
}];
}

我知道 self.movi​​eUrl 不是 nil。但是当我打印与视频关联的 NSData 的大小(以字节为单位)时,它们前后相同,均为 30,000,000 字节。

但根据this question ,上面的代码应该可以工作。

我究竟做错了什么?

最佳答案

在 Swift 3.0 中

从相机胶卷中选择视频

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if info[UIImagePickerControllerMediaType] as? String == (kUTTypeMovie as? String) {
// here your video capture code
let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL!
let data = NSData(contentsOf: videoURL! as URL)!
print("File size before compression: \(Double(data.length / 1048576)) mb")
let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".m4v")
compressVideo(inputURL: videoURL as! URL, outputURL: compressedURL) { (exportSession) in
guard let session = exportSession else {
return
}

switch session.status {
case .unknown:
break
case .waiting:
break
case .exporting:
break
case .completed:
guard let compressedData = NSData(contentsOf: compressedURL) else {
return
}
print("File size after compression: \(Double(compressedData.length / 1048576)) mb")
case .failed:
break
case .cancelled:
break
}
}
}
self.dismiss(animated: true, completion: nil)
}

压缩类型:

AVAssetExportPresetLowQuality
AVAssetExportPresetMediumQuality
AVAssetExportPresetHighestQuality
AVAssetExportPreset640x480
AVAssetExportPreset960x540

压缩视频的方法

 func compressVideo(inputURL: URL, outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) {
let urlAsset = AVURLAsset(url: inputURL, options: nil)
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetLowQuality) else {
handler(nil)

return
}

exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileTypeQuickTimeMovie
exportSession.shouldOptimizeForNetworkUse = true
exportSession.exportAsynchronously { () -> Void in
handler(exportSession)
}
}

输出:

File size before compression: 25.0 mb
File size after compression: 7.0 mb

关于ios - 在 iOS 中压缩视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096928/

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