1

我一直在到处寻找解决方案!在本网站和其他网站上。我发现了一些有趣的东西,但它们并没有解决我的问题。我会解释的。

我有一个视频,我用 xuggler 对它的每一帧进行分级。当我得到所有帧时,我会使用颜色算法对它​​们进行编辑。另外,我将音频存储在 mp3 文件中。

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

我已经完成了第一部分,但我不知道如何创建具有相同特征的视频。我正在关注这段代码:

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

无法使用 Xuggler 对视频进行编码

但它拍摄快照并使用了一个奇怪的循环:

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

我不知道如何设置正确的特性。这应该很容易,因为我对视频了如指掌!大小、帧率和帧数。

我的代码:

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;

}

有人能帮帮我吗?

4

3 回答 3

1

快速浏览一下您的代码,我发现您没有设置帧速率。我不知道您使用的是什么版本的 xuggler,但对我来说,我在添加视频流时设置了帧速率:

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

现在另一种(可能更合适)的方法是通过打开您的原始视频来创建您的 IMediaWriter。

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

或者,也许您可​​以通过抓取第一个容器,然后抓取格式来实现。我只对流数据而不是存档数据执行此操作。

于 2013-10-22T16:42:12.420 回答
1
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;

        }

    }

}
于 2014-03-26T05:01:33.773 回答
0

试试这个帧速率:

final long frameRate =DEFAULT_TIME_UNIT.convert(25, MILLISECONDS);
于 2013-04-02T13:06:48.400 回答