Search code examples
iosswiftvideo-streamingvlcvideo-processing

Make snapshot of VLCMediaPlayer (MobileVLCKit)


I'm working on an app, showing stream video (by rtmp). I've started to use MobileVLCKit for it. I need a function that will make a snapshot of video. I have tried the built-in function

    func saveVideoSnapshotAt(path: String!, withWidth: UInt32, andHeight: UInt32)

But it doesn't work. That's what support have written about it on videolan forum:

"This method is OS X only. While it is exposed on iOS (what it should not be, I agree), it won't work on mobile devices."

Then I tried to use standard iOS functionality:

    @IBOutlet weak var videoView: UIView!
    var mediaPlayer = VLCMediaPlayer()
    if let url = NSURL(string: "rtmp://someurl") {
        let media = VLCMedia(URL: url)
        mediaPlayer.media = media
        mediaPlayer.drawable = videoView
        mediaPlayer.play()
    }
    UIGraphicsBeginImageContext(videoView.frame.size);
    guard let context = UIGraphicsGetCurrentContext() else {
        print("Couldn't get context")
        return
    }
    videoView.layer.renderInContext(context)
    guard let screenshot = UIGraphicsGetImageFromCurrentImageContext() else {
        UIGraphicsEndImageContext()
        return
    }
    UIGraphicsEndImageContext()

In result I get only UIView background screenshot, not video in it. Does anyone know if there is any solution or at least which direction I should keep searching?


Solution

  • Solution was simpler than i thought.

        let size = mediaPlayer.drawable.frame.size
    
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)
    
        let rec = mediaPlayer.drawable.frame
        mediaPlayer.drawable.drawViewHierarchyInRect(rec, afterScreenUpdates: false)
    
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();