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

查看:203
本文介绍了通过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.

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

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

推荐答案

问题似乎是rails不处理ios需要流式传输mp4s的http范围请求。

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

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

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天全站免登陆