使用PhotoKit(PHAsset)导出视频每次都会显示不同的视频文件 [英] Exporting video using PhotoKit (PHAsset) gives different video file every time

查看:657
本文介绍了使用PhotoKit(PHAsset)导出视频每次都会显示不同的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用该方法(此问题的结尾)从设备中检索视频。它做了什么,它找到了库中的第一个视频,创建了导出会话并将视频导出到MOV文件中。

I use the method (a the end of this question) to retrieve video from the device. What it does, it finds the first video in the library, creates export session and exports the video into MOV file.

两次运行应用程序后(停止应用程序)方法运行),正在比较两个结果文件。两个文件都不同。我期待两个文件都是相同的,因为正在导出相同的资产。

After two runs of the application (stopping the app between method runs), two resulting files are being compared. Both files are different. I was expecting that both files would be the same, as the same asset is being exported.

还有一句话:在同一个应用程序运行中运行该方法两次给了我两个与预期相同的文件。

One more remark: running the method twice in the same application run gives me two identical files as expected.

是否可以让PhotoKit每次运行时导出相同的文件?

Is it possible to make PhotoKit to export the same file every time it runs?

- (void)testVideoRetrievalSO {

    PHAsset *oneVideo = [[PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil] firstObject];

    PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
    options.networkAccessAllowed = YES;
    options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
    options.version = PHVideoRequestOptionsVersionOriginal;


    [[PHImageManager defaultManager] requestExportSessionForVideo:oneVideo
                                                          options:options
                                                     exportPreset:AVAssetExportPresetPassthrough
                                                    resultHandler:
     ^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
         NSLog(@"Video test run on asset %@", oneVideo.localIdentifier);
         NSString *folderPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
         NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingPathExtension:@"mov"];
         NSString *tempFile = [folderPath stringByAppendingPathComponent:fileName];
         NSURL *tempFileUrl = [NSURL fileURLWithPath:tempFile];

         [exportSession setOutputFileType:AVFileTypeQuickTimeMovie];
         [exportSession setOutputURL:tempFileUrl];

         [exportSession exportAsynchronouslyWithCompletionHandler:^{
             NSLog(@"Video test run exported video into file: %@", tempFile);
         }];
     }];
}


推荐答案

更新

目前尚不清楚,但我认为从相机胶卷输出视频并不能保证每次都能获取相同的视频。所以我通过 [NSFileManager copyItemAtURL:toURL:error:] 将视频从相机胶卷复制到我的文档文件夹和url(avurlasset.URL),然后每次复制相同的视频文件。目前这是我的最终解决方案。

It's not clear but I think exporting video from the camera roll does not guarantee fetching same video in every time. So I copied the video from camera roll to my document folder with url (avurlasset.URL) by [NSFileManager copyItemAtURL:toURL:error:] then it copies the same video file in every time. For now it is my final solution.

在这种情况下,你必须使用 requestAVAssetForVideo 而不是 requestExportSessionForVideo

In this case you have to use requestAVAssetForVideo not requestExportSessionForVideo

所以在你的情况下,

PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.version = PHVideoRequestOptionsVersionOriginal;

[[PHImageManager defaultManager] requestAVAssetForVideo:asset
                                                options:options
                                          resultHandler:
 ^(AVAsset * _Nullable avasset,
   AVAudioMix * _Nullable audioMix,
   NSDictionary * _Nullable info)
{
     NSError *error;
     AVURLAsset *avurlasset = (AVURLAsset*) avasset;

     // Write to documents folder
     NSURL *fileURL = [NSURL fileURLWithPath:tmpShareFilePath];
     if ([[NSFileManager defaultManager] copyItemAtURL:avurlasset.URL
                                                 toURL:fileURL
                                                 error:&error]) {
         NSLog(@"Copied correctly");
     }
 }];

这篇关于使用PhotoKit(PHAsset)导出视频每次都会显示不同的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆