在Xuggler中无法从BGR24重新采样到YUV420P

时间:2014-07-05 03:44:17

标签: image video encoding xuggler

我正在尝试将sequantial图像转换为视频文件。 但是,这个源在编码视频时会出错。

public class Image2Video {

private static final double FRAME_RATE = 20;

private static final int SECONDS_TO_RUN_FOR = 20;

private static final String outputFilename = "C:/image/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:/image");
    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 = 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);

    }
    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+".jpeg";
        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;

    }

}

}

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您的图像宽度和高度不完全相同。 尝试使用类似尺寸的图像运行程序,您可以看到问题消失。

您可以将图像转换为相同的宽度/高度,然后再将其转换为此程序。