Capture first frame of the video as an image on iOS and macOS in swift

import Foundation
import Cocoa
import AVKit
import AVFoundation

let asset: AVURLAsset = AVURLAsset.init(url: "URL OF ANY VIDEO local/remote")
let imgGenerator: AVAssetImageGenerator = AVAssetImageGenerator.init(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
            
imgGenerator.generateCGImagesAsynchronously(forTimes: [NSValue(time: CMTime.zero)]) { (_, image, _, res, error) in
                
                if error == nil {
                    DispatchQueue.main.async {
                           let imageView = NSImage.init(cgImage: image!, size: NSSize.init(width: 180.0, height: 180.0));
                    }
                } else {
                    DispatchQueue.main.async {
                        // Some other fallback image
                    }
                }
}
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •