压缩iOS AVSession视频

3
我正在使用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个回答

3
我以前写过自己的摄像头库,我的应用程序将长视频文件上传到Parse,所有视频文件都在10 MB以下。确保您的视频文件不超过10 MB的诀窍是控制影响视频文件大小的两个主要因素,即录制质量分辨率
通过设置录制输入会话属性(由苹果文档记录)AVCaptureSession,很容易控制这两个主要因素。
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];

这是一个很棒的答案!非常感谢。我会尝试这个。 - ian
它运行正常!我选择了initWithQuality:AVCaptureSessionPreset1280x720并将其限制为8秒,文件大小不到10MB。 - ian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接