图像到视频Xuggler

时间:2013-05-27 10:37:14

标签: java xuggler xuggle

我使用xuggler但不明白。我有ArrayList,我想从这个图像,视频制作。但是当我在视频图像上编译此代码时,前5秒钟没有改变,而视频上的5秒钟根本没有。如何解决它。

public class ScreenRecordingExample {

    private static int FPS = 1000 / 25;
    private static final String outputFilename = "c:/mydesktop.mp4";
    private static Dimension screenBounds;

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

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

        long startTime = System.nanoTime();

        List<BufferedImage> BIList = getImagesFromDirectory(null);

        for (int index = 0; index < BIList.size(); index++) {

            BufferedImage bgrScreen = convertToType(BIList.get(index),BufferedImage.TYPE_3BYTE_BGR);
            writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime,
                    TimeUnit.NANOSECONDS);

            try {
                Thread.sleep((long) (1000 / 15));
            } catch (InterruptedException e) {}
        }

        writer.close();

    }

    public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {

        BufferedImage image;

        // if the source image is already the target type, return the source image
        if (sourceImage.getType() == targetType) {
            image = sourceImage;
        } // otherwise create a new image of the target type and draw the new image
        else {
            image = new BufferedImage(sourceImage.getWidth(),
                    sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }

        return image;

    }

    private static List<BufferedImage> getImagesFromDirectory(File directory) throws IOException {


        File dir = new File("C:\\fotos\\");
        final String[] EXTENSIONS = new String[]{"gif", "png", "bmp"};

        final List<BufferedImage> imageList = new ArrayList<BufferedImage>();

        FilenameFilter IMAGE_FILTER = new FilenameFilter() {

            @Override
            public boolean accept(final File dir, final String name) {
                for (final String ext : EXTENSIONS) {
                    if (name.endsWith("." + ext)) {
                        return (true);
                    }
                }
                return (false);
            }
        };
        if (dir.isDirectory()) { // make sure it's a directory
            for (final File f : dir.listFiles(IMAGE_FILTER)) {
                BufferedImage img = null;
                img = ImageIO.read(f);
                imageList.add(img);
            }
        }
        return imageList;
    }

2 个答案:

答案 0 :(得分:1)

编辑你的问题,这本身就是一个难题。不明白你的意思是“视频图像的前5秒没有变化,视频上的最后5秒完全没有变化”。

答案 1 :(得分:0)

我希望你能找到答案,但这可能有助于其他人,在调用getImagesFromDirectory之后启动开始时间初始化

long startTime = System.nanoTime();
List<BufferedImage> BIList = getImagesFromDirectory(null);

List<BufferedImage> BIList = getImagesFromDirectory(null);
long startTime = System.nanoTime();*