benjipott / video.js-chromecast

Use chromecast with videojs html5 player
Other
207 stars 86 forks source link

Send mp4 files through PHP script #45

Open ShinJii89 opened 6 years ago

ShinJii89 commented 6 years ago

Hi,

I'm sending mp4 files through php script (yes all needed headers are here for example content-type video/mp4) but when I want to play it with your plugin to chromecast there's error:

VIDEOJS: Cast APIs not available video.js:23329 VIDEOJS: Cast APIs not available. Max reconnect attempt video.js:23329 VIDEOJS: Cast APIs are available video.js:23329 VIDEOJS: Cast video: https://subdomain.domain.com/vipvideo.php?vid=id&type=subtitles video.js:23329 VIDEOJS: Session initialized: 536c8964-45e7-4003-a10c-c2328d8e5fae source : https://subdomain.domain.com/vipvideo.php?vid=id&type=subtitles type : video/mp4 video.js:23329 VIDEOJS: Cast Error: {"code":"session_error","description":"LOAD_FAILED","details":{"type":"LOAD_FAILED"}}

Why? It's because something wrong on my side (but it's playing in browser) or it can't be passed through php by chromecast itself? How chromecast works? Because to play this videos people should be logged etc. so when I'm logged in browser it should work, right? I don't need any additional sessions for chromecast?

benjipott commented 6 years ago

please send me a demo page on codepen or other

Fenny commented 6 years ago

@ShinJii89 Can you share some code so we can help you?

ShinJii89 commented 6 years ago

Yyyy.. I think i'm sure it's because of that i'm streaming video through php function like readfile(), not directly link to .mp4 :P When I checked it with .mp4 file directly it works... But if you still think it's not because of php I can give you some code...

Fenny commented 6 years ago

Couk you share me your php code so I can take a look. In the meantime, i think this link will help you out: https://stackoverflow.com/questions/15797762/reading-mp4-files-with-php

ShinJii89 commented 6 years ago

Most code is from there - https://gist.github.com/ranacseruet/9826293 and I have there function session_write_close(); because without that there was a bug in browsers with long loading pages while playing video etc.

Fenny commented 6 years ago

Do you use a remote or local file?

ShinJii89 commented 6 years ago

Local... I mean it's on that server from where I stream chromecast.

Fenny commented 6 years ago

Could you try, this works for me:

$file='path/file.mp4'; header('Content-Type: video/mp4'); #Optional if you'll only load it from other pages header('Accept-Ranges: bytes'); header('Content-Length:'.filesize($file)); readfile($file);

ShinJii89 commented 6 years ago

I have (it's on other subdomain - subdomain.domain.com, but i'm playing it from domain.com):

`$file='pathtomp4'; if (@file_exists($file)) {

                    $fp = @fopen($file, 'rb');
                    $size = @filesize($file); // File size
                    $length = $size;           // Content length
                    $start = 0;               // Start byte
                    $end = $size - 1;       // End byte
                    //ob_get_clean();

                    header('Content-type: video/mp4');
                    header("Cache-Control: max-age=2592000, public");
                    header("Expires: " . gmdate('D, d M Y H:i:s', time() + 2592000) . ' GMT');
                    header("Last-Modified: " . gmdate('D, d M Y H:i:s', @filemtime($file)) . ' GMT');
                    header("Accept-Ranges: bytes 0-$end");
                    // header("Accept-Ranges: bytes");
                    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);
                    } else {
                        header("Content-Length: " . $length);
                        header("Accept-Ranges: bytes");
                    }
                   // ob_start();
                    $buffer = 1024 * 8;
                    while (!feof($fp) && ($p = ftell($fp)) <= $end) {
                        if ($p + $buffer > $end) {
                            $buffer = $end - $p + 1;
                        }
                        set_time_limit(0);
                        $data = @stream_get_contents($fp, $buffer);

                        echo $data;
                        //echo fread($fp, $buffer);
                       // ob_flush();
                        //flush();
                    }
                    fclose($fp);
                    exit();`
ShinJii89 commented 6 years ago

and? did you check this? :D

ShinJii89 commented 6 years ago

anyone? :)

ShinJii89 commented 6 years ago

@benjipott I sent you e-mail with login data, can you check this out?

benjipott commented 6 years ago

Your original source still working directly so , i try to build a new version.

ShinJii89 commented 6 years ago

So let me know :D

ShinJii89 commented 6 years ago

@benjipott and? Still nothing? :(