Search code examples
javayoutubejcodec

I was trying to convert images to a video using jcodec


I was trying to convert a set of images into video using Jcodec, Saw a video on youtube that was running correctly. I did everything they showed. But the code didn't work. plz, help. I'm posting the link to the video, my code, and the errors.

video link: https://www.youtube.com/watch?v=NYhnqE3E4CI

java code:

package imagetovideoencoder;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jcodec.api.awt.AWTSequenceEncoder;
import org.jcodec.api.SequenceEncoder;
import org.jcodec.common.io.NIOUtils;
import org.jcodec.common.io.SeekableByteChannel;
import org.jcodec.scale.AWTUtil;
import org.jcodec.common.*;

/**
 *
 * @author dell
 */
public class ImagetoVideoEncoder {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        AWTSequenceEncoder enc = AWTSequenceEncoder.createSequenceEncoder(new File("C:\\Users\\dell\\Desktop\\Downloads from seenium\\test.mp4"));
        enc.getEncoder().setKeyInterval(25);
        int framestoEncode = 100;


        for(int i=1;i<=framestoEncode/2;++i)
        {
            BufferedImage image = ImageIO.read(new File("C:/Users/dell/Desktop/Downloads from seenium/0f1f4905877e9a5e92b069c966059cce.jpg"));
            enc.encodeImage(image);
        }

        for(int i=1;i<=framestoEncode/2;++i)
        {
            BufferedImage image = ImageIO.read(new File("C:/Users/dell/Desktop/Downloads from seenium/54cae4238a5cf_-_american-muscle-facts-05-0312-xln.jpg"));
            enc.encodeImage(image);
        }

        enc.finish();
    }

}

error: run:

Exception in thread "main" java.lang.NoSuchMethodError: org.jcodec.api.SequenceEncoder.(Lorg/jcodec/common/io/SeekableByteChannel;)V at org.jcodec.api.awt.AWTSequenceEncoder.(AWTSequenceEncoder.java:27) at org.jcodec.api.awt.AWTSequenceEncoder.createSequenceEncoder(AWTSequenceEncoder.java:23) at imagetovideoencoder.ImagetoVideoEncoder.main(ImagetoVideoEncoder.java:30) C:\Users\dell\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 9 seconds)


Solution

  • The above code worked. A proper method to encode images into video. It was the problem of incompatible JAR files as suggested by Jens in one of the answers. So I deleted the added JAR files and repeated the steps shown in the video. It worked. Only thing I noticed was that the images needed to be of the same size. So I just ran a small function to resize the images to same size. It works fine.