Sunday, December 11, 2016

Images to Movie Converter using Java

This blog post will help you to convert your images to a video using Java.

Language Used:
Java

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

Reference:
http://stackoverflow.com/questions/13643046/how-to-convert-images-into-video-in-android-using-javacv
http://stackoverflow.com/questions/28721396/convert-images-to-video-in-android
http://stackoverflow.com/questions/15867696/javacv-opencv-cvloadimage-not-working

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)  
        {  
                 Scanner s=new Scanner(System.in);  
                 System.out.println("Enter the directory path of images (for eg c:\\test)");  
                 String imgPath=s.nextLine();  
                 System.out.println("Enter the directory with video file name where resulting video will be saved (for eg c:\\test\\abc.mp4)");  
                 String vidPath=s.nextLine();  
                 ArrayList<String> links = new ArrayList<>();  
                 File f=new File(imgPath);  
                 File[] f2=f.listFiles();  
                 for(File f3:f2)  
                 {  
                      links.add(f3.getAbsolutePath());  
                 }  
                 convertJPGtoMovie(links, vidPath);  
                 System.out.println("Video has been created at "+vidPath);  
                 s.close();  
        }  

How it works:
1) We make a simple scanner object to get user input
2) We ask the directory containing the images to be converted to video
3) We ask user the resulting directory along with video file name from user.
4) We scan the directory obtained from step 2 and save all the image path in a ArrayList.
5) Now we call convertJPGtoMovie along with the ArrayList (containing all images path) and the resulting video path

convertJPGtoMovie method:
       public static void convertJPGtoMovie(ArrayList<String> links, String vidPath)  
       {  
            OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();  
            FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(vidPath,640,720);  
            try {  
                 recorder.setFrameRate(1);  
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);  
        recorder.setVideoBitrate(9000);  
        recorder.setFormat("mp4");  
        recorder.setVideoQuality(0); // maximum quality  
        recorder.start();  
              for (int i=0;i<links.size();i++)  
              {  
               recorder.record(grabberConverter.convert(cvLoadImage(links.get(i))));  
              }  
              recorder.stop();  
             }  
             catch (org.bytedeco.javacv.FrameRecorder.Exception e){  
               e.printStackTrace();  
             }  
       }  

How it works:
1) We make an object of ToIplImage named grabberConverter which we are going to use later for converting from IplImage to Frame Object
2) We make a FFmpegFrameRecorder object named recorder which would help to convert our images to video. It takes 3 argument. First is the video file path which need to be created. Second argument defines the image width and third argument defines the image height.
3) We define the frame rate of the resulting video. Similarly we need to set other video parameters like video codec, bitrate, format, quality.
4) After video configuration has been made we start the recording by calling the start method.
5) Now cvLodImage method takes the image we passed and convert them into IplImage. Now we use grabberConverter from step1 to convert the IplImage into Frame.
6) We utilise the record method of FFmpegFrameRecorder which takes the Frame from step5 and place it in the resulting video. This way our video has the first image.
7) Step5 and Step6 are repeated over loop covering all the images which need to be embedded into the video.
8) After all images are covered, we call the stop button which marks the completion of our video.

Output:
 Enter the directory path of images (for eg c:\test)  
 C:\Users\anurag\Desktop\images\extra  
 Enter the directory where resulting video will be saved (for eg c:\test\abc.mp4)  
 C:\Users\anjain\Desktop\images\extra\5.mp4  
 Video has been created at C:\Users\anurag\Desktop\images\extra\5.mp4  
 Output #0, mp4, to 'C:\Users\anjain\Desktop\images\extra\5.mp4':  
   Stream #0:0: Video: mpeg4, yuv420p, 640x720, q=2-31, 9 kb/s, 1 tbn, 1 tbc  

Full Program:
 package com.cooltrickshome;  
 import static org.bytedeco.javacpp.opencv_imgcodecs.*;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 import java.util.Scanner;  
 import org.bytedeco.javacpp.avcodec;  
 import org.bytedeco.javacv.FFmpegFrameRecorder;  
 import org.bytedeco.javacv.OpenCVFrameConverter;  
 public class ImageToMovie {  
      /**  
       * @param args  
       * @throws IOException   
       */  
       public static void main(String []args)  
        {  
                 Scanner s=new Scanner(System.in);  
                 System.out.println("Enter the directory path of images (for eg c:\\test)");  
                 String imgPath=s.nextLine();  
                 System.out.println("Enter the directory with video file name where resulting video will be saved (for eg c:\\test\\abc.mp4)");  
                 String vidPath=s.nextLine();  
                 ArrayList<String> links = new ArrayList<>();  
                 File f=new File(imgPath);  
                 File[] f2=f.listFiles();  
                 for(File f3:f2)  
                 {  
                      links.add(f3.getAbsolutePath());  
                 }  
                 convertJPGtoMovie(links, vidPath);  
                 System.out.println("Video has been created at "+vidPath);  
                 s.close();  
        }  
       public static void convertJPGtoMovie(ArrayList<String> links, String vidPath)  
       {  
            OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();  
            FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(vidPath,640,720);  
            try {  
                 recorder.setFrameRate(1);  
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);  
        recorder.setVideoBitrate(9000);  
        recorder.setFormat("mp4");  
        recorder.setVideoQuality(0); // maximum quality  
        recorder.start();  
              for (int i=0;i<links.size();i++)  
              {  
               recorder.record(grabberConverter.convert(cvLoadImage(links.get(i))));  
              }  
              recorder.stop();  
             }  
             catch (org.bytedeco.javacv.FrameRecorder.Exception e){  
               e.printStackTrace();  
             }  
       }  
 }  

Hope it helps :)

10 comments:

  1. Hello, there is one way the put text on the images?

    ReplyDelete
    Replies
    1. This can help
      https://stackoverflow.com/questions/2736320/write-text-onto-image-in-java

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

      >>>>> Download Full

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

      >>>>> Download Now

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

      >>>>> Download LINK Hf

      Delete
  2. How to convert the image as grabber.

    ReplyDelete
  3. Is it possible to use an array or list of images rather than read them as files? I already created a list of frames in my program and I would prefer not to save them to the disk just to build a video from them.

    ReplyDelete
  4. Amazing video converter for online is available here:

    File to File Converter

    ReplyDelete
  5. I am unable to execute this code on a linux machine.
    Oct 27, 2020 10:57:19 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacv.FFmpegFrameRecorder] with root cause
    java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacv.FFmpegFrameRecorder
    at com.winfo.scripts.SeleniumKeyWords.convertJPGtoMovie(SeleniumKeyWords.java:836)
    at com.winfo.scripts.SeleniumKeyWords.getFailedPdfNew(SeleniumKeyWords.java:699)
    at com.winfo.scripts.SeleniumKeyWords.createPdf(SeleniumKeyWords.java:1031)
    at com.winfo.scripts.RunAutomation.report(RunAutomation.java:46)
    at com.winfo.controller.JobController.executeTestScript(JobController.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:142)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)


    I am getting the above error. Can anyone help?
    Thanks in advance.

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

    >>>>> Download Full

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

    >>>>> Download Now

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

    >>>>> Download LINK

    ReplyDelete