Using PHP to Output an Mp4 Video

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;
}

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>

Convert video to MP4 with php

Here is how I would do it:

Try out this code! ( Tested and works fine )

<form method="post" enctype="multipart/form-data" name="form">





<input type="file" name="media-vid" class=" file_multi_video" accept="video/*">



<input type="submit" name="submit" value="upload"/>

</form>

Create Video File using PHP

ffmpeg will allow you to create videos from still images.

For example, to create a video with a 10fps frame rate, from images 001.jpg .... 999.jpg:

ffmpeg -r 10 -b 1800 -i %03d.jpg test1800.mp4

You can mux streams (audio and video) like (add relevant codec options for bitrate, etc)

ffmpeg -i video.mp4 -i audio.wav -map 1.1 -map 2.1 output.mp4

I'm not going to go into more detail, as ffmpeg is a pain (args change in incompatible ways between versions, and depending on how it was compiled), and finding a rate/compression/resolution setting that is good for you is trial-and-error.

Streaming video/mp4 with php

Nevermind. I downloaded the file from the server and compared it with original stored on server in hex.
The downloaded one had one more byte on begining: 0A and that was the problem. Everything works now.



Related Topics



Leave a reply



Submit