views:

44

answers:

3

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.

+1  A: 

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.

Taylor
The video or audio file itself would ideally be a static file, though the HTML in which it's embedded would be written by PHP.
TRiG
+1  A: 

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.

Paul Dixon
The `virtual()` function looks fascinating, and I thank you for drawing my attention to a piece of PHP I knew nothing about, but I've decided to go with `mod_cern_meta`.
TRiG
+1  A: 

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.

mario
I'd never heard of mod_cern_meta. Looks interesting.
TRiG
@TRiG: It's a pretty old module, possibly from pre-Apache times. But it's likely the most efficient solution for amending headers.
mario
I did some testing and got it to work, so I've decided to go with this. Projected workflow: `1` User uploads video or audio file. `2` PHP calls FFmpeg to find out various information about the media file (container, codecs, duration). `3` PHP calls FFmpeg to create versions of the file in all formats needed for HTML5 (unless the original upload is already in one such format, in which case we have a little less work to do). `4` PHP uses the "duration" information it got from FFmpeg earlier to create `.meta` files for each Ogg file. I haven't even started on the uploader yet!
TRiG