neekeetab / CachingPlayerItem

Play and cache media content simultaneously on your iOS device
MIT License
520 stars 89 forks source link

Generating Thumbnails for Remote Videos without File Extensions #47

Closed yilmazedis closed 1 year ago

yilmazedis commented 1 year ago

Problem Statement: I have remote videos that are missing their file extensions. I can play the videos using CachingPlayerItem, but I also need to generate their thumbnails. I am struggling to come up with an algorithm to accomplish this task and need assistance.

Request for Help: Can someone provide guidance or a solution for generating thumbnails for remote videos without file extensions? Any help would be greatly appreciated.

smhk commented 1 year ago

I may have good news

Apple now do all this. Are you aware of AVAssetImageGenerator

Hope it helps. @yilmazedis

yilmazedis commented 1 year ago

I am currently using AVAssetImageGenerator to generate thumbnails for videos with extensions, but it doesn't work for remote videos without extensions. I need a solution to generate thumbnails for these videos. I use this.

smhk commented 1 year ago

Gotchya. Ah, i too noticed that issue. I often just had to change extensions on the server. I did not think of the problem "no extension". FWIW perhaps ask on stackoverflow. Sorry I have no solution.

10Macit commented 1 year ago

You can use mime types with AVURLAsset if there is no file extension with video url's.

    func getThumbnailImageFromVideoUrl(url: URL, completion: @escaping ((_ image: UIImage?)->Void)) {
        DispatchQueue.global().async { //1
           let mimeType = "video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\""
            let asset = AVURLAsset(url: url, options: ["AVURLAssetOutOfBandMIMETypeKey": mimeType])
            let avAssetImageGenerator = AVAssetImageGenerator(asset: asset) //3
            avAssetImageGenerator.appliesPreferredTrackTransform = true //4
            let thumnailTime = CMTimeMake(value: 0, timescale: 1) //5
            do {
                let cgThumbImage = try avAssetImageGenerator.copyCGImage(at: thumnailTime, actualTime: nil) //6
                let thumbImage = UIImage(cgImage: cgThumbImage) //7
                DispatchQueue.main.async { //8
                    completion(thumbImage) //9
                }
            } catch {
                print(error.localizedDescription) //10
                DispatchQueue.main.async {
                    completion(nil) //11
                }
            }
        }
    }
yilmazedis commented 1 year ago

Thank you so much, I didn't know that. It is a lot for me.