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

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

问题描述

我在 Web 应用程序中使用 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.

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

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/

另一个原因是内存使用.Rails 服务器正在执行比 nginx 更复杂和更高级的处理.它具有智能功能,例如连接到数据库和处理会话.这些东西不是免费的,也不是服务器上的内存成本.像 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 服务器以直接对客户端执行此操作.您将减少开销.除此之外,视频是另一个问题.我建议查看为托管视频而构建的外部托管服务.Amazon 有 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天全站免登陆