有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何通过编程将图像编码到Java视频文件中?

我正在尝试使用以下方法将一些相同分辨率的图像编码到视频文件中:

jCodec

  • jcodec。。示例description

    但是它非常耗时,而且不是一个合适的工具来编码大量的图像,而且它会快速延长时间

FFMPEG

  • FFMPEG。。示例description

    但是ffmpeg只能从图像文件创建视频。图像需要在物理系统上创建

我听说Xuggler它的API可以在java程序中用于创建视频文件,但由于它的网站似乎已损坏。我无法尝试

有人知道如何将java格式的图像编码成视频文件吗?请帮助

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    使用命令行,可以通过多种方式将图像转换为视频。可以在java中使用这些命令进行保存。您可以从以下链接获取这些命令:

    1. Using ffmpeg to convert a set of images into a video
    2. Create a video slideshow from images

    我正在分享一段代码片段来解决这个问题:

    从HTML5画布保存png图像的代码

    Base64 decoder = new Base64();
    byte[] pic = decoder.decodeBase64(request.getParameter("pic"));
    String frameCount = request.getParameter("frame");
    InputStream in = new ByteArrayInputStream(pic);
    BufferedImage bImageFromConvert = ImageIO.read(in);
    String outdir = "output\\"+frameCount;
    //Random rand = new Random();
    File file = new File(outdir);
    if(file.isFile()){
        if(file.delete()){
            File writefile = new File(outdir);
            ImageIO.write(bImageFromConvert, "png", file);
        }
    }
    

    从视频创建图像的代码

    String filePath = "D:\\temp\\some.mpg";
    String outdir = "output";
    File file = new File(outdir);
    file.mkdirs();
    Map<String, String> m = System.getenv();
    
    /*
     * String command[] =
     * {"D:\\ffmpeg-win32-static\\bin\\ffmpeg","-i",filePath
     * ,"-r 30","-f","image2",outdir,"\\user%03d.jpg"};
     * 
     * ProcessBuilder pb = new ProcessBuilder(command); pb.start();
     */
    String commands = "D:\\ffmpeg-win32-static\\bin\\ffmpeg -i " + filePath
            + " -r 30  -f image2 " + outdir + "\\image%5d.png";
    Process p = Runtime.getRuntime().exec(commands);
    

    从图像创建视频的代码

    String filePath = "output";
    File fileP = new File(filePath);
    String commands = "D:\\ffmpeg-win32-static\\bin\\ffmpeg -f image2 -i "
            + fileP + "\\image%5d.png " + fileP + "\\video.mp4";
    System.out.println(commands);
    Runtime.getRuntime().exec(commands);
    System.out.println(fileP.getAbsolutePath());
    

    功劳归于yashprit


    Android开发者的另一种方法:

    1. 在Android中创建一个临时文件夹
    2. 将图像复制到新文件夹中
    3. 首先,按照数字顺序重命名图片。对于 例如,img1。jpg,img2。jpg,img3。jpg,。。。然后你可以跑:
    4. 以ffmpeg-f image2-i img%d.jpg的编程方式运行此程序 /tmp/a.mpg以编程方式运行

    使用以下代码:

    void convertImg_to_vid()
    {
        Process chperm;
        try {
            chperm=Runtime.getRuntime().exec("su");
              DataOutputStream os = 
                  new DataOutputStream(chperm.getOutputStream());
    
                  os.writeBytes("ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg\n");
                  os.flush();
    
                  chperm.waitFor();
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    资源链接:

    1. Create a Video file from images using ffmpeg