Java:如何从图像数组创建电影?

发布于 2024-12-27 02:02:00 字数 809 浏览 5 评论 0原文

我基本上有一个字节矩阵。每行(即 byte[])代表一个图像。如何创建一个电影(任何格式 - avi、mpeg 等等),并将其保存为文件? 每个图像可以是以下之一:

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.

只要我能够创建电影,我就可以选择我喜欢的格式。

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

我不需要实际的实现,只需让我知道这个想法以及我需要哪些外部库(如果我需要的话)。

在创建电影(添加一些文本)之前,我还需要编辑一些图像(字节流)。我怎样才能做到这一点?

解决方案必须是“仅限 Java”!没有外部程序,没有外部命令(但我可以使用外部罐子)。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌辣 2025-01-03 02:02:01

您听说过 JMF (Java Media Framework),从示例中您可以找到以下示例:< a href="http://www.oracle.com/technetwork/java/javase/documentation/jpegimagestomovie-176885.html" rel="noreferrer">从 (JPEG) 列表生成电影文件图片

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

温柔女人霸气范 2025-01-03 02:02:01

您可以尝试使用此 gif 编码器 制作 gif。

You can try making a gif with this gif encoder.

轮廓§ 2025-01-03 02:02:01

我编写了一个 MJPEG 阅读器和编写器,用于在 Java 小程序中播放视频。 MJPEG 不是最先进的视频格式,但它非常容易操作。该代码是我的计算机视觉库 BoofCV 的一部分,但您可以出于自己的目的撕掉这一类。

  1. 下载此文件: CreateMJpeg.java
  2. 看main函数。它在 jpeg 图像中读取的位置放置您的 byte[] 数据,但您需要首先将其转换为 jpeg 的。
    • 您可以使用标准 java 库将其转换为 jpeg
  3. 运行修改后的代码并欣赏您的电影

抱歉,它不是一种更用户友好的格式,但至少您不需要像其他解决方案那样弄乱 JNI。

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.

您的好友蓝忘机已上羡 2025-01-03 02:02:00

解决方案似乎是使用 Mencoder (或者至少,看起来成为半受欢迎的选择)。

这是专门针对图像到电影 Mencoder 中的功能。

至于在将文本编码为视频的一部分之前将文本渲染到帧上,您可以使用 Java2D 的图像操作库预先在图像顶部绘制文本例如:

这是一种方法,此常见问题解答应该可以帮助您朝这个方向开始Java2D、字体渲染等,并提供更多资源的指针。

ImageIO 库还允许您读取/写入多种图像格式,从而有效地允许您对图像进行转码,例如 .jpg -> .缓冲图像-> .png,或者您需要的任何方式,如果您想在转换过程中临时存储图像文件,和/或在为转换项目导入图像时将所有图像转换为单一格式等。

取决于您想要支持多少种输出格式,您可能会执行类似

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

...其中“destinationFormat”类似于“m4v”、“mpeg2”、“h.264”、“gif”等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文