使用 UIImagePickerController 选择电影时避免视频压缩?

发布于 2024-09-07 15:53:01 字数 151 浏览 4 评论 0原文

我正在使用 UIImagePickerController 允许我的用户从资源库中选择视频。

当用户选择第二个屏幕上的“选择”按钮时,视图会显示进度条和“正在压缩视频...”消息。

为什么会发生这种情况?

有什么方法可以避免这种压缩操作吗?

I'm using UIImagePickerController to allow my user to select a video from the asset library.

When the user selects the "Choose" button on the second screen, the view displays a progress bar and a "Compressing Video..." message.

Why is this happening?

Is there some way I can avoid this compression operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自由如风 2024-09-14 15:53:01

答:目前无法控制 UIImagePickerController 如何压缩选取的视频。

我刚刚做了一些快速测试。使用我创建的测试应用程序,我两次选择相同的视频 - 一次将 videoQuality 属性设置为 UIImagePickerControllerQualityTypeHigh,一次将其设置为 UIImagePickerControllerQualityTypeLow代码>.复制的结果文件大小完全相同,均为 15.1MB,帧大小为 360x480。原件大小为 72.5MB,帧大小为 480x640。显然这个属性根本不影响所使用的压缩。

Answer: There is currently no way to control how UIImagePickerController compresses the picked video.

I just did some quick tests. Using a test app I created, I picked the same video two times -- once with the videoQuality property set to UIImagePickerControllerQualityTypeHigh and once with it set to UIImagePickerControllerQualityTypeLow. The resulting files that were copied over are exactly the same size, 15.1MB with a frame size of 360x480. The original was 72.5MB with a frame size of 480x640. Apparently this property doesn't affect the compression used at all.

风情万种。 2024-09-14 15:53:01

UIImagePickerControllervideoQuality 属性设置为“高”(UIImagePickerControllerQualityTypeHigh = 0)

从 SDK 文档中:
“如果在图像选择器中显示录制的影片,请指定您不想降低影片的视频质量。”

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/c_ref/UIImagePickerControllerQualityType

Set the videoQuality property of the UIImagePickerController to "High" (UIImagePickerControllerQualityTypeHigh = 0)

From the SDK documentation:
"If displaying a recorded movie in the image picker, specifies that you do not want to reduce the video quality of the movie."

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/c_ref/UIImagePickerControllerQualityType

你在我安 2024-09-14 15:53:01

由于还没有办法避免使用 UIImagePickerController 进行压缩,因此我想包括一些关于如何创建自己的图像选择器来避免压缩的想法。

这将允许访问原始视频文件:

iOS 8

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
for (PHAsset *asset in assetsFetchResult) {
    PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
    videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal;

    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:videoRequestOptions resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        // the AVAsset object represents the original video file
    }];
}

查看 PhotoKit 文档以访问集合(时刻)和其他选项。

以下是 Apple 使用 PhotoKit 的示例应用程序,可以将其修改为照片选择器: https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

这是 GitHub 上的一个照片选择器库,它使用 PhotoKit,看起来很有前途,因为它为您提供了所有选定图像/视频的 PHAsset 对象: https://github.com/guillermomuntaner/GMImagePicker

iOS 7及以下

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        // If you want, you can filter just pictures or videos
        // I just need videos so I do this:
        [group setAssetsFilter:[ALAssetsFilter allVideos]];

        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
            if (asset){
                // You can now add this ALAsset in your own video picker.
                // Note that you can only access the ALAsset as long as 
                // you maintain a reference to the ALAssetsLibrary

                // Or if you want to process the video, you can create an AVAsset:
                NSURL *url = asset.defaultRepresentation.url;
                AVAsset *videoAsset = [AVAsset assetWithURL:url];
            }
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];

Since there is no way yet to avoid compression using UIImagePickerController, I wanted to include some ideas of how you can create your own image picker that will avoid compression.

This will allow access to the raw video files:

iOS 8

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
for (PHAsset *asset in assetsFetchResult) {
    PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
    videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal;

    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:videoRequestOptions resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        // the AVAsset object represents the original video file
    }];
}

Look at the PhotoKit documentation for accessing collections (moments) and other options.

Here is a sample app from Apple using PhotoKit that could be modified to be a photo picker: https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

Here is a photo picker library on GitHub that uses PhotoKit that looks promising since it gives you the PHAsset objects for all the selected images/videos: https://github.com/guillermomuntaner/GMImagePicker

iOS 7 and below

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        // If you want, you can filter just pictures or videos
        // I just need videos so I do this:
        [group setAssetsFilter:[ALAssetsFilter allVideos]];

        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
            if (asset){
                // You can now add this ALAsset in your own video picker.
                // Note that you can only access the ALAsset as long as 
                // you maintain a reference to the ALAssetsLibrary

                // Or if you want to process the video, you can create an AVAsset:
                NSURL *url = asset.defaultRepresentation.url;
                AVAsset *videoAsset = [AVAsset assetWithURL:url];
            }
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
堇年纸鸢 2024-09-14 15:53:01

从 iOS 11 开始,您可以指定 videoExportPreset 并将其设置为 AVAssetExportPresetPassthrough

picker.videoExportPreset = AVAssetExportPresetPassthrough

这仍然会显示“压缩”进度条,但速度会快得多,特别是对于较小的视频。

Starting from iOS 11, you can specify the videoExportPreset and set it to AVAssetExportPresetPassthrough:

picker.videoExportPreset = AVAssetExportPresetPassthrough

This will still show the "compressing" progress bar though, but will be much faster, especially for smaller videos.

你不是我要的菜∠ 2024-09-14 15:53:01

对于那些提供使用 videoQuality 属性建议的人,文档明确指出它是视频捕获选项,而不是选择器选项。

正如 Jack 在下面提到的,它也用于转码。看起来我读文档的速度太快了,因为我没有注意到转码的提及。

For those giving the advice to use the videoQuality property, documentation is clearly stating that it is a video capture option, not a picker option.

As Jack is mentioning it below, it is also for transcoding. Looks like I read the doc too quickly because I didn't notice the transcoding mention.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文