in Education by
I am trying to get a thumbnail image from a video from a URL using AVFoundation but I am getting a black blank image as attached. The video URL is https://www.riptide.tv/app/assets/2/3/small_3001190658061_2.mp4 Below is my code: func videoSnapshot(videoURL: String, imgName : String) -> UIImage? { let asset = AVAsset(url: URL(string: videoURL)!) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true let time = CMTimeMakeWithSeconds(Float64(1), preferredTimescale: 100) do { let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) let thumbnail = UIImage(cgImage: img) downloadedThumbnails.updateValue(thumbnail, forKey: imgName) return thumbnail } catch { return UIImage(named: imgName) } } black img extracted JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If you change your code to func videoSnapshot(videoURL: String, imgName : String) -> UIImage? { let asset = AVAsset(url: URL(string: videoURL)!) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true let time = CMTimeMakeWithSeconds(Float64(5), preferredTimescale: 100) do { let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) let thumbnail = UIImage(cgImage: img) return thumbnail } catch { return UIImage(named: imgName) } } thats 5 seconds in. You can see the thumbnail generated. This is because AVAssetImageGenerator is pretty flexible in what times it gets its frames func videoSnapshot(videoURL: String, imgName : String) -> UIImage? { let asset = AVAsset(url: URL(string: videoURL)!) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true assetImgGenerate.requestedTimeToleranceAfter = .zero assetImgGenerate.requestedTimeToleranceBefore = .zero let time = CMTimeMakeWithSeconds(Float64(1), preferredTimescale: 100) do { let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) let thumbnail = UIImage(cgImage: img) return thumbnail } catch { return UIImage(named: imgName) } } Setting assetImgGenerate.requestedTimeToleranceAfter = .zero assetImgGenerate.requestedTimeToleranceBefore = .zero ensures you get a frame accurate thumbnail. For more info see https://developer.apple.com/documentation/avfoundation/avassetimagegenerator/1390571-requestedtimetolerancebefore

Related questions

0 votes
    I have to download images to my iOS apps local file system and read them back. On my simulator I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I have a table of items with a checkmark disclosure button, and I want to create a second table ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I am facing a problem that makes me to waste a lot of time. Each time I insert an element on a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am trying to upload image by converting image to base64 format. And i am getting below error. Error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am facing a problem that makes me to waste a lot of time. Each time I insert an element on a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    I replaced the Google Analytics SDK with the Firebase SDK. I'm looking to generate some of the previously- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    in Obj-C it was possible to iterate through alphabet with: for (char x='A'; x...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    i am new to iphone app development.i am trying to connect facebook using the facebook api's using iphone ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a single view in my application which does not receive any events from iOS. I tried to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    Convert a .doc or .pdf to an image and display a thumbnail in Ruby? Does anyone know how to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Convert a .doc or .pdf to an image and display a thumbnail in Ruby? Does anyone know how to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Convert a .doc or .pdf to an image and display a thumbnail in Ruby? Does anyone know how to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    A card is drawn from a well-shuffled pack of 52 cards. Find the probability of getting (i) a red face card (ii) a black king. Select the correct answer from above options...
asked Nov 23, 2021 in Education by JackTerrance
0 votes
    A card is drawn from a well shuffled deck of cards. What is the probability of getting a king, given that the card ... \frac{7}{13}\) Select the correct answer from above options...
asked Nov 19, 2021 in Education by JackTerrance
...