Extract a frame image from video in scala


Sometimes it becomes a general requirement to show the preview of a video wherever video gets displayed. Like on YouTube , If you’ll right click on almost any video , you’d find that each video contains a preview image of a frame of that video.

Here is a short tutorial for doing the same i.e extracting a frame image from a video in Scala via using Xuggle.

Add the following dependency in your project to get the xuggler jar.

 "xuggle" % "xuggle-xuggler" % "5.4"

Following is the resolver for the jar

 resolvers += "xuggle repo" at "http://xuggle.googlecode.com/svn/trunk/repo/share/java/"

Lets us start the process step by step :

1. Instantiate the reader from “xuggle.mediatool.ToolFactory” and specify “from where to read the video”.

 val mediaReader: IMediaReader = ToolFactory.makeReader("/home/neelkanth/Desktop/demo.mp4")

2. Set the limit in which mediaReader has to read the video files.

   var count = 0
    while (count < 1) {
      mediaReader.readPacket()
    }

3. Set the type of Buffere Image you want to extract.

  mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR)

4. Now we have to add a media listner for the “mediaReader” reader so lets implement the listner.

  class ImageSnapListener extends MediaListenerAdapter {

      override def onVideoPicture(event: IVideoPictureEvent) = {
       dumpImageToFile(event.getImage)
        count = count + 1
      }

      def dumpImageToFile(image: BufferedImage) {
        ImageIO.write(image, "png", new File("/home/neelkanth/Desktop/frame"))

      }
    }

what we did here is :-

i) – Overridden the onVideoPicture method and increased the counter by one in order
to deny the mediaReader to read more files.
ii)- Written a method dumpImageToFile that makes the frame image to be written on specified location using javax.imageio.ImageIO.

Here is the complete code file that you can run directly. Just specify the path in
– mediaReader of “From where to read ?”
-ImageIO of “Where to write the extracted frame image?”

import com.xuggle.mediatool.event.IVideoPictureEvent
import java.awt.image.BufferedImage
import com.xuggle.mediatool.MediaListenerAdapter
import javax.imageio.ImageIO
import com.xuggle.mediatool.ToolFactory
import com.xuggle.mediatool.IMediaReader
import java.io.File

object FrameFromVideo  extends App {

  def extractFrameFromVideo {
    
    var count = 0
    
    // Call the reader from "xuggle.mediatool.ToolFactory"
    val mediaReader: IMediaReader = ToolFactory.makeReader("/home/neelkanth/Desktop/demo.mp4")

    // Setting buffered image type
    mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR)

    mediaReader.addListener(new ImageSnapListener)

    while (count < 1) {
      mediaReader.readPacket()
    }

    class ImageSnapListener extends MediaListenerAdapter {

      override def onVideoPicture(event: IVideoPictureEvent) = {
        val obtainedinputStream = dumpImageToFile(event.getImage)
        count = count + 1
      }

      def dumpImageToFile(image: BufferedImage) {
        ImageIO.write(image, "png", new File("/home/neelkanth/Desktop/frame"))

      }
    }
  }
  
  extractFrameFromVideo

}

About Neelkanth Sachdeva

Software Consultant @ Knoldus Software LLP Co-Founder @ My Cell Was Stolen
This entry was posted in Java, Scala, Web and tagged , . Bookmark the permalink.

One Response to Extract a frame image from video in scala

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s