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

1 comment:

  1. Illustrators, artists, architects, engineers and a bunch of different professionals are at all times in want of the flexibility to transform bitmap photos to vector photos a the vector format permits printing, scaling or resizing of photos with out lack of decision. If you want to learn more about this topic please visit onlineconvertfree

    ReplyDelete