glenskie16 Posted October 3, 2019 Share Posted October 3, 2019 I am currently streaming my mp4 files perfectly fine using php. My issue is, how do I detect when the video is either no longer being watched, or disconnected? I need to run a function after this so that I know the video is no longer being watched for analytics. Is there a way maybe with sockets or something? I'm currently using this. if (file_exists($request)) { $fp = @fopen($request, 'rb'); $size = filesize($request); $length = $size; $start = 0; $end = $size - 1; 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 = ($end < $c_end ? $end : $c_end); if (($c_end < $c_start) || (($size - 1) < $c_start) || ($size <= $c_end)) { 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); ob_end_flush(); $buffer = 8192; $time_start = time(); $bytes_read = 0; while (!feof($fp) && (($p = ftell($fp)) <= $end)) { $response = stream_get_line($fp, $buffer); echo $response; $bytes_read += strlen($response); if (30 <= time() - $time_start) { file_put_contents('/var/www/html/movieCon/'.$user.'_'.$token.'_'.$movie_id.'.con', time()); $time_start = time(); $bytes_read = 0; } } fclose($fp); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/ Share on other sites More sharing options...
requinix Posted October 3, 2019 Share Posted October 3, 2019 The most you can get is if the connection closes. Normally PHP will quit the script if the connection is closed, so first make it not do that. During your loop see if the connection closed. If so then (exit the loop and) do whatever you want. Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570243 Share on other sites More sharing options...
glenskie16 Posted October 3, 2019 Author Share Posted October 3, 2019 Would i need to increase the execution time on php for this method to work ? Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570248 Share on other sites More sharing options...
requinix Posted October 3, 2019 Share Posted October 3, 2019 You should have had to do that for your script in its current form anyways. Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570255 Share on other sites More sharing options...
Zane Posted October 3, 2019 Share Posted October 3, 2019 Define "no longer being watched". The video ending is pretty clear, but not being watched could be a number of things. No longer in window scroll view Video has been stopped Video has been paused How are you streaming this mp4? Though an HTML5 player? Or, do you have a specialized library for outputting the video to a specific third-party video player? Or, perhaps a own roll-your-own version of a video player? In any case, these events should be available from the video player itself. PHP is just going to grab the mp4 and output it for the video player to use. Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570267 Share on other sites More sharing options...
Barand Posted October 3, 2019 Share Posted October 3, 2019 8 minutes ago, Zane said: but not being watched could be a number of things. The viewer has fallen asleep The viewer has gone to make a drink etc Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570270 Share on other sites More sharing options...
Zane Posted October 3, 2019 Share Posted October 3, 2019 The viewer make have taken off their glasses or been stabbed in the eyes. Perhaps the cat walked in front of the computer screen. Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570272 Share on other sites More sharing options...
Barand Posted October 3, 2019 Share Posted October 3, 2019 3 minutes ago, Zane said: Perhaps the cat walked in front of the computer screen My cat was always doing that - puked all over the keyboard once. Had to get a new one (keyboard, that is) Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570273 Share on other sites More sharing options...
Zane Posted October 3, 2019 Share Posted October 3, 2019 1 minute ago, Barand said: My cat [...] puked all over the keyboard Yes another reason to stop watching the video. Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570274 Share on other sites More sharing options...
glenskie16 Posted October 4, 2019 Author Share Posted October 4, 2019 So like if they just disconnect from the stream. Also it can be in any player, like VLC, html5, so it would need to know if the person disconnected from the stream, or if they lost connection or any situation. Quote Link to comment https://forums.phpfreaks.com/topic/309320-php-stream-mp4-detect-when-client-stops-streaming-or-disconnects/#findComment-1570288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.