在Rails开发环境中为.ogv文件设置Mime类型

时间:2010-04-19 12:43:04

标签: ruby-on-rails ruby video mime-types

我正在玩HTML5视频并在ERB中使用以下代码段:

<video id="movie" width="320" height="240" poster="/test.jpg" preload="none" controls="">
    <source src="/test.mp4" type="video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;">
    <source src="/test.ogv" type="video/ogg; codecs=&quot;theora, vorbis&quot;">
</video>

mp4视频从我在开发环境中运行的服务器流入chrome。然而,firefox使用海报图像显示视频播放器,但是有一个大X.问题似乎是mongrel不确定ogv扩展的mime类型,只返回text / plain,如curl所示:

$ curl -I http://0.0.0.0:3000/pr6.ogv
HTTP/1.1 200 OK
Connection: close
Date: Mon, 19 Apr 2010 12:33:50 GMT
Last-Modified: Sun, 18 Apr 2010 12:46:07 GMT
Content-Type: text/plain
Content-Length: 13652587

那么我在哪里配置东西以便返回正确的mime类型?我已经尝试了各种创意搜索答案,但我能找到的最接近的东西是使用Mime :: Type.register然而这似乎只是处理responds_to的东西而且当我尝试它时没有任何效果情况下。

那么电脑设置? Mongrel配置? Rails App配置?不确定在哪里看,也不是mime类型的专家。我的开发环境是Mac,Rails 2.3.5。

1 个答案:

答案 0 :(得分:9)

我遇到了同样的问题,并在此处找到了解决方案:http://9elements.com/io/?p=306

我正在运行rails 2.3.5,所以我将以下代码放在config / initializers / mime_types.rb中,然后重置我的服务器(我使用WEBrick进行本地开发)

Rack::Mime::MIME_TYPES.merge!({
  ".ogg"     => "application/ogg",
  ".ogx"     => "application/ogg",
  ".ogv"     => "video/ogg",
  ".oga"     => "audio/ogg",
  ".mp4"     => "video/mp4",
  ".m4v"     => "video/mp4",
  ".mp3"     => "audio/mpeg",
  ".m4a"     => "audio/mpeg"
})

现在curl显示了ogv文件的正确mime类型。