简体   繁体   中英

Unable to generate video thumbnail from firebase video url in iOS swift?

I am using a below method to generate a video thumbnail from a remote server url. If my url is not a firebase url for eg.

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

Then I am able to generate the thumbnail but if my url is firebase url like below

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

Then video thumbnail is not generated.

Method for getting video thumbnail

 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
        }
    }

Please let me know what is the issue?

Please try below code it's working for me.

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")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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