Java: How do I create a movie from an array of images?

前端 未结 4 785
野趣味
野趣味 2020-11-27 17:11

I basically have an matrix of bytes. Each row (meaning byte[]) represents an image. How do I create a movie out of that (any format - avi, mpeg, whatever), and save it as a

相关标签:
4条回答
  • 2020-11-27 17:51

    I wrote an MJPEG reader and writer for playing videos inside of Java applets. MJPEG is not the most advanced video format but it is very easy to manipulate. The code is part of my computer vision library BoofCV, but you could just rip out this one class for your own purposes.

    1. Download this file: CreateMJpeg.java
    2. Look at main function. Where it reads in jpeg images put your byte[] data, but you will need to convert it to jpeg's first.
      • You can convert it into a jpeg using the standard java library
    3. Run modified code and enjoy your movie

    Sorry its not in a more user friendly format, but at least you don't need to mess with JNI like some other solutions.

    0 讨论(0)
  • 2020-11-27 17:52

    Have you heard about JMF (Java Media Framework), from the sample you can find this example : Generating a Movie File from a List of (JPEG) Images

    0 讨论(0)
  • 2020-11-27 18:03

    The solution seems to be to use Mencoder (or at least, that seems to be a semi-popular choice).

    Here's a link that specifically addresses images-to-movies capabilities in Mencoder.

    As for rendering text onto the frames before encoding them as part of the video, you can use Java2D's image manipulation libraries to simply draw text on top of the images beforehand For example:

    • Load up the images into BufferedImage objects via the ImageIO library's .read method
    • Use Graphics2D's .drawString method to render the text

    That's one way to do it, and this FAQ should get you started in that direction with Java2D, font rendering, etc., and offer pointers to further resources.

    The ImageIO library also allows you to read/write a number of image formats, effectively allowing you to transcode images from, say, .jpg -> BufferedImage -> .png, or any which way you need to do it, if you want to store the image files temporarily during the conversion process, and/or convert all the images to a single format when importing them for the conversion project, etc.

    Depending on how many output formats you want to support, you'll probably do something like

    public void createMovie(BufferedImage[] frames, String destinationFormat)
    

    ...where "destinationFormat" is something like "m4v", "mpeg2", "h.264", "gif", etc.

    0 讨论(0)
  • 2020-11-27 18:16

    You can try making a gif with this gif encoder.

    0 讨论(0)
提交回复
热议问题