Open external videos(mp3/mp4) with PHP

Asked

Viewed 1,996 times

6

Hello! I am using a "player" (in php) to open my external videos.

However the user does not have control of the length bar of the video, he can not have control, nor return the video, nor advance, and it hurts me a lot.

Player php 01

$video = "http://www.blogger.com/video-play.mp4?contentId=b65e1fd886cd357";
header('Content-Type: video/mp4');
readfile ($video);

With player 01, as I said, the user has no control over the duration bar.

I ended up finding another code, where the user gets to have control with some changes.

Player php 02

$request = "video.mp3";
$file = $request;
$fp = @fopen($file, 'rb');
$size   = filesize($file); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte
header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {
    $c_start = $start;
    $c_end   = $end;
    list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    if (strpos($range, ',') !== false) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    if ($range == '-') {
        $c_start = $size - substr($range, 1);
    }else{
        $range  = explode('-', $range);
        $c_start = $range[0];
        $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
    }
    $c_end = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    $start  = $c_start;
    $end    = $c_end;
    $length = $end - $start + 1;
    fseek($fp, $start);
    header('HTTP/1.1 206 Partial Content');
}
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
    if ($p + $buffer > $end) {
        $buffer = $end - $p + 1;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
}
fclose($fp);
exit();

With player 02, the problem is that it can’t pull from external sites such as blogger.

I need a solution so that Player php 01 can make the duration bar accessible, or a solution so that Player php 02 can pull from external site.

I really need to use the php readfile, I can’t use another player.

  • 1

    A Javascript solution would be easier. Your server communicating with others who don’t have webservice doesn’t seem very secure

  • @Luanpedro, I don’t know about the rule of your business, but what is the possibility of you downloading the video to the server and making the call locally?

  • Another thing, I believe that through the @fopen($file, 'rb'); you may not be able to do these negotiations remotely because it’s not a stream, that is, you don’t keep a connection to anything to get or send something additional, it literally just opens.

  • I cannot use javascript =/ > Sveen It would be impossible, I would have to upload thousands of files to my server > David dias

3 answers

0

As the PHP documentation tells to the readfile function,

A URL can be used as a filename if fopen Wrappers is active. See fopen() for more details on how to specify Urls as a filename. See also the supported Protocols and Wrappers for information about what capabilities each wrapper has, usage notes and information about predefined variables provided.

You can try using the function file_get_contents

$video_url = "http://www.blogger.com/video-play.mp4?contentId=b65e1fd886cd357"
$video = file_get_contents($video_url);
header('Content-Type: video/mp4');
echo $video;

Or you can choose to download the video, save it and use the readfile :)

-1

Friend use javascript for this, look at an example

https://videojs.com/

Go to this site and look at the video on the home page...

<head>
  <link href="https://vjs.zencdn.net/7.6.5/video-js.css" rel="stylesheet">

  <!-- If you'd like to support IE8 (for Video.js versions prior to v7) -->
  <script src="https://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script>
</head>

<body>
  <video id='my-video' class='video-js' controls preload='auto' width='640' height='264'
  poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
    <source src='MY_VIDEO.mp4' type='video/mp4'>
    <source src='MY_VIDEO.webm' type='video/webm'>
    <p class='vjs-no-js'>
      To view this video please enable JavaScript, and consider upgrading to a web browser that
      <a href='https://videojs.com/html5-video-support/' target='_blank'>supports HTML5 video</a>
    </p>
  </video>

  <script src='https://vjs.zencdn.net/7.6.5/video.js'></script>
</body>

There is no secret... and there are several plugin for jquery that does this too...

I saw your comment now, that there are thousands of files and can not download, so since you are reading the videos with PHP the other way would be this...

video.php
<?php
header("Content-Type: video/mp4")
echo file_get_contents($_GET['url']);
?> 

Now just call on the page:

<source src='video.php?url=http://www.blogger.com/video-play.mp4' type='video/mp4'>

got it?

  • this site also uses a browser player on https://elojobhigh.com.br/ check out the template here

  • javascript will load it to your server, without downloading...

-2

I did so and functioned:

video.php

$file = "urldovideo.mp4";
header('Content-Type: video/mp4');
readfile($file);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.