通过 rails 向 Ipad 提供 mp4 文件的正确方法是什么? [英] What is the proper way to serve mp4 files through rails to an Ipad?

查看:19
本文介绍了通过 rails 向 Ipad 提供 mp4 文件的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在使用默认 rails 3 应用程序提供可在 ipad 上播放的 mp4 时遇到问题.在桌面上的 chrome 和其他浏览器中查看路由时,mp4 会正确提供.

We're having trouble serving mp4s that will play on an ipad using a default rails 3 app. The mp4 is served correctly when viewing the route in chrome and other browsers on a desktop.

这是我们的代码:

file_path = File.join(Rails.root, 'test.mp4')
send_file(file_path, :disposition => "inline", :type => "video/mp4")

我们点击 0.0.0.0:3000/video/test.mp4 来查看视频,但在 ipad 上显示无法播放图标.我们尝试修改各种标题Content-Length"、Content-Range"等,但它们似乎不会影响最终结果.

We hit 0.0.0.0:3000/video/test.mp4 to view the video and are presented with cannot play icon on the ipad. We've tried modifying various headers "Content-Length", "Content-Range", etc but they don't seem to affect the end result.

我们也在一定程度上尝试过使用 send_data

We've also tried using send_data to some extent

File.open(file_path, "r") do |f|
    send_data f.read, :type => "video/mp4"
end 

在 Ipad 上观看时,相同的视频可以从公共文件夹中正常播放.

The same video serves fine from the public folder when viewed on the Ipad.

通过 rails 向 Ipad 提供 mp4 文件的正确方法是什么?

What is the proper way to serve mp4 files through rails to an Ipad?

推荐答案

问题似乎是 Rails 无法处理 ios 流式传输 mp4 所需的 http 范围请求.

The problem seems to be that rails doesn't handle http-range requests which ios needs for streaming mp4s.

这是我们的开发解决方案,(使用瘦作为我们的服务器):

This was our solution for development, (using thin as our server):

  if(request.headers["HTTP_RANGE"]) && Rails.env.development?

    size = File.size(file_path)
    bytes = Rack::Utils.byte_ranges(request.headers, size)[0]
    offset = bytes.begin
    length = bytes.end - bytes.begin + 1

    response.header["Accept-Ranges"]=  "bytes"
    response.header["Content-Range"] = "bytes #{bytes.begin}-#{bytes.end}/#{size}"
    response.header["Content-Length"] = "#{length}"

    send_data IO.binread(file_path,length, offset), :type => "video/mp4", :stream => true,  :disposition => 'inline',
              :file_name => file_name

  else
    send_file(file_path, :disposition => 'inline', :stream => true, :file_name => file_name)
  end

最终我们将使用 nginx XSendfile 为我们的生产环境中的资产提供服务,因为上述解决方案是比我们需要的慢得多.

Ultimately we will be using nginx XSendfile to serve the assets in our production environment as the above solution is much slower than what we need.

这篇关于通过 rails 向 Ipad 提供 mp4 文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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