简体   繁体   中英

images to video Xuggler

I use xuggler but not understand that.I have ArrayList and I want make from this images, video. But when I compile this code first 5 second on video image not change and last 5 second on video not at all. How fix it.

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

Edit your question, its a puzzle in itself. Dint understand what you meant by "first 5 second on video image not change and last 5 second on video not at all."

I hope you would have found your answer, but this might help someone else, move start time initialisation after the calling getImagesFromDirectory

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM