{"id":1653,"date":"2016-10-28T22:52:06","date_gmt":"2016-10-28T22:52:06","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1653"},"modified":"2016-10-28T22:52:06","modified_gmt":"2016-10-28T22:52:06","slug":"how-to-use-php-to-output-an-mp4-video","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2016\/10\/how-to-use-php-to-output-an-mp4-video\/","title":{"rendered":"How to use PHP to output an mp4 video ?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/phpmind-mp4.png\" alt=\"phpmind-mp4\" width=\"969\" height=\"443\" class=\"aligncenter size-full wp-image-1654\" srcset=\"https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/phpmind-mp4.png 969w, https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/phpmind-mp4-300x137.png 300w, https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/phpmind-mp4-768x351.png 768w, https:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2016\/10\/phpmind-mp4-624x285.png 624w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/><br \/>\nI want my videos URL to keep hidden from the users while still, they can able to see the video.\u00a0<\/p>\n<p>It have 2 parts.<\/p>\n<p>1. PHP code and<br \/>\n2. HTML video Code<\/p>\n<pre lang=\"php\">\r\n<?php\r\n$path = 'your.mp4';\r\n\r\n$size=filesize($path);\r\n\r\n$fm=@fopen($path,'rb');\r\nif(!$fm) {\r\n  \/\/ You can also redirect here\r\n  header (\"HTTP\/1.0 404 Not Found\");\r\n  die();\r\n}\r\n\r\n$begin=0;\r\n$end=$size;\r\n\r\nif(isset($_SERVER['HTTP_RANGE'])) {\r\n  if(preg_match('\/bytes=\\h*(\\d+)-(\\d*)[\\D.*]?\/i', $_SERVER['HTTP_RANGE'], $matches)) {\r\n    $begin=intval($matches[0]);\r\n    if(!empty($matches[1])) {\r\n      $end=intval($matches[1]);\r\n    }\r\n  }\r\n}\r\n\r\nif($begin>0||$end<$size)\r\n  header('HTTP\/1.0 206 Partial Content');\r\nelse\r\n  header('HTTP\/1.0 200 OK');\r\n\r\nheader(\"Content-Type: video\/mp4\");\r\nheader('Accept-Ranges: bytes');\r\nheader('Content-Length:'.($end-$begin));\r\nheader(\"Content-Disposition: inline;\");\r\nheader(\"Content-Range: bytes $begin-$end\/$size\");\r\nheader(\"Content-Transfer-Encoding: binary\\n\");\r\nheader('Connection: close');\r\n\r\n$cur=$begin;\r\nfseek($fm,$begin,0);\r\n\r\nwhile(!feof($fm)&#038;&#038;$cur<$end&#038;&#038;(connection_status()==0))\r\n{ print fread($fm,min(1024*16,$end-$cur));\r\n  $cur+=1024*16;\r\n  usleep(1000);\r\n}\r\ndie();\r\n?>\r\n\r\n\r\n<\/pre>\n<pre lang=\"html\">\r\n<video width=\"320\" height=\"240\" controls>\r\n    <source src=\"mp4.php\" type=\"video\/mp4\">\r\n    Your browser does not support the video tag.\r\n<\/video>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I want my videos URL to keep hidden from the users while still, they can able to see the video.\u00a0 It have 2 parts. 1. PHP code and 2. HTML video Code Your browser does not support the video tag.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[40,3],"tags":[],"class_list":["post-1653","post","type-post","status-publish","format-standard","hentry","category-html5","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":1,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"predecessor-version":[{"id":1655,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1653\/revisions\/1655"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}