通过 servlet 使用 MEncoder 进行视频编码
我正在开发一个在服务器上进行视频编码的应用程序,并且在使用 MEncoder 编码视频时遇到了问题。当通过命令行运行时,该解码器无法正常工作
Runtime.getRuntime().exec(“D:\mencoder\mnc\mencoder.exe video1.avi -o outvideo1.flv -of lavf -oac mp3lame -lameopts abr:br=64 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=300:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=320:240,harddup -quiet”) ;
解码器启动并使用我的参数在 Windows 控制台中工作,但是当它从 servlet 运行时,它只是挂在进程列表中,并且在 Web 服务器之前不执行任何操作已停止。当尝试从简单的 java 应用程序使用解码器时,它运行正确。感谢您的帮助。
I was developing an application for video encoding on the server and got a problem with encoding video with MEncoder. This decoder doesn't work correctly when runned by a command line with
Runtime.getRuntime().exec(“D:\mencoder\mnc\mencoder.exe video1.avi -o outvideo1.flv -of lavf -oac mp3lame -lameopts abr:br=64 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=300:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=320:240,harddup -quiet”) ;
The decoder launches and works in windows console with my parameters, but when it's run from a servlet it just hangs in process list and doesn't do anything before the web-server is stopped. When trying to use decoder from a simple java applcation, it runs correctly. Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这绝对是一个糟糕的方法。一般来说,mencoder 花费的时间比用户愿意等待的时间要长得多。此外,如果 mencoder 泄漏内存或崩溃,您将面临整个 jvm 崩溃的风险。您也无法控制生成多少个进程。更好的解决方案是使用 jms 队列或插入的数据库。然后,您还有另一个批处理作业来拾取这些视频并处理这些视频。如果您使用数据库方法,您可以随着事情的进展更新数据库,并使用户了解最新的进度统计信息。一般来说,将这种运行时执行保留在 Web 应用程序容器之外。
This is definitely a bad way to do this. In general, mencoder will take much longer than users will be willing to wait. Also, if mencoder leaks memory or crashes you risk taking down the whole jvm. You also have no control over how many of these processes get spawned. A better solution is to have a jms queue, or a database that you insert into. Then you have another batch job that picks those up and processes those videos. If you use the database approach, you can update the database as things progress, and keep the user up to date with progress stats. In general keep this kind of runtime execution out of your web application container.
它不起作用的原因是 MPlayer 在终端中输出大量内容,一旦输出达到 4096 字节(Linux 上的管道缓冲区限制),进程就会挂起在下一个 printf 上,直到在另一端读取一些数据为止的管道。在您的情况下,它会永远挂起,因为您没有读取标准输出和错误流。
为了解决这个问题,您可以在启动进程后立即关闭这些流:
The reason it doesn't work is because MPlayer outputs a lot of stuff in the terminal and as soon as it's output reaches 4096 bytes ( pipe buffer limit on Linux ) the process hangs on the next printf until some data is read on the other side of the pipe. And in your case it hangs forever as you are not reading standart output and error streams.
In order to get around this you can just close these streams as soon as you launch your process: