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

我一直在try 使用AVFoundation和AVAssetExportSession,以获得更小的文件大小.我try 了以下代码:

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

但是this hasn't reduced the file size at all.我知道我在做什么是可能的,因为在苹果的Photos应用程序中,当你 Select "share on YouTube"时,会自动处理视频文件,使其足够小,可以上传.我想在我的应用程序中做同样的事情.

我怎样才能做到这一点?

推荐答案

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

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方法来进行转码.

Objective-c相关问答推荐

错误无法使 UICollectionElementKindCell 类型的视图出列

通过指针算术访问数组值与 C 中的下标

如何检测 iPhone 5c 的 colored颜色 ?

将未知数量的行添加到静态单元UITableView

将 HEX NSString 转换为 NSData

获取 Objective-C 类或实例的所有方法

如果用户点击屏幕键盘,我如何关闭键盘?

有没有办法清除 UIImage 类使用的缓存?

如何使用 Cocoa 创建临时文件?

测试应用程序是否确实从 UILocalNotification 变为活动状态

如何在目标 C 中创建单例类

烦人的[Environment: Sandbox]提示

使用对象引用的 NSArray,我是显式释放数组中的所有对象还是仅释放数组本身?

如何在 iPhone SDK 中使用自定义字体?

无法加载 xcode 项目,因为它已从另一个项目或工作区打开

获取包含在 NSString 中的文件的扩展名

Modal segue,导航栏消失

NSCharacterSet:如何将_添加到 alphanumericCharacterSet 文本限制?

将数据传回前一个视图控制器

Block 隐式保留'self';明确提及touch以表明这是预期行为