4

This is how I access the video:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    if let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL {
        let video = NSData(contentsOfURL: videoURL)
    }
}

That video I would like to show the user in my controller (I do not mean UIDocumentInteractionController to preview this). I need a thumbnail of that video, and then assign this to UIImageView. Is it possible?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • Look at this post: [Creating thumbnail from local video in swift](http://stackoverflow.com/questions/31779150/creating-thumbnail-from-local-video-in-swift) Regards – Romain Lebran Nov 27 '15 at 09:22

4 Answers4

15

The simplest answer:

import AVFoundation

private func thumbnailForVideoAtURL(url: NSURL) -> UIImage? {

    let asset = AVAsset(URL: url)
    let assetImageGenerator = AVAssetImageGenerator(asset: asset)

    var time = asset.duration
    time.value = min(time.value, 2)

    do {
        let imageRef = try assetImageGenerator.copyCGImageAtTime(time, actualTime: nil)
        return UIImage(CGImage: imageRef)
    } catch {
        print("error")
        return nil
    }
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 1
    Thumbnail generated successfully but image rotated to 90 degree. – AiOsN Jun 02 '16 at 09:16
  • Checking with youtube urls and getting this error: `Error Domain=AVFoundationErrorDomain Code=-11850 "Operation Stopped" UserInfo={NSUnderlyingError=0x7fb4a3c04870 {Error Domain=NSOSStatusErrorDomain Code=-12939 "(null)"}, NSLocalizedFailureReason=The server is not correctly configured., NSLocalizedDescription=Operation Stopped}` – Pushpendra Oct 24 '17 at 12:44
  • @Pushpendra tam getting the same error how did u solve it – veeresh kumbar Jul 27 '18 at 07:58
  • Thumbnail image is rotated by 90 degrees. – Faris Muhammed Jul 13 '19 at 07:20
  • 1
    To fix the thumbnail rotation, I used: `assetImageGenerator.appliesPreferredTrackTransform = true` as it is default false. – Pranav Kasetti Aug 24 '19 at 15:35
7

Here's SWIFT 3.0 using extension

import Foundation
import UIKit
import MediaPlayer

import SystemConfiguration

extension AVAsset{
    var videoThumbnail:UIImage?{

        let assetImageGenerator = AVAssetImageGenerator(asset: self)
        assetImageGenerator.appliesPreferredTrackTransform = true

        var time = self.duration
        time.value = min(time.value, 2)

        do {
            let imageRef = try assetImageGenerator.copyCGImage(at: time, actualTime: nil)
            let thumbNail = UIImage.init(cgImage: imageRef)


            print("Video Thumbnail genertated successfuly".DubugSuccess())

            return thumbNail

        } catch {

            print("error getting thumbnail video".DubugError(),error.localizedDescription)
            return nil


        }

    }
}

Then simply from anywhere you have access to AVAsset/URL:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    if let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL {
        let video = NSData(contentsOfURL: videoURL)

        //Create AVAsset from url
        let ass = AVAsset(url:videoURL)

        if let videoThumbnail = ass.videoThumbnail{
          print("Success")
        }
    }
}
Jad
  • 2,139
  • 1
  • 16
  • 28
2

You can generate a thumbnail with this method:

import AVFoundation

func generateThumnail(url : NSURL) -> UIImage?{
    let asset: AVAsset = AVAsset(URL: url)
    let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    let time        : CMTime = CMTimeMake(1, 30)
    let img         : CGImageRef
    do {
        try img = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
        let frameImg: UIImage = UIImage(CGImage: img)
        return frameImg
    } catch {

    }
    return nil
}
Manuel
  • 1,675
  • 12
  • 18
0

Answer for AiOsN and the way how to fix image rotation. You just need to change this line of code:

return UIImage(CGImage: imageRef)

to this one:

return UIImage(CGImage: imageRef scale: 1.0 , orientation: UIImageOrientation.Right)

and the image will show you correct rotation.