从AVSession压缩ios视频​​

时间:2015-11-17 23:26:29

标签: ios video parse-platform avfoundation

我正在使用LLSimpleCamera在我的应用中捕获视频:

https://github.com/omergul123/LLSimpleCamera

效果很好!我喜欢它,但是,我也使用Parse作为我的后端,最大文件大小为10MB。这不应该是一个问题,因为我作为AVSession捕获的视频都不到10秒,所以它不应该那么大。

有没有办法压缩视频,或者LLSimple相机中有什么东西我没有看到创建这个视频太大。

例如,我的4秒视频导致PFFile大于10 MB,如10.6 MB!

下面是使用LLSimpleCamera库上传代码的代码:

- (void)uploadMessage {
NSData *fileData;
NSString *fileName;
NSString *fileType;

fileData = [NSData dataWithContentsOfURL:self.videoUrl];
fileName = @"video.mov";
fileType = @"video";

NSLog(@"filesize = %@",[NSByteCountFormatter stringFromByteCount:fileData.length countStyle:NSByteCountFormatterCountStyleFile]);

float fileSize = (float)fileData.length;
NSLog(@"filesize = %f", fileSize);



if(fileSize <= 10485760) {

    PFFile *file = [PFFile fileWithName:fileName data:fileData];
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                message:@"Please try sending your message again."
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
        else {
            PFObject *message = [PFObject objectWithClassName:@"Scenes"];
            [message setObject:file forKey:@"file"];
            [message setObject:fileType forKey:@"fileType"];
            [message setObject:[[PFUser currentUser] objectId] forKey:@"userId"];
            [message setObject:[[PFUser currentUser] username] forKey:@"userName"];
            [message setObject:[[PFUser currentUser] objectForKey:@"loggedInVenueId"] forKey:@"venueId"];
            [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                        message:@"Please try sending your message again."
                                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alertView show];
                }
                else {
                    // Everything was successful!

                }
            }];
        }
    }];

}else
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Video too big!"
                                                        message:@"Please try sending your message again."
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}




[self.navigationController popViewControllerAnimated:YES];

}

1 个答案:

答案 0 :(得分:3)

之前我已经编写了自己的相机库,我的应用程序在Parse上传了很长的视频文件,所有这些文件都低于10 MB。确保视频文件不超过10 MB限制的技巧是控制影响视频文件大小的两个主要因素,即录制质量分辨率

通过为AVCaptureSession设置录制输入会话属性documented by Apple,可以很容易地控制这两个主要因素:

NSString *const AVCaptureSessionPresetPhoto;
NSString *const AVCaptureSessionPresetHigh;
NSString *const AVCaptureSessionPresetMedium;
NSString *const AVCaptureSessionPresetLow;
NSString *const AVCaptureSessionPreset320x240;
NSString *const AVCaptureSessionPreset352x288;
NSString *const AVCaptureSessionPreset640x480;
NSString *const AVCaptureSessionPreset960x540;
NSString *const AVCaptureSessionPreset1280x720;
NSString *const AVCaptureSessionPresetiFrame960x540;
NSString *const AVCaptureSessionPresetiFrame1280x720;

我认为你的4秒超过10MB的视频是全高清视频,其会话预设设置为AVCaptureSessionPresetHigh。初始化LLSimpleCamera库时,最好选择较低的库,例如:

[[LLSimpleCamera alloc] initWithQuality:AVCaptureSessionPresetMedium
                                         position:CameraPositionBack
                                     videoEnabled:YES];