根据这篇关于 Ogg 媒体的 Mozilla 文章,媒体在浏览器中使用 X-Content-Duration
标头,给出片段的长度(以秒为单位)。
假设我将该长度存储在某个地方(当然在数据库中,也许也在文件名本身中(video-file-name.XXX.ogv
,其中 XXX
是时间以秒为单位)),有什么方法可以仅使用 Apache .htaccess
设置来形成这个额外的标头?我问这个问题是因为将文件加载到 PHP 脚本中似乎很笨拙,特别是当 PHP 默认添加其他禁用缓存的标头,并且无法正确响应范围(部分内容)请求时。是的,可以用 PHP 编写大量代码来支持 ETag 和范围请求,但当 Apache 内置了所有这些功能时,仅仅为了添加一个标头就完成所有这些工作似乎有些过分了。
According to this Mozilla article on Ogg media, media works more seamlessly in the browser with an X-Content-Duration
header, giving the length in seconds of the piece.
Assuming that I have that length stored somewhere (certainly in a database, perhaps also in the filename itself (video-file-name.XXX.ogv
, where XXX
is the time in seconds)), is there any way to form this extra header using only Apache .htaccess
settings? I ask because loading the file into a PHP script seems clumsy, especially when PHP will by default add other headers which disable caching, and won't respond properly to range (partial content) requests. Yes, a lot of code could be written in PHP to support ETags and range requests, but it seems like overkill to do all that just to add one header, when Apache has all that functionality built in.
发布评论
评论(3)
这是 mod_cern_meta 的域。它允许为文件静态分配额外的 HTTP 标头。
您可以使用 cron 作业,并为每个视频生成一个 *.meta 文件。
This is the domain of mod_cern_meta. It allows statically assigning extra HTTP headers to files.
You could use a cron job, and generate a *.meta file for every video.
我没有示例,但您应该能够使用 mod_header 在 .htaccess 级别指定 HTTP 响应标头。
当然,我应该在哪里添加标头的问题实际上取决于您访问它的方式。如果您只是下载静态资源,那么通过 Apache 添加它是有意义的。但是,您提到了数据库。如果您决定将这些文件存储在数据库中,那么您将有一些提供该文件的 API,在这种情况下,API 实现应该附加标头而不是卸载到 apache。
另外,如果您想要的动态数据需要处理来确定(它不在文件名等中),那么您已经在使用一些代码引擎来实现它,只需添加标头即可。
I don't have examples, but you should be able to use mod_header to specify HTTP Response headers at the .htaccess level.
Of course, the question of where should I add the header really depends on how you are accessing it. If you are just hitting a static resource for download, adding it via Apache makes sense. However, you mention a DB. If you decide to store those files in a database, then you have some API providing the file, in which case that API implementation should append the header and not offload to apache.
Also, if the dynamic data you are wanting ever requires processing to determine (its not in the filename or etc) then you're already using some code engine to achieve it, just let that add the header.
这就是您使用 mod_perl 扩展所做的事情,拦截这些请求并在允许 Apache 继续处理之前添加额外的标头。
一种可能有效的纯 PHP 方法是使用 mod_rewrite 让请求通过 PHP 路由,添加附加标头,然后让 Apache 使用 虚拟功能。
或者,您可以使用持续时间数据库构建静态 .htaccess 文件,该文件使用 mod_header 为每个请求的文件插入正确的持续时间标头。
This is the kind of thing you do with a mod_perl extension, to intercept these requests and add additional headers before allowing Apache to continue handling it.
One purely PHP approach which might work is to have the requests route through PHP using mod_rewrite, add the additional header, but then let Apache handle the rest by using the virtual function.
Alternatively, you could use your database of durations to contruct a static .htaccess file which uses mod_header to insert the correct duration header for each requested file.