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

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

问题描述

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



我一直在试验AVFoundation和 AVAssetExportSession 尝试获取较小的文件大小。我试过以下代码:

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

完全没有缩小文件大小。我知道我在做什么是可能的,因为在苹果的照片应用程序中,当您选择在YouTube上共享时,会自动处理视频文件,使其足够小以便上传。我想在我的应用程序中做同样的事情。



我怎样才能做到这一点?

$ c> AVCaptureSession 和 AVAssetWriter 您可以设置压缩设置:

<$ p AVVideoWidthKey:@(video_width),
AVVideoHeightKey:@(video_height),
AVVideoCompressionPropertiesKey:$ b $ {code> NSDictionary *设置= @ {AVVideoCodecKey:AVVideoCodecH264,
b @ {AVVideoAverageBitRateKey:@(desired_bitrate),
AVVideoProfileLevelKey:AVVideoProfileLevelH264Main31,/ *或者任何配置文件&您希望使用的级别* /
AVVideoMaxKeyFrameIntervalKey:@(desired_keyframe_interval)}};

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

编辑:我猜如果你坚持使用 UIImagePicker 来创建电影,你必须使用 AVAssetReader的 copyNextSampleBuffer AVAssetWriter的 appendSampleBuffer 方法来完成转码。


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.

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!");
}];

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?

解决方案

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

Edit: 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天全站免登陆