从 AVSession 压缩 ios 视频

Compress ios video from AVSession

我正在使用 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];

}

我以前写过自己的相机库,我的应用程序在 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;

我认为您超过 10MB 的 4 秒视频是在会话预设设置为 AVCaptureSessionPresetHigh 时录制的全高清视频。初始化LLSimpleCamera库的时候最好选择低一点的,例如:

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