【问题标题】:UIImagePickerController: Strange result of video compressionUIImagePickerController:视频压缩的奇怪结果
【发布时间】:2012-06-09 23:02:16
【问题描述】:

在我的应用中,用户可以录制视频或从他的库中选择一个。当然,我使用UIImagePickerController 并将其sourceType 设置为UIImagePickerControllerSourceTypeCameraUIImagePickerControllerSourceTypePhotoLibrary。 在这两种情况下,videoQuality 都设置为 UIImagePickerControllerQualityTypeMedium

现在,我执行了以下操作:我将 iPhone 放在背面拍摄了一个 15 秒的视频,所以它是漆黑的。当我从库中选择这个视频时,它大约有 0.6 MB 大。当我在我的应用程序中拍摄相同的视频(15 秒,漆黑)时,我得到一个超过 4 MB 的文件。

有人可以证实吗?我简直不敢相信我是第一个注意到的人,但话又说回来,我没有太多空间可以在这里搞砸(尽管如此,我可能还是这样做了)。或者有人对此有解释/解决方案吗?

(我在 iOS 5.1 使用 iPhone 4)

【问题讨论】:

标签: iphone ios video uiimagepickercontroller


【解决方案1】:

你明白了吗?

现在我遇到了同样的问题,PhotoLibrary 中有一个视频(再过 2 分钟);当我使用 UIImagePickerController 得到它时,它只有大约 30 Mb;但是我通过asset.defaultRepresentation(使用这种方式(Getting video from ALAsset)),它达到了大约300Mb;也许 UIImagePickerController 以某种方式压缩了数据;我需要弄清楚,但没有任何进展......

=================

编辑:UIVideoEditorController 可以将视频压缩到小尺寸;你可以像 UIImagePickerController 一样设置 videoQuality。

可能是这样的:当你使用 UIImagePickerController 选择一个视频,并设置allowEditing=YES,它会呈现 UIVideoEditorController 压缩视频,然后你得到压缩后的小尺寸视频。

【讨论】:

【解决方案2】:

我现在想通了。解决方案不是减小尺寸,而是降低比特率。这就是我认为当您从库中选择视频时 Apple 正在做的事情。

在这里查看我的答案:https://stackoverflow.com/a/16035330/884119

【讨论】:

    【解决方案3】:

    压缩视频的最佳方式。

    这里是代码。

           -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    
        NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    
       NSData *data = [NSData dataWithContentsOfURL:videoURL];
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
                 NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
                 NSString *caldate = [NSString stringWithFormat:@"%@.mov",[now description]];
                caldate = [caldate stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
                 NSString  *documentsDirectory = [paths objectAtIndex:0];
                 NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory,caldate];
    
                NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
                NSURL *selectedVideoUrl;
    
                if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
                         == kCFCompareEqualTo) {
    
                                   tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
                                   selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
                         }
    
                       NSLog(@"old move %@",path);
    
    
            NSURL *url = [NSURL fileURLWithPath:tempFilePath];
            [data writeToFile:path atomically:YES];
            //Delete the original asset
            NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
            NSString  *documentsDirectory1 = [paths1 objectAtIndex:0];
            NSString *path1 = [NSString stringWithFormat:@"%@/%@", documentsDirectory1,caldate];
            NSURL *url1 = [NSURL fileURLWithPath:path1];
    
            NSLog(@"new move %@",path);
    
            [self convertVideoToLowQuailtyWithInputURL:url outputURL:url1 handler:Nil];
            [picker  dismissModalViewControllerAnimated: YES];
    

    }

        -(void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
                                   outputURL:(NSURL*)outputURL
                                     handler:(void (^)(AVAssetExportSession*))handler{
    
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
    exportSession.outputURL = outputURL;
    if ([[UIApplication sharedApplication]canOpenURL:inputURL]){
        NSLog(@"open");
    }
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    NSLog(@"errrsfseSession %@", exportSession.error);
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
     {
         if(exportSession.status != AVAssetExportSessionStatusCompleted){
             NSLog(@"exportSession  %@", exportSession.error);
         }
         if (exportSession.status == AVAssetExportSessionStatusCompleted)
         {
            NSLog(@"doneszdfsadj");
    
         }
    
     }];
        }
    

    【讨论】:

    • 这没有回答问题。您的示例只是减小了视频的尺寸。这里的问题是,虽然尺寸相同,但从库中选择的视频比应用程序中录制的视频要小得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多