35

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 file? Each image can be one of the following:

int JPEG    Encoded formats.
int NV16    YCbCr format, used for video.
int NV21    YCrCb format used for images, which uses the NV21 encoding format.
int RGB_565 RGB format used for pictures encoded as RGB_565.
int YUY2    YCbCr format used for images, which uses YUYV (YUY2) encoding format.
int YV12    Android YUV format: This format is exposed to software decoders and applications.

I can choose the format to whatever I like, as long as I get to create the movie.

public void createMovie(byte[][] images) {
  // and ideas on what to write here?
}

I don't need the actual implementation, just let me know the idea and what external libraries I need (if I need any).

I also need to edit some of the images (the byte stream) before I create the movie (to add some text). How can I do that?

The solution needs to be "Java only"! No external programs, no external commands (but I can use external jars).

Thanks!

user361676
  • 719
  • 2
  • 7
  • 16
  • 4
    +1, for one amazing question. Wish i could answer this one but my cup is totally empty on this. Hope someone will. Regards – nIcE cOw Jan 13 '12 at 20:31
  • From another question [37963625/answer-38024735](https://stackoverflow.com/questions/37963625/how-to-encode-images-into-a-video-file-in-java-through-programming#answer-38024735) you can also use `ffmpeg` to accomplish your goal, and I can vouch that the examples in that answer work, as far as generating a video from an array of images. – iggy12345 May 02 '20 at 21:51

4 Answers4

8

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:

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.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • 1
    Thanks for the answer! Your solution sounds great! But Mencoder seems to be an external program. Did I mention I need this in Java only? :) I didn't.... – user361676 Jan 14 '12 at 06:19
  • Oh, I didn't realize that. If you don't want to use external libraries/programs via system calls, then I don't have any alternate solutions, I'm afraid. – jefflunt Jan 14 '12 at 15:16
  • At least my question about how to write text over the images was answered. Thank you for that! – user361676 Jan 14 '12 at 16:05
5

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

webpat
  • 1,889
  • 16
  • 21
  • This would be an ideal solution, but I couldn't find any tutorial that describes how to write a video file using JMF. That thread has no answers that I can read... – user361676 Jan 14 '12 at 16:04
  • Sorry for the previous link, i didn't see it was unsolved. This sample is from the JMF documentation, should be better. – webpat Jan 15 '12 at 01:51
2

You can try making a gif with this gif encoder.

gpojd
  • 22,558
  • 8
  • 42
  • 71
1

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.

lessthanoptimal
  • 2,722
  • 2
  • 23
  • 25