从Live Photo中提取视频部分 [英] Extract video portion from Live Photo

查看:462
本文介绍了从Live Photo中提取视频部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Has anyone figured out how to extract the video portion from a Live Photo? I'm working on an app to convert Live Photos into a GIF, and the first step is to get the video file from the Live Photo. It seems like it should be possible, because if you plug in your phone to a Mac you can see the separate image and video files. I've kinda run into a brick wall in the extraction process, and I've tried many ways to do it and they all fail.

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

The first thing I did was obtain a PHAsset for what I think is the video part of the Live Photo, by doing the following:

    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,我尝试过:

To convert the PHAsset to an AVAsset I've tried:

    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.value的调试打印给出0.
我也尝试过没有添加.mov但它仍然不起作用。

I don't think this one works because the debug print with the duration.value gives 0. I've also tried without the ".mov" addition and it still doesn't work.

我也尝试过:

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

并且debugPrint(avAsset)打印nil所以它没有工作。

And the debugPrint(avAsset) prints nil so it doesn't work.

我有点担心他们可能无法做到这一点,似乎我已经进入圈子,因为它似乎是PHAsset I得到的仍然是实时照片,而不是一个视频。

I'm kind of afraid they might have made it impossible to do, it seems like I'm going in circles since it seems like the PHAsset I got is still a Live Photo and not actually a video.

推荐答案

使用 PHAssetResourceManager PHAssetResource 获取视频文件。

Use the PHAssetResourceManager to get the video file from the PHAssetResource.

PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes, 
    toFile: fileURL, options: nil, completionHandler: 
  {
     // Video file has been written to path specified via fileURL
  }

这篇关于从Live Photo中提取视频部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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