PHP passthru with proper child process closing

173 Views Asked by At

I am currently using the code below to try and restream a hls live stream via php the reason for this is php is all i have access to on the system i am using i can not use or install anything else and i like to watch my home tv in my current location my hls streams come direct from my own tbs octo encoder at my home so i know thats all good.

my issue is that the stream randomly stops after around 30-50 seconds of playing it can be restarted no problem so i know the reading of the stream is not the issue.

also when i disconnect and stop watching the stream the ffmpeg / streamlink processes do not stop and stay running in the back ground this has led to me getting some rather frustrating emails from the owner of the system i am using

does anyone know if this is the best way to handel this task and how to possibly close the child processes created by passthru?

PS: I know all caps is not the correct way to code in production but as this is my personal code and it does not effect the way in witch the code is run it makes it so much easier for me to read being dyslexic!

<?php

ini_set('output_buffering', '0');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

set_time_limit(0);
ob_implicit_flush();

include './core/functions.php';

$PROCESS_URL = 'HTTP STREM URL GOES HERE';
    
$ENCODING_TYPE = 'mpegts';
    
$ENCODING_SETTINGS = 'streamlink '. $PROCESS_URL . ' best -O | '. FFMPEG .
    ' -threads 0' .
    ' -re' .
    ' -probesize 9000000'.
    ' -analyzeduration 9000000'.
    ' -i pipe:0' .
    ' -c:v copy -c:a copy' .
    ' -tune zerolatency' .
    ' -preset ultrafast' .
    ' -fflags +genpts' .
    ' -mpegts_flags initial_discontinuity' .
    ' -f ' . $ENCODING_TYPE . 
    ' pipe:1 2>' . LOG_DIRECTORY . '/' . MD5($PROCESS_URL) . '_ffmpeg.txt';

header('Content-type: application/octet-stream');
header('X-Accel-Buffering: no');

@passthru($ENCODING_SETTINGS);

?>
0

There are 0 best solutions below