How can I turn a series of images into a video using JCodec?

后端 未结 2 612
情书的邮戳
情书的邮戳 2021-01-07 07:06

I\'m trying to use JCodec to turn a series of images into a video inside of a Java SE desktop application. The few methods I\'ve tried all resulted in a video that Windows

2条回答
  •  孤城傲影
    2021-01-07 07:53

    I had a lot of issues with jcodec's RgbToYuv420() transform.
    I used my own function to transform my RGB BufferedImage to Yuv420.
    One issue I had was that RgbToYuv420 averages the computed upix and vpix values, and it was causing the colors in my mp4 to be all wonky.

    // make a YUV420J out of BufferedImage pixels
    private Picture makeFrame(BufferedImage bi, ColorSpace cs)
    {   
        DataBuffer imgdata = bi.getRaster().getDataBuffer();
        int[] ypix = new int[imgdata.getSize()];
        int[] upix = new int[ imgdata.getSize() >> 2 ];
        int[] vpix = new int[ imgdata.getSize() >> 2 ];
        int ipx = 0, uvoff = 0;
    
        for (int h = 0; h < bi.getHeight(); h++) {
            for (int w = 0; w < bi.getWidth();  w++) {
                int elem = imgdata.getElem(ipx);
                int r = 0x0ff & (elem >>> 16);
                int g = 0x0ff & (elem >>> 8);
                int b = 0x0ff & elem;
                ypix[ipx] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
                if ((0 != w % 2) && (0 != h % 2)) {
                    upix[uvoff] = (( -38 * r + -74 * g + 112 * b) >> 8) + 128;
                    vpix[uvoff] = (( 112 * r + -94 * g + -18 * b) >> 8) + 128;
                    uvoff++;
                }
                ipx++;
            }
        }
        int[][] pix = { ypix, upix, vpix, null };
        // old API
        // return new Picture(bi.getWidth(), bi.getHeight(), pix, ColorSpace.YUV420J);
        return Picture.createPicture(bi.getWidth(), bi.getHeight(), pix, ColorSpace.YUV420J);
    }
    

提交回复
热议问题