使用 xuggler 从一组图像创建视频

发布于 2025-01-03 00:42:43 字数 1904 浏览 1 评论 0原文

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

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

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

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

http://www.javacodegeeks.com/2011/02/xuggler-tutorial-frames-capture-video.html" 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;

}

有人能帮我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2025-01-10 00:42:43

快速浏览一下您的代码,我发现您没有设置帧速率。我不知道您使用的是哪个版本的 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);

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

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.

久而酒知 2025-01-10 00:42:43
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;

        }

    }

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

        }

    }

}
知足的幸福 2025-01-10 00:42:43

试试这个帧率:

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

try this for frame rate:

final long frameRate =DEFAULT_TIME_UNIT.convert(25, MILLISECONDS);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文