Rails 4 - 如何提供视频? [英] Rails 4 - How to serve video?

查看:380
本文介绍了Rails 4 - 如何提供视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 4作为我的网络应用程序而我正在使用VideoJS来显示它,例如:

I'm using Rails 4 for my web application and I'm using VideoJS to display it, like:

<video id="video1" class="video-js" controls preload="auto" width="640" height="264" poster="{{pCtrl.video.poster}}"  src="<%= @video.file %>">
        <source src="<%= @video.file %>" type="video/<%= @video.file.file.filename.last(3) %>">
</video>

我总是从VideoJS收到此错误:

I was always getting this error from VideoJS:

VIDEOJS: ERROR: (CODE:3 MEDIA_ERR_DECODE) The video playback was aborted due to a corruption problem or because the video used features your browser did not support.

当我打开Chrome的开发工具时,我看到视频(340Mb .mp4文件)已下载状态为206(部分内容)且仅为2.8 Mb。

When I open Chrome's developper tools, I see that the video (the 340Mb .mp4 file) is downloaded with status 206 (Partial Content) and only 2.8 Mb.

我认为这是VideoJS的问题,但后来我尝试直接访问,例如 http:// localhost:3000 / uploads / video / file / glrbfmso1449450792.mp4

I thought it was a problem with VideoJS, but then I tried to access directly, like "http://localhost:3000/uploads/video/file/glrbfmso1449450792.mp4"

该视频将播放几秒钟然后冻结,除非我刷新页面否则它将无法播放。我认为它只能播放2.8 Mb。因此我认为它与WEBrick有关。

The video would play for a few seconds and then it freezes and it won't play unless I refresh the page. I think it only plays the 2.8 Mb loaded. So I believe it has something to do with WEBrick.

问题不在于视频文件,因为它在VLC上正常播放以及我在使用Glassfish时(编程)在Java)。

The problem is not with the video file, as it plays normally on VLC and when I'm using Glassfish (programming in Java).

我的问题是:我应该在Rails或WEBrick中配置什么?我应该编辑哪些设置?或者这是我经历的异常?

My question, then, is: Is there something I should configure in Rails or WEBrick? Is there any settings that I should edit? Or is this what I'm going through unusual?

推荐答案

不要使用rails来提供视频文件。虽然像独角兽这样的服务器支持流媒体视频文件,但有几个原因可能是个坏主意。这里有很多不同的联系。 rails应用程序通常应该花费不超过200毫秒来返回响应。当您的流式视频时,连接保持打开的时间并不常见。

Don't use rails to serve video files. Although servers such as unicorn support streaming video files there are a few reasons why it's a bad idea. There are vastly different connections at play here. A rails app generally should take no longer than ~200ms to return a response. When your streaming video it would not be uncommon for the connection to stay open for much longer.

为什么重要?考虑一下rails web服务器设计用于运行ruby代码并生成基于文本的输出。为了尽快做到这一点,非常聪明的人在编写诸如puma和raptor等服务器时会做出一些决定。在猛禽的情况下,我知道他们从不在运行时分配新对象,因为它太昂贵了。相反,它们维护一个对象池来处理传入的请求。请求将被移交,然后池中的对象被释放。这对于短期请求很好,但是如果你的池中只有5个对象怎么办?只要有5个人在您的网站上观看视频,您的服务器就会很快耗尽连接。在发生这种情况时,您的网站将覆盖世界其他地区。有关猛禽如何工作的详细信息,请参阅 http://www.rubyraptor.org/how-we-made-raptor-up-to-4x-faster-than-unicorn-and-up- to-2x-fast-than-puma-torquebox /

Why does that matter? Consider for a moment that a rails web sever designed to run ruby code and produce text based output. To do this as quickly as possible very smart people make a number of decisions when writing servers such as puma and raptor. In the case of raptor I know they never allocate a new object during runtime as it's too expensive. They instead maintain a pool of objects for handling incoming requests. The request will be handed and then the objects in the pool released. That's fine for short lived requests but what if you only have 5 objects in your pool? Your sever will very quickly run out of connections as soon as you have 5 people watching a video on your site. While this is happening your site is down to the rest of the world. For more details on how raptor works see http://www.rubyraptor.org/how-we-made-raptor-up-to-4x-faster-than-unicorn-and-up-to-2x-faster-than-puma-torquebox/

另一个原因是内存使用。与nginx相比,rails服务器正在进行更复杂和更高级别的处理。它具有智能,如连接数据库和处理会话。这些东西不是免费提供的,并且会降低服务器的内存。诸如nginx之类的前端服务器没有任何这种开销,并且可以以完全相同的方式提供视频文件。

Another reason is memory use. A rails server is doing far more complex and high level processing than say nginx. It has smarts in it like connecting to databases and handing sessions. These things don't come for free and cost memory on your server. A front end server such as nginx does not have any of this overhead and can serve video files in the exact same way.

理想情况下,rails甚至不应该提供静态资产,例如JS和CSS。您可以更好地配置前向Web服务器,以便直接对客户端执行此操作。您将减少开销。视频是另外一个问题。我建议看看为托管视频而构建的外部托管服务。亚马逊有S3,我相信还有其他一些。

Ideally rails should not even serve static assets such as JS and CSS. Your better configuring your forward facing web server to do this directly to the client. You will have less overhead. Video is a whole other issue on top of that. I would suggest looking at an external hosting service built for hosting video. Amazon has S3 and I am sure there are a few others.

同样的论点也适用于WEBRick。它是一个开发服务器,甚至没有编译的好处。这是纯粹的红宝石,所以它会比猛禽或美洲豹更糟糕。

The same argument would go for WEBRick. It's a development server that does not even have the benefit of being compiled. It's pure ruby so it's going to be even worse than say raptor or puma.

这篇关于Rails 4 - 如何提供视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆