Java 以编程方式将视频转换为 FLV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/502753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:28:10  来源:igfitidea点击:

Programmatically convert a video to FLV

javavideoflvopencmstranscode

提问by Raibaz

i am currently working on a web application that needs to accept video uploaded by users in any format (.avi, .mov, etc.) and convert them to flv for playing in a flash-based player.

我目前正在开发一个需要接受用户以任何格式(.avi、.mov 等)上传的视频并将它们转换为 flv 以在基于 Flash 的播放器中播放的 Web 应用程序。

Since the site is OpenCms-based, the best solution would be a ready-made plugin for OpenCms that allowed to upload and play videos doing the transcode operation in background, but just a set of Java classes to do the transcode would be great and then i could make the uploading form and playback part on my own.

由于该站点是基于 OpenCms 的,因此最好的解决方案是为 OpenCms 提供一个现成的插件,它允许上传和播放在后台执行转码操作的视频,但只需一组 Java 类来执行转码就很棒,然后我可以自己制作上传表单和播放部分。

采纳答案by Cameron Pope

You basically have two choices if you want to host, transcode and stream flv files (and don't want to buy a video transcoding application): you can call out to FFMpeg/MEncoder or you can use an external Web service. You could also sidestep the problem completely by allowing them to embed YouTube videos on your site.

如果您想托管、转码和流式传输 flv 文件(并且不想购买视频转码应用程序),您基本上有两种选择:您可以调用 FFMpeg/MEncoder,或者您可以使用外部 Web 服务。您还可以通过允许他们在您的网站上嵌入 YouTube 视频来完全回避这个问题。

If you go the 'local FFMpeg route' I would suggest simply using ProcessBuilder and constructing a command-line to execute FFMpeg. That way you get full control over what gets executed, you avoid JNI, which is an absolute nightmare to work with, and you keep OS-specific code out of your app. You can find FFMPeg with all the bells and whistles for pretty much any platform. There's a good chance it's already on your server.

如果您选择“本地 FFMpeg 路线”,我建议您只需使用 ProcessBuilder 并构建一个命令行来执行 FFMpeg。这样您就可以完全控制要执行的内容,避免使用 JNI(这是一个绝对的噩梦),并且您可以将特定于操作系统的代码排除在您的应用程序之外。您几乎可以在任何平台上找到具有所有花里胡哨的 FFMPeg。它很有可能已经在您的服务器上。

The nice thing about the 'Local FFMPeg' route is that you don't have to pay for any extra hosting, and everything is running locally, although your hosting admin might start complaining if you're using a crazy amount of disk and CPU. There are some other StackOverflow questions that talk about some of the gotchas using FFMpegto create flvs that you can actually play in the flash player.

“本地 FFMPeg”路线的好处是您无需为任何额外的托管付费,并且一切都在本地运行,尽管如果您使用大量磁盘和 CPU,您的托管管理员可能会开始抱怨。还有一些其他 StackOverflow 问题讨论了使用 FFMpeg创建可以在 Flash 播放器中实际播放的 flv 的一些问题。

The Web service route is nice because there is less setup involved. I have not used Hey!Watchbut it looks promising. PandaStreamis easy to set up and it works well, plus you get all your videos on S3 with no additional effort.

Web 服务路由很好,因为涉及的设置较少。我没有用过Hey!Watch但它看起来很有希望。PandaStream易于设置且运行良好,而且您无需额外努力即可在 S3 上获取所有视频。

回答by thaBadDawg

There's a great open source tool call FFmpegthat I use to transcode my videos. I use PHP making shell calls to make it come to life, but I can't imagine that it would be too hard to get it to play nice with Java. (Maybe this could be a good starting point for you.)

有一个很棒的开源工具调用FFmpeg,我用它来对我的视频进行转码。我使用 PHP 进行 shell 调用以使其栩栩如生,但我无法想象让它与 Java 一起玩会太难。(也许这对你来说是一个很好的起点。)

I feed my installation 30+ gig batches on a weekly basis and it always comes out as quality material. The only tricky part for me has been getting it compiled to handle a wide variety of video formats. On the bright side, this has provided me with heavy lifting I need.

我每周为我的装置提供 30 多个演出批次,而且它总是作为优质材料出现。对我来说唯一棘手的部分是编译它以处理各种视频格式。从好的方面来说,这为我提供了我需要的繁重工作。

回答by Mark

You could try using an online service like HeyWatchto convert your video. Never used it but they claim

您可以尝试使用HeyWatch等在线服务来转换您的视频。从未使用过,但他们声称

"transparent upload, send videos transparently from your website"

“透明上传,从您的网站透明发送视频”

Not a java solution, but you wouldn't have to worry about what OS your web application is on.

不是 java 解决方案,但您不必担心您的 Web 应用程序在什么操作系统上。

If OS wasn't an issue I agree with the answer theBadDawggave. I don't know of and have had not any luck finding a pure java solution.

如果操作系统不是问题,我同意theBadDawg给出的答案。我不知道也没有找到纯 Java 解决方案的运气。

回答by X-Istence

Encoding files in one format to another takes a lot of development time to get right, which is why there is so little in terms of decoders/encoders that are able to accomplish those feats. The ones that are well known and used the most are ffmpeg and mencoder.

将一种格式的文件编码为另一种格式需要大量的开发时间才能正确处理,这就是为什么能够完成这些壮举的解码器/编码器很少。众所周知和使用最多的是ffmpeg和mencoder。

What you may want to look into is to see if the platform you are running on (Windows/Mac OS X/Other unix) has an underlying set of API calls you can use that is able to decode the files, and re-encode them. Windows has DirectShow and Mac OS X has Quicktime. Not sure if you can access those API's using Java though.

您可能想要查看的是,您正在运行的平台(Windows/Mac OS X/Other unix)是否具有一组可以使用的底层 API 调用,这些调用能够解码文件并重新编码它们. Windows 有 DirectShow,Mac OS X 有 Quicktime。不确定您是否可以使用 Java 访问这些 API。

FFMpeg does have a Java wrapper available: FFMPEG Java, and there is also FOBSwhich has a JNI available for their C++ wrapper around ffmpeg. The last one that I found jFFmpeg, however there are some posts that I found with Google suggesting that the project may not be alive any longer.

FFMpeg 确实有一个可用的 Java 包装器:FFMPEG Java,还有一个FOBS,它有一个 JNI 可用于他们围绕 ffmpeg 的 C++ 包装器。我找到的最后一个jFFmpeg,但是我在 Google 上发现了一些帖子,暗示该项目可能不再存在。

Your best bet would be either mencoder from mplayer and or ffmpeg. Ffmpeg can be installed as a separate binary and then called from other code using the default "shell" commands. If you are however not able to execute commands you may need to look at using an online conversion website like Mark Robinson suggested.

您最好的选择是来自 mplayer 和或 ffmpeg 的 mencoder。Ffmpeg 可以作为单独的二进制文件安装,然后使用默认的“shell”命令从其他代码调用。但是,如果您无法执行命令,则可能需要使用 Mark Robinson 建议的在线转换网站。

回答by Sukumar

FFMpeg is the best when it comes to video transcoding.

在视频转码方面,FFMpeg 是最好的。

You can use java wrappers for ffmpeg - http://fmj-sf.net/ffmpeg-java/getting_started.phphttp://sourceforge.net/projects/jffmpeg/

您可以为 ffmpeg 使用 java 包装器 - http://fmj-sf.net/ffmpeg-java/getting_started.php http://sourceforge.net/projects/jffmpeg/

回答by Esteban Küber

There is an open source library used by MPlayer, called mencoder, wich supports FLV, as well as a lot of other codecs.

MPlayer 使用了一个名为mencoder 的开源库,它支持 FLV 以及许多其他编解码器。

There is a Java GUIyou could see how was made

有一个Java GUI,你可以看到它是如何制作的

This could helptoo.

这也有帮助

I don't seem to be able to find any example notcalled from the console, so it may not be usefull for you. :S

我似乎无法找到任何从控制台调用的示例,因此它可能对您没有用。:S

EditAlso take a look at this question.

编辑也看看这个问题

回答by Baishampayan Ghose

This can be slightly tangential, but I have found Panda Streamto be a very useful solution to all kinds of video encoding problems.

这可能有点不切实际,但我发现Panda Stream是解决各种视频编码问题的非常有用的解决方案。

All you have to do is to upload the video file to an Amazon EC2 instance running Panda and it will encode the video to your desired formats and quality and will issue a callback to your application server with the details when it's done. You can then use the bundled Flash Video player or your own player to play the videos on your site.

您所要做的就是将视频文件上传到运行 Panda 的 Amazon EC2 实例,它会将视频编码为您所需的格式和质量,并在完成后向您的应用程序服务器发出回调并提供详细信息。然后,您可以使用捆绑的 Flash 视频播放器或您自己的播放器在您的网站上播放视频。

It's a very scalable (thanks to Amazon EC2 & S3), cost-effective and customisable solution compared to rolling your own.

与滚动您自己的解决方案相比,这是一个非常可扩展(感谢 Amazon EC2 和 S3)、经济高效且可定制的解决方案。

Highly recommended.

强烈推荐。

Update:

更新:

The architecture of Panda is as follows:

Panda 的架构如下:

Architecture of Panda Stream
(source: pandastream.com)

Panda Stream 的架构
(来源:pandastream.com

  1. Page displays Panda's upload form in an iframe or popup
  2. Video upload with AJAX progress bar
  3. API callback when encoding is complete
  4. Video streamed to user
  1. 页面在 iframe 或 popup 中显示 Panda 的上传表单
  2. 使用 AJAX 进度条上传视频
  3. 编码完成时的 API 回调
  4. 视频流式传输给用户

回答by Art Clarke

You can encode video in Java using Xuggler, which is a Java API that natively uses FFmpeg's C code behind the scenes.

您可以使用Xuggler在 Java 中对视频进行编码,Xuggler是一种 Java API,在后台原生使用 FFmpeg 的 C 代码。

回答by Robert McIntyre

If you want to do it with java, you can do it very easily using Xuggle.

如果你想用java来做,你可以很容易地用Xuggle来做。

They have a great website explaining how to do everything

他们有一个很棒的网站,解释了如何做所有事情

the documentation is here: http://build.xuggle.com/view/Stable/job/xuggler_jdk5_stable/javadoc/java/api/index.html

文档在这里:http: //build.xuggle.com/view/Stable/job/xuggler_jdk5_stable/javadoc/java/api/index.html

and an excellent tutorial telling you how to do what you want is here: http: //blog.xuggle.com/2009/06/05/introduction-to-xuggler-mediatools/

一个很棒的教程告诉你如何做你想做的事:http://blog.xuggle.com/2009/06/05/introduction-to-xuggler-mediatools/

They provide an easy way to do what you want in the first tutorial, which is simple trans-coding.

它们提供了一种简单的方法来完成第一个教程中的操作,即简单的转码。

I've found that it works alright for encoding to flv. What it does behind the scenes is use ffmpeg, so anything that will trip up ffmpeg will also fail with xuggle.

我发现它可以很好地编码为 flv。它在幕后所做的是使用 ffmpeg,所以任何会绊倒 ffmpeg 的东西也会因 xuggle 而失败。

The relevant sample java code is:

相关的示例java代码是:

 // create a media reader
 IMediaReader reader = ToolFactory.makeReader("videofile.flv");

 // add a viewer to the reader, to see the decoded media
 reader.addListener(ToolFactory.makeWriter("output.mov", reader));

 // read and decode packets from the source file and
 // and dispatch decoded audio and video to the writer
 while (reader.readPacket() == null)
   ;

Which I got from http ://wiki.xuggle.com/MediaTool_Introduction

我从 http://wiki.xuggle.com/MediaTool_Introduction 得到的

If you want some fully working clojure code... here it is :)

如果你想要一些完整的 clojure 代码......这里是:)

(import '(com.xuggle.mediatool ToolFactory))
(import '(com.xuggle.mediatool IMediaDebugListener IMediaDebugListener$Event))

    (defn readerRecurse
      "calls .readPacket until there's nothing left to do2"
      [reader]
      (if (not (nil? (.readPacket reader))) ; here .readPacket actually does the processing as a side-effect.
        true                                   ; it returns null when it has MORE ro process, and signals an error when done... 
        (recur reader)))


    (defn convert
      "takes video and converts it to a new type of video"
      [videoInput videoOutput]
      (let [reader (ToolFactory/makeReader videoInput)]
        (doto reader
          (.addListener (ToolFactory/makeWriter videoOutput reader))
          (.addListener (ToolFactory/makeDebugListener (into-array [IMediaDebugListener$Event/META_DATA]))))
        (readerRecurse reader)))

now all you have to do is something like:

现在你所要做的就是:

(convert "/path/to/some_file.stupid_extention" "/path/to/awesome.flv")

and you're done!

你就完成了!

回答by dctanner

You might also be interested in hearing that we've now released Panda as a hosted service as well, which makes the setup and scaling easier :)

您可能还有兴趣听说我们现在也发布了 Panda 作为托管服务,这使得设置和扩展更容易:)

http://pandastream.com

http://pandastream.com