Showing posts with label movie. Show all posts
Showing posts with label movie. Show all posts

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 :)

Saturday, November 19, 2016

Play Movies & Videos on Desktop Wallpaper using Java

This blog post will help you play movie/video on your desktop wallpaper.

Langauge Used:
Java & C++

Git Location:
https://github.com/csanuragjain/extra/tree/master/DesktopMovieWallpaper

Working Software:

Part 1 https://github.com/csanuragjain/extra/blob/master/DesktopMovieWallpaper/Working%20Software/Working%20Software.z01?raw=true
Part 2
https://github.com/csanuragjain/extra/blob/master/DesktopMovieWallpaper/Working%20Software/Working%20Software.zip?raw=true

Download both part and use winzip or winrar to extract.

Concept:

1) We ask user the video which he would like to set as desktop wallpaper
2) Now we convert the video to images using tutorial http://cooltrickshome.blogspot.com/2016/11/convert-movie-to-images-using-java.html
3) Assume step 2 converted the video to 100 images.
4) We will start a loop and set desktop wallpaper all the 100 images found in step 3 one by one.
5) Overall effect is a movie playing in desktop wallpaper

Note:
1) This program converts the video to images. The overall converted images will be of size greater than the video. Make sure you have sufficient space if you are trying to convert a big video.

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>  

Java Program:
Main method:
      public static void main(String[] args) throws InterruptedException, 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();  
            File imgPath=new File("convert");  
            imgPath.delete();  
            imgPath.mkdirs();  
            String imagePath=imgPath.getAbsolutePath();  
            convertMovietoJPG(mp4Path, imagePath,"jpg",3);  
            System.out.println("Conversion complete. Please find the images at "+imagePath);  
            changeDesktopWallpaper(imagePath,5000);  
      }  

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 make a new folder named convert in current project directory. This will be the directory where images extracted from video would be kept
4) We call convertMovietoJPG method which converts the mp4 to images. I kept frametoJump as 3 but you may change it as per your requirement. More details on this http://cooltrickshome.blogspot.com/2016/11/convert-movie-to-images-using-java.html
5) Finally we call changeDesktopWallpaper which will set the images extracted from movie as wallpaper one by one giving a movie effect.
6) First argument defines the path containing the converted image and 5000 represents the millisecond after which video will start replay after it completes.

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) Already covered the explanation at http://cooltrickshome.blogspot.com/2016/11/convert-movie-to-images-using-java.html

changeDesktopWallpaper method:
      public static void changeDesktopWallpaper(String path, int sleepTime) throws InterruptedException  
      {  
           System.out.println("Starting to replay video after every "+(sleepTime/1000)+"s");  
           File f=new File(path);  
           File[] filePath=f.listFiles();  
           while(true)  
           {  
           for(int i=1;i<=filePath.length;i++)  
           {       
           changeWallpaper(path+"\\"+i+".jpg");  
           }  
           Thread.sleep(sleepTime);  
           }  
      }  


How it works:
1) We point file array on all the images extracted from video
2) For each image we call changeWallpaper which changes the wallpaper to that image.
3) Once all images are complete it waits for sleepTime and then restarts from step2

C++ Program:

wallpaperchanger.h
 /* DO NOT EDIT THIS FILE - it is machine generated */  
 #include <jni.h>  
 /* Header for class com_cooltrickshome_DesktopMovieWallpaper */  
 #ifndef _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #define _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #ifdef __cplusplus  
 extern "C" {  
 #endif  
 /*  
  * Class:   com_cooltrickshome_DesktopMovieWallpaper  
  * Method:  changeWallpaper  
  * Signature: (Ljava/lang/String;)I  
  */  
 JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *, jclass, jstring);  
 #ifdef __cplusplus  
 }  
 #endif  
 #endif  

wallpaperchanger.cpp
  #include <iostream>  
  #include <windows.h>  
  #include <fstream>  
  #include <cstdlib>  
  #include <jni.h>  
  #include "wallpaperchanger.h"  
  JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *env, jclass, jstring wallpaper){  
  const char *wallpaper_file = env->GetStringUTFChars(wallpaper, JNI_FALSE);  
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void *)wallpaper_file, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);  
  env->ReleaseStringUTFChars(wallpaper, wallpaper_file);  
  }  

How it works:
1) changeWallpaper method used by Java is defined in wallpaperchanger.dll which is created using wallpaperchanger.h & wallpaperchanger.cpp (Conversion from cpp & h to dll is explained at https://cooltrickshome.blogspot.in/2016/11/creating-your-personal-keylogger-from.html)
2) For cpp code, wallpaper variable contains the path of the wallpaper which is passed by Java
3) First we need to convert it to const char* which is done using env->GetStringUTFChars
4) SystemParametersInfo method is used to convert the wallpaper. We pass the desktop wallpaper path in it to change the wallpaper
5) Now we release the converted string.

Output:
 Enter the path of mp4 (for eg c:\test.mp4)  
 C:\Users\anjain\Desktop\images\rough\1.mp4  
 Video has 132 frames and has frame rate of 25.0  
 Conversion complete. Please find the images at C:\Users\anjain\workspace\BrowserMobProxy\cooltrickshome\convert  
 Starting to replay video after every 5s  

Full Program:

DesktopMovieWallpaper.java
 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.Java2DFrameConverter;  
 import org.bytedeco.javacv.FrameGrabber.Exception;  
 public class DesktopMovieWallpaper {  
      public static native int changeWallpaper(String path);  
      static  
   {  
     System.loadLibrary("wallpaperchanger");  
   }  
      public static void changeDesktopWallpaper(String path, int sleepTime) throws InterruptedException  
      {  
           System.out.println("Starting to replay video after every "+(sleepTime/1000)+"s");  
           File f=new File(path);  
           File[] filePath=f.listFiles();  
           while(true)  
           {  
           for(int i=1;i<=filePath.length;i++)  
           {       
           changeWallpaper(path+"\\"+i+".jpg");  
           }  
           Thread.sleep(sleepTime);  
           }  
      }  
       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();  
          }  
        }  
      public static void main(String[] args) throws InterruptedException, 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();  
            File imgPath=new File("convert");  
            imgPath.delete();  
            imgPath.mkdirs();  
            String imagePath=imgPath.getAbsolutePath();  
            convertMovietoJPG(mp4Path, imagePath,"jpg",3);  
            System.out.println("Conversion complete. Please find the images at "+imagePath);  
            changeDesktopWallpaper(imagePath,5000);  
      }  
 }  

wallpaperchanger.h
 /* DO NOT EDIT THIS FILE - it is machine generated */  
 #include <jni.h>  
 /* Header for class com_cooltrickshome_DesktopMovieWallpaper */  
 #ifndef _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #define _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #ifdef __cplusplus  
 extern "C" {  
 #endif  
 /*  
  * Class:   com_cooltrickshome_DesktopMovieWallpaper  
  * Method:  changeWallpaper  
  * Signature: (Ljava/lang/String;)I  
  */  
 JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *, jclass, jstring);  
 #ifdef __cplusplus  
 }  
 #endif  
 #endif  

wallpaperchanger.cpp
  #include <iostream>  
  #include <windows.h>  
  #include <fstream>  
  #include <cstdlib>  
  #include <jni.h>  
  #include "wallpaperchanger.h"  
  JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *env, jclass, jstring wallpaper){  
  const char *wallpaper_file = env->GetStringUTFChars(wallpaper, JNI_FALSE);  
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void *)wallpaper_file, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);  
  env->ReleaseStringUTFChars(wallpaper, wallpaper_file);  
  }  

Hope it helps :)

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 :)