使用xuggler从一组图像创建视频 [英] Video Creation from a set of images with xuggler

查看:186
本文介绍了使用xuggler从一组图像创建视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案!在这个网站和其他人。
我发现了一些有趣的事情,但他们没有解决我的问题。我会解释一下。



我有一个视频,我用xuggler研究每一帧。当我得到所有的帧时,我用颜色算法编辑它们。此外,我将音频存储在mp3文件中。



现在我需要从所有框架创建一个视频,当然,这个视频应该具有相同的特性帧速e持续时间。之后,我必须合并音频。



我已经做了第一部分,但是我不知道如何创建一个具有相同特征的视频。我遵循这个代码:



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



无法用Xuggler编码视频



但它需要快照,它使用一个奇怪的循环:

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

我不知道如何设置正确的特征。这应该很容易,因为我知道有关视频的一切!大小,帧率和帧数。



我的代码:

  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自动生成的catch块
e1.printStackTrace();
}
writer.encodeVideo(0,videoImage,nextFrameTime,
TimeUnit.MILLISECONDS);
nextFrameTime + = frameRate;

}
writer.close();


$ b 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;

}

任何人帮助我吗?

解决方案

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

文件夹=新文件(C:/ Users / arsingh / Desktop / tempo / video);
文件[] listOfFiles = folder.listFiles();

int indexVal = 0;
(文件文件: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);

}
//如果需要,告诉作者关闭并编写预告片
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);
}
返回图像;
}

private static BufferedImage getImage(int index){

try {
String fileName = index +。
System.out.println(fileName:+ fileName);
文件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);
}
返回;

}

catch(异常e){

e.printStackTrace();

返回null;

}

}

}


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?

解决方案

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;

        }

    }

}

这篇关于使用xuggler从一组图像创建视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆