繁体   English   中英

使用PhotoKit(PHAsset)导出视频每次都会显示不同的视频文件

[英]Exporting video using PhotoKit (PHAsset) gives different video file every time

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

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

还有一点:在同一个应用程序运行中运行该方法两次,给出了两个相同的文件。

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

- (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)复制到我的文档文件夹,然后每次都复制相同的视频文件。 现在这是我的最终解决方案。

在这种情况下,您必须使用requestAVAssetForVideo而不是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");
     }
 }];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM