如何减小使用 UIImagePickerController 创建的视频的文件大小? [英] How can I reduce the file size of a video created with UIImagePickerController?

查看:34
本文介绍了如何减小使用 UIImagePickerController 创建的视频的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户使用 UIImagePickerController 录制视频,然后将其上传到 YouTube.问题是 UIImagePickerController 创建的视频文件很大,即使视频只有 5 秒长.例如,一个 5 秒长的视频是 16-20 兆字节.我想将视频保持在 540 或 720 质量,但我想减小文件大小.

I have an app that allows a user to record a video with UIImagePickerController and then upload it to YouTube. The problem is that the video file that UIImagePickerController creates is HUGE, even when the video is only 5 seconds long. For example, a 5 second long video is 16-20 megabytes. I want to keep the video in 540 or 720 quality, but I want to reduce the file size.

我一直在尝试使用 AVFoundation 和 AVAssetExportSession 来尝试获得更小的文件大小.我试过以下代码:

I've been experimenting with AVFoundation and AVAssetExportSession to try to get a smaller file size. I've tried the following code:

AVAsset *video = [AVAsset assetWithURL:videoURL];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetPassthrough];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:@"vid1.mp4"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    NSLog(@"done processing video!");
}];

但是这根本没有减小文件大小.我知道我在做什么是可能的,因为在 Apple 的照片应用程序中,当您选择 在 YouTube 上分享" 时,将自动处理视频文件,使其小到可以上传.我想在我的应用程序中做同样的事情.

But this hasn't reduced the file size at all. I know what I'm doing is possible because in Apple's Photos app, when you select "share on YouTube", will automatically process the video file so its small enough to upload. I want to do the same thing in my app.

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

使用 AVCaptureSessionAVAssetWriter 你可以设置压缩设置如下:

With AVCaptureSession and AVAssetWriter you can set the compression settings as such:

NSDictionary *settings = @{AVVideoCodecKey:AVVideoCodecH264,
                           AVVideoWidthKey:@(video_width),
                           AVVideoHeightKey:@(video_height),
                           AVVideoCompressionPropertiesKey:
                               @{AVVideoAverageBitRateKey:@(desired_bitrate),
                                 AVVideoProfileLevelKey:AVVideoProfileLevelH264Main31, /* Or whatever profile & level you wish to use */
                                 AVVideoMaxKeyFrameIntervalKey:@(desired_keyframe_interval)}};

AVAssetWriterInput* writer_input = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];

我想如果你一开始就坚持使用 UIImagePicker 来创建电影,你将不得不使用 AVAssetReader's copyNextSampleBufferAVAssetWriter's appendSampleBuffer 方法来进行转码.

I guess if you insist on using the UIImagePicker to create the movie in the first place, you'll have to use AVAssetReader's copyNextSampleBuffer and AVAssetWriter's appendSampleBuffer methods to do the transcode.

这篇关于如何减小使用 UIImagePickerController 创建的视频的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆