Saturday, November 19, 2016

Convert Movie to Images using Java

This blog post will help you to convert any Movie/Video to Images using Java.

Language Used:
Java

Git Location:
https://github.com/csanuragjain/converter/tree/master/MovieToImage

Pom Dependency:
Please add the below pom dependency from https://mvnrepository.com/artifact/org.bytedeco/javacv :
 <dependency>  
   <groupId>org.bytedeco</groupId>  
   <artifactId>javacv</artifactId>  
   <version>1.0</version>  
 </dependency>  

Program:
Main method:
       public static void main(String []args) throws Exception, IOException  
        {  
                 Scanner s=new Scanner(System.in);  
                 System.out.println("Enter the path of mp4 (for eg c:\\test.mp4)");  
                 String mp4Path=s.nextLine();  
                 System.out.println("Enter the folder path where the images will be saved (eg c:\\convertedImages)");  
                 String imagePath=s.nextLine();  
                 convertMovietoJPG(mp4Path, imagePath,"jpg",0);  
        }  

How it works:
1) We make a scanner object to read user input
2) We ask user to enter path of mp4 file
3) We ask user to enter folder path of output images
4) We call convertMovietoJPG method which converts the mp4 to images.
5) convertMovietoJPG takes 4 arguments.
6) First argument define the mp4 path.
7) Second argument define the output image folder path.
8) Third argument tells the extension for output images.
9) Fourth argument tells if we want to save all frames from video or not. For example if I give it as 2 then Frame2,Frame4... and likewise would be stored and Frame1,Frame3... likewise would be ignored. This is done to reduce the size of output image folder.

convertMovietoJPG method:
       public static void convertMovietoJPG(String mp4Path, String imagePath, String imgType, int frameJump) throws Exception, IOException  
       {  
                 Java2DFrameConverter converter = new Java2DFrameConverter();  
          FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(mp4Path);  
          frameGrabber.start();  
          Frame frame;  
          double frameRate=frameGrabber.getFrameRate();  
          int imgNum=0;  
          System.out.println("Video has "+frameGrabber.getLengthInFrames()+" frames and has frame rate of "+frameRate);  
          try {           
            for(int ii=1;ii<=frameGrabber.getLengthInFrames();ii++){  
            imgNum++;       
            frameGrabber.setFrameNumber(ii);  
            frame = frameGrabber.grab();  
            BufferedImage bi = converter.convert(frame);  
            String path = imagePath+File.separator+imgNum+".jpg";  
            ImageIO.write(bi,imgType, new File(path));  
            ii+=frameJump;  
            }  
            frameGrabber.stop();  
          } catch (Exception e) {  
            e.printStackTrace();  
          }  
        }  


How it works:
1) We create an object of Java2DFrameConverter which will help us to convert the frame obtained from video into BufferedImage.
2) We create an object of FFmpegFrameGrabber which will actually start grabbing the frames from the video
3) We start the frame grabber by calling the start method
4) Now we determine the video frame rate by using getFrameRate() method
5) Now we determine the number of frames peresent in the video using getLengthInFrames
6) We iterate through each frame in video.
7) For each loop iteration we tell frameGrabber the current frame by using setFrameNumber method
8) Using grab method we get the frame whose frame number is defined in step7
9) We convert the frame to BufferedImage using the convert method
10) We write the grabbed image to local disk using ImageIO.write
11) Assume we dont want all frames and only want to capture (All frames/2) then we keep frameJump to 2 so that ii always gets incremented by 2 and saving only half of actual images.
12) Now we stop the frameGrabber after the iteration completes which marks the completion of conversion.

Output:
Enter the path of mp4 (for eg c:\test.mp4)
C:\Users\anurag\Desktop\video\1.mp4
Enter the folder path where the images will be saved (eg c:\convertedImages)
C:\Users\anurag\Desktop\video\convert
Video has 132 frames and has frame rate of 25.0
Conversion complete. Please find the images at C:\Users\anurag\Desktop\video\convert

Reference:
http://stackoverflow.com/questions/15735716/how-can-i-get-a-frame-sample-jpeg-from-a-video-mov
https://code.google.com/archive/p/javacv/

Full Program:
 package com.cooltrickshome;  
 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.Scanner;  
 import javax.imageio.ImageIO;  
 import org.bytedeco.javacv.FFmpegFrameGrabber;  
 import org.bytedeco.javacv.Frame;  
 import org.bytedeco.javacv.FrameGrabber.Exception;  
 import org.bytedeco.javacv.Java2DFrameConverter;  
 public class MovieToImage {  
      /**  
       * @param args  
       * @throws IOException  
       */  
       public static void main(String []args) throws Exception, IOException  
        {  
                 Scanner s=new Scanner(System.in);  
                 System.out.println("Enter the path of mp4 (for eg c:\\test.mp4)");  
                 String mp4Path=s.nextLine();  
                 System.out.println("Enter the folder path where the images will be saved (eg c:\\convertedImages)");  
                 String imagePath=s.nextLine();  
                 convertMovietoJPG(mp4Path, imagePath,"jpg",0);  
                 System.out.println("Conversion complete. Please find the images at "+imagePath);  
        }  
       public static void convertMovietoJPG(String mp4Path, String imagePath, String imgType, int frameJump) throws Exception, IOException  
       {  
                 Java2DFrameConverter converter = new Java2DFrameConverter();  
          FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(mp4Path);  
          frameGrabber.start();  
          Frame frame;  
          double frameRate=frameGrabber.getFrameRate();  
          int imgNum=0;  
          System.out.println("Video has "+frameGrabber.getLengthInFrames()+" frames and has frame rate of "+frameRate);  
          try {           
            for(int ii=1;ii<=frameGrabber.getLengthInFrames();ii++){  
            imgNum++;       
            frameGrabber.setFrameNumber(ii);  
            frame = frameGrabber.grab();  
            BufferedImage bi = converter.convert(frame);  
            String path = imagePath+File.separator+imgNum+".jpg";  
            ImageIO.write(bi,imgType, new File(path));  
            ii+=frameJump;  
            }  
            frameGrabber.stop();  
          } catch (Exception e) {  
            e.printStackTrace();  
          }  
        }  
 }  

Hope it helps :)

11 comments:

  1. Most of the frames are blurred
    The video is OK and HitFilm show me nice frames
    See bad frames here
    https://drive.google.com/open?id=1fJ9RZXBsJWgEqBPRwqHJhMUbvdOhNUxj

    ReplyDelete
    Replies
    1. Can you please share the video. I will check

      Delete
    2. Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Now

      >>>>> Download Full

      Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download LINK

      >>>>> Download Now

      Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Full

      >>>>> Download LINK gR

      Delete
    3. Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Now

      >>>>> Download Full

      Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download LINK

      >>>>> Download Now

      Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Full

      >>>>> Download LINK HS

      Delete
  2. iDealshare VideoGo also helps to convert video frames to image. Simply add the video file to iDealshare VideoGo, and click Profile drop download button to select jpg, png or gif from Picture and Animation category, and click Convert button.

    ReplyDelete
  3. Nice article. It's very helpful to me. Thank you for share with us. Can you please check my Free Online Facebook Video Downloader tool.

    ReplyDelete
  4. One factor I do know is that my private image won't ever be discovered at my neighbor's home or in an area retailer, as a result of I discovered a spot that does one thing completely unique with any image, changing it right into a dialog piece, not only a image that hangs on the wall. If you want to learn more about this topic, then please visit https://onlineconvertfree.com

    ReplyDelete
  5. Thank you so much for this - Very helpful. I spent a whole night up trying to figure this out..

    ReplyDelete
  6. Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Now

    >>>>> Download Full

    Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download LINK

    >>>>> Download Now

    Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete
  7. Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Now

    >>>>> Download Full

    Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download LINK

    >>>>> Download Now

    Advance Programs And Tricks In Java: Convert Movie To Images Using Java >>>>> Download Full

    >>>>> Download LINK Xm

    ReplyDelete