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 :
Program:
Main method:
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:
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:
Full Program:
Hope it helps :)
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 :)