从Live Photo中提取video部分

有没有人想出如何从现场照片提取video部分? 我正在开发一个应用程序,将Live Photos转换为GIF,第一步是从Live Photo获取video文件。 这似乎应该是可能的,因为如果你插入手机到Mac,你可以看到单独的图像和video文件。 我在提取过程中遇到了一堵砖墙,而且我尝试了很多方法来完成,而且都失败了。

我做的第一件事是通过执行以下操作获取我认为是Live Photovideo部分的PHAsset:

if let livePhoto = info["UIImagePickerControllerLivePhoto"] as? PHLivePhoto { let assetResources = PHAssetResource.assetResourcesForLivePhoto(livePhoto) for assetRes in assetResources { if (assetRes.type == .PairedVideo) { let assets = PHAsset.fetchAssetsWithLocalIdentifiers([assetRes.assetLocalIdentifier], options: nil) if let asset = assets.firstObject as? PHAsset { 

将PHAsset转换为AVAsset我试过了:

  asset.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) -> Void in if let url = contentEditingInput?.fullSizeImageURL { let movieUrl = url.absoluteString + ".mov" let avAsset = AVURLAsset(URL: NSURL(fileURLWithPath: movieUrl), options: nil) debugPrint(avAsset) debugPrint(avAsset.duration.value) } }) 

我不认为这一个工作,因为与duration.valuedebugging打印0.我也尝试没有“.mov”加法,它仍然无法正常工作。

我也试过:

  PHImageManager.defaultManager().requestAVAssetForVideo(asset, options: nil, resultHandler: { (avAsset, audioMix, info) -> Void in debugPrint(avAsset) }) 

而debugPrint(avAsset)打印零,所以它不起作用。

我有点害怕他们可能做不到这一点,看起来好像我在圈子里,因为看起来PHAsset仍然是一个现场照片,而不是一个video。

使用PHAssetResourceManagerPHAssetResource获取video文件。

 PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes, toFile: fileURL, options: nil, completionHandler: { // Video file has been written to path specified via fileURL } 
 // suppose you have PHAsset instance (you can get it via [PHAsset fetchAssetsWithOptions:...]) PHAssetResource *videoResource = nil; NSArray *resourcesArray = [PHAssetResource assetResourcesForAsset:asset]; const NSInteger livePhotoAssetResourcesCount = 2; const NSInteger videoPartIndex = 1; if (resourcesArray.count == livePhotoAssetResourcesCount) { videoResource = resourcesArray[videoPartIndex]; } if (videoResource) { NSString * const fileURLKey = @"_fileURL"; NSURL *videoURL = [videoResource valueForKey:fileURLKey]; // load video url using AVKit or AVFoundation } 

我不小心做到了。 我有一个名为Goodreader的ios应用程序(在appstore中可用),它具有一个类似Windows的文件pipe理器。 导入实况照片时,会将其保存为以.pvt结尾的文件夹,其中包含jpg和mov文件。 只有一个警告:您需要在邮件应用程序中打开实时照片,然后将其发送给自己或其他人,以查看“导入至好评”选项,而不是从照片应用程序。