Ruby on rails 通过rails向Ipad提供mp4文件的正确方式是什么?
我们在使用默认的rails 3应用程序在ipad上播放MP4时遇到问题。在chrome和桌面上的其他浏览器中查看路线时,mp4可以正常运行 这是我们的代码:Ruby on rails 通过rails向Ipad提供mp4文件的正确方式是什么?,ruby-on-rails,ipad,mp4,Ruby On Rails,Ipad,Mp4,我们在使用默认的rails 3应用程序在ipad上播放MP4时遇到问题。在chrome和桌面上的其他浏览器中查看路线时,mp4可以正常运行 这是我们的代码: 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上出现了“无法
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上出现了“无法播放”图标。我们尝试过修改各种标题“内容长度”、“内容范围”等,但它们似乎不会影响最终结果
我们还尝试在某种程度上使用send_数据
i、 e
当在Ipad上观看相同的视频时,从公用文件夹中可以很好地观看
通过rails向Ipad提供mp4文件的正确方式是什么?问题似乎在于rails无法处理ios流媒体mp4所需的http范围请求 这是我们的开发解决方案(使用thin作为我们的服务器):
最终,我们将使用nginx为生产环境中的资产提供服务,因为上述解决方案比我们需要的慢得多 我认为代码中有一个fencepost错误。我认为长度应该是
bytes.end-bytes.begin+1
——如果字节范围是从字节10到12,那么应该是3个字节。此外,如果出于某种原因使用send_data
,请确保在响应标题中设置内容长度
。感谢您提供此解决方案!我添加了基于@tovodeverett评论的更正。使用Sinatra(而不是Rails)进行开发,我使用Sinatra/streaming contrib成功地复制了send_数据行为,如下所示:stream{| out | out.write IO.binread(file|u path,length,offset);out.flush}
感谢您的解决方案!我们注意到,为了使这种方法在Chrome中工作,必须显式地将响应状态设置为206
。
File.open(file_path, "r") do |f|
send_data f.read, :type => "video/mp4"
end
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