Video Creation from a set of images with xuggler

陌路散爱 提交于 2019-12-23 03:19:17

问题


I have been looking for a solution everywhere! On this website and on others. I have found some interesting things, but they didn't solve my problem. I will explain it.

I have one video, I grad each frame of it with xuggler. When I get all the frames I edit all of them with a color algorithm. Also, I store the audio in an mp3 file.

Now I need to create a video from all the frames, this video, of course, should have the same characteristics as frame rate e duration. After that I have to merge the audio.

I have done the first part, but I don't know how to create a video with the same characteristics. I am following this code:

http://www.javacodegeeks.com/2011/02/xuggler-tutorial-frames-capture-video.html

Can't encode video with Xuggler

But it takes the snapshot and it uses a strange loop:

for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++)

I can't figure out how to set the right characteristic. It should be easy because I know everything about the video! size, frame rate and number of frame.

My code:

public static void main(String[] args) throws IOException {

    final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);
    writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, 
               720, 304);
    long nextFrameTime = 0;
    final long frameRate =25/1000;
    long startTime = System.nanoTime();
    while (indexVideo<1597) {
        BufferedImage videoImage = null;
        try {
            videoImage = getVideoImage();
        } catch (AWTException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        writer.encodeVideo(0, videoImage,nextFrameTime, 
                TimeUnit.MILLISECONDS);
        nextFrameTime += frameRate;

}
    writer.close();
}

private static BufferedImage getVideoImage() throws IOException, AWTException {

     File imgLoc = new File("D:/Gianfranco/Sample/"+indexVideo+".png");
     BufferedImage img;
    img = ImageIO.read(imgLoc);
    System.out.println(imgLoc.getName());
    indexVideo++;
    return img;

}

Ca anyone help me out?


回答1:


A quick glance at your code shows me that you aren't setting the frame rate. I don't know what version of xuggler you are using, but for me I set the frame rate when adding video stream:

writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, IRational.make(<frames>/<per second>), 720, 304);

Now another (probably more appropriate) approach would be to create your IMediaWriter by opening your original video.

    IMediaReader reader = ToolFactory.makeReader("inputFile.mp4");
    IMediaWriter writer = ToolFactory.makeWriter("outputFile.mp4", reader);

Or maybe you make it by grabbing the container of the first, then grabbing the format. I've only done this for streaming data and not archived data.




回答2:


package com.Raamji.Work;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;

public class VideoGenerator {

    private static final double FRAME_RATE = 20;

    private static final int SECONDS_TO_RUN_FOR = 20;

    private static final String outputFilename = "C:/myVideo.mp4";

    private static Dimension screenBounds;

    private static Map<String, File> imageMap = new HashMap<String, File>();

    public static void main(String[] args) {

        final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);

        screenBounds = Toolkit.getDefaultToolkit().getScreenSize();

        writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4,screenBounds.width / 2, screenBounds.height / 2);

        File folder = new File("C:/Users/arsingh/Desktop/tempo/video");
        File[] listOfFiles = folder.listFiles();

        int indexVal = 0;
        for (File file : listOfFiles) {
            if (file.isFile()) {
                indexVal++;
                System.out.println("file.getName() :"+file.getName());
                imageMap.put(file.getName(), file);
            }
        }

        //for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) {
        for (int index = 1; index <= listOfFiles.length; index++) {
            BufferedImage screen = getImage(index);
            BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR);
            writer.encodeVideo(0, bgrScreen, 300*index, TimeUnit.MILLISECONDS);

        }
        // tell the writer to close and write the trailer if needed
        writer.close();
        System.out.println("Video Created");

    }

    public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {
        BufferedImage image;
        if (sourceImage.getType() == targetType) {
            image = sourceImage;
        }
        else {
            image = new BufferedImage(sourceImage.getWidth(),
            sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }
        return image;
    }

    private static BufferedImage getImage(int index) {

        try {
            String fileName=index+".jpg";
            System.out.println("fileName :" + fileName);
            File img = imageMap.get(fileName);

            BufferedImage in=null;
            if (img != null) {
                System.out.println("img :"+img.getName());
                in = ImageIO.read(img);
            }else
            {
                System.out.println("++++++++++++++++++++++++++++++++++++++index :" + index);
                img = imageMap.get(1);
                in = ImageIO.read(img);
            }
            return in;

        }

        catch (Exception e) {

            e.printStackTrace();

            return null;

        }

    }

}



回答3:


try this for frame rate:

final long frameRate =DEFAULT_TIME_UNIT.convert(25, MILLISECONDS);


来源:https://stackoverflow.com/questions/9175543/video-creation-from-a-set-of-images-with-xuggler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!