Toni Ojsteršek
Toni Ojsteršek

Reputation: 11

Playing mp4 files from rar or zip files

I have one problem. I would like to order from (zip) files play video in such a way to get out connection so you can include it in HTML5 badge. As I gave an example. But this not working.

<?php $video = fopen('zip://video.zip#video.mp4', 'r'); ?>
<video>
    <source src='<? echo $video; ?>' type='video/mp4' /> 
</video>

Upvotes: 1

Views: 1578

Answers (1)

EPB
EPB

Reputation: 4029

$video in the above code is just a server-side file handle you could use to read the file from the zip. It's not directly usable for the browser.

You'll need to handle reading the file and returning it in a separate HTTP request. Usually you'd use a second script for this. (Or if your video is relatively small, you might be able to use data urls, but it's not something I'd try to do.) Additionally, if you want to allow for byte range requests, you'd have to handle that yourself in your video serving logic.

Here's a fairly simple scenario:

My videos.zip file contains a couple of different videos, and I want to retrieve a specific one and show it on a page called video.php

First I have my viewer, say video.php, [edit: containing the video tag and with a URL to my second script as the source. Since I might want to serve the other file at some point, I set it up to accept a filename in the v query parameter.]

..some html/php..
<video>
   <source src='zipserve.php?v=itsrainintoast.mp4' type='video/mp4' /> 
</video>
..more html/php..

Then, in zipserve.php I have something like this:

$filename = $_GET['v']; //You probably want to check that this exists first, btw.
$fp = fopen('zip://videos.zip#'.$filename, 'r');
if($fp)
{
   header('content-type: video/mp4');
   //Note: you should probably also output an appropriate content-length header.
   while(!feof($fp))
   {
      echo fread($fp, 8196);
   }
   fclose($fp);
}
else
{
   echo 'Some error message here.';
}

--Addendum-- It should also be noted that this'll require the zip php extension to be enabled.

A more complete example of a video fetching script with range handling and the like can be found in the question at mp4 from PHP - Not playing in HTML5 Video tag but you'd need to tweak it to allow reading from the zip file.

Upvotes: 2

Related Questions