无法在iOS Swift中从Firebase视频URL生成视频缩略图?

时间:2018-12-05 06:18:39

标签: ios swift firebase video firebase-storage

我正在使用以下方法从远程服务器URL生成视频缩略图。如果我的网址不是Firebase网址,例如。

  

https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4

然后我可以生成缩略图,但是如果我的网址是firebase网址,如下所示

  

https://firebasestorage.googleapis.com/v0/b/shaberi-a249e.appspot.com/o/message-videos%2F8EDAC3FC-D754-4165-990A-97F6ECE120A6.mp4?alt=media&token=b3271370-a408-467d-abbc-7df2beef45c7

然后不生成视频缩略图。

获取视频缩略图的方法

 func createThumbnailOfVideoFromRemoteUrl(url: String) -> UIImage? {

        let asset = AVAsset(url: URL(string: url)!)
        let assetImgGenerate = AVAssetImageGenerator(asset: asset)
        assetImgGenerate.appliesPreferredTrackTransform = true
        //Can set this to improve performance if target size is known before hand
        //assetImgGenerate.maximumSize = CGSize(width,height)
        let time = CMTimeMakeWithSeconds(1.0, 100)
        do {
            let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
            let thumbnail = UIImage(cgImage: img)
            return thumbnail
        } catch {
            print(error.localizedDescription)
            return nil
        }
    }

请让我知道是什么问题?

1 个答案:

答案 0 :(得分:2)

请尝试下面的代码为我工作。

func createThumbnailOfVideoFromRemoteUrl(url: String) {

        if let asset = AVAsset(url: URL(string: url)!) as? AVAsset {
            //let durationSeconds = CMTimeGetSeconds(asset.duration)
            let generator = AVAssetImageGenerator(asset: asset)

            generator.appliesPreferredTrackTransform = true

            let time = CMTimeMakeWithSeconds(3.0, preferredTimescale: 600)
            //var thumbnailImage: CGImage

            generator.generateCGImagesAsynchronously(forTimes: [NSValue(time: time)]) { (time, thumbnail, cmtime, result, error) in
                if (thumbnail != nil) {
                    DispatchQueue.main.async {
                        self.profileImgView.image = UIImage(cgImage: thumbnail!)
                    }
                }
            }
        }
    }


self.createThumbnailOfVideoFromRemoteUrl(url: "https://firebasestorage.googleapis.com/v0/b/shaberi-a249e.appspot.com/o/message-videos%2F8EDAC3FC-D754-4165-990A-97F6ECE120A6.mp4?alt=media&token=b3271370-a408-467d-abbc-7df2beef45c7")

enter image description here