Reading Mp4 Files with PHP

Reading mp4 files with PHP

You need to implement the skipping functionality yourself in PHP. This is a code snippet that will do that.

<?php

$path = 'file.mp4';

$size=filesize($path);

$fm=@fopen($path,'rb');
if(!$fm) {
// You can also redirect here
header ("HTTP/1.0 404 Not Found");
die();
}

$begin=0;
$end=$size;

if(isset($_SERVER['HTTP_RANGE'])) {
if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
$begin=intval($matches[0]);
if(!empty($matches[1])) {
$end=intval($matches[1]);
}
}
}

if($begin>0||$end<$size)
header('HTTP/1.0 206 Partial Content');
else
header('HTTP/1.0 200 OK');

header("Content-Type: video/mp4");
header('Accept-Ranges: bytes');
header('Content-Length:'.($end-$begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');

$cur=$begin;
fseek($fm,$begin,0);

while(!feof($fm)&&$cur<$end&&(connection_status()==0))
{ print fread($fm,min(1024*16,$end-$cur));
$cur+=1024*16;
usleep(1000);
}
die();

More Performance

Note that this is not the most efficient way to do it, because the whole file needs to go through PHP, so you will just need to try how it goes for you.

Assuming the reason you want to do this is to restrict access, and you need more efficiency later, you can use a flag for the web server.

Apache with X-Sendfile module or lightty (nginx info here)

$path = 'file.mp4';
header("X-Sendfile: $path");
die();

This is a bit more advanced and you should only use it if you need it, but it is relaxing to know you have an upgrade option when you start out with something that is rather easy but has mediocre performance.

PHP read MP4 file with another extension

I have tested your code, the Content-Type: video/mp4 and readfile should work (it should not be related to the file extensions, imagine if you get the file data from a BLOB, it should also work)

But just if the home directory is relative ,then please use "./home/" instead of "/home/"

and

make sure that the directory / file permissions are set properly. Say make sure the home directory is EXECUTABLE (chmod a+x for home directory) and the mus files are READ-PERMITTED (chmod a+r for the MUS files)

So this open_file.php works:

<?php
header("Content-Type: video/mp4");
readfile("./home/" . $_GET["file"]);
?>

See a working example here:

http://www.createchhk.com/SO/testSO20Nov2021b.php

code as follows:

<video width='640px' height='480px' controls='controls' />
<source type='video/mp4' src='open_file.php?file=test2.mus'>
</video>

Read MP4 files from a URL using PHP

i have tried do that before ,
it's not working because fseek() do not support http,
the file must be 'seekable' that mean you must get file from path

check this answer https://stackoverflow.com/a/21722576/5380308

Get mp4 file tags

You can have a lot of options like;

1. php-mp4info

A simple PHP class to read MP4 meta data from MP4 based files, such as MP4, M4V and F4V.


2. php-reader

PHP Reader is a well documented small library written in PHP to read and write media files and their information headers in an object-oriented manner. Currently supported formats are ASF (Windows Media Player files, ie WMA, WMV, etc), ID3, including both ID3v1 and ID3v2 (MPEG files, ie MP3), MPEG Audio Bit Stream (ie ABS, MP1, MP2, MP3), MPEG Program Stream (MPEG movies, and DVD and HD DVD video discs, ie MPG, MPEG, VOB, EVO), and ISO Base Media File Format (eg QuickTime, MPEG-4 and iTunes AAC files, ie QT, MOV, MP4, M4A, M4B, M4P, M4V, etc).


3. getID3()

getID3() is a PHP script that extracts useful information (such as ID3 tags, bitrate, playtime, etc.) from MP3s & other multimedia file formats (Ogg, WMA, WMV, ASF, WAV, AVI, AAC, VQF, FLAC, MusePack, Real, QuickTime, Monkey's Audio, MIDI and more).


4. PHP based MP4/F4V meta data reader

The MP4Info class is a simple extensible PHP class reading the MP4 container’s frames (called boxes) to get various information, namely the video duration, the video/audio codecs, the width and the height, as well as the embedded XMP meta data.

Using php to output an mp4 video

Iphones use something called byte-ranges for audio and video requests. See this link for a solution. It's in Appendix A.

http://mobiforge.com/developing/story/content-delivery-mobile-devices

Play mp4 file through php in HTML5 Video Tag in Chrome?

OK! I've resolved my problem :) Hope this will help anyone has the same problem as me

Just use that code:

$file = 'local_file';
$newfile = 'outFile';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($newfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}

how to download mp4 file and hide url path using php

I wasnt able to get it working with any of the suggestions here, but after continuing to dig deeper, I was able to get it working.

The only issue though is that I have to save the file from my second server to my first server, for the download to work. I then delete it, but I'm pretty sure this is not efficient performance wise. However, nothing else I tried work.

The key thing(s) that got it to work is ob_start(), while (ob_get_level()) { ob_end_clean(); }

full code -

ob_start();

$userid = $_POST['userID'];
$videosid = $_POST['videosID'];

$nameOld = 'https://path/to/get/'.$userid.'/'.$videosid.'/'.$videosid.'.mp4';
$save = '/path/to/save/it/'.$userid.'/'.$videosid.'/'.$videosid.'.mp4';

$nameNew = "download.mp4";

file_put_contents($save, fopen($nameOld, 'r'));

set_time_limit(0);

header('Connection: Keep-Alive');
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=$nameNew");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($save));

while (ob_get_level()) {
ob_end_clean();
}

readfile($save);
exit;

// delete file when done
unlink($save);

If anyone can suggest a more efficient code example, please go ahead.



Related Topics



Leave a reply



Submit