به نام همه خوبیها
اگه در زمینه Live transcoding stream مطالعه کرده باشید حتما با نرم افزارهایی چون Wowza (ویندوز و لینوکس) و ffserver (لینوکس) آشنایی دارید اما اگر خواستیم به کمک PHP و خیلی راحت تر این قابلیت را ایجاد کنیم می توانید از proc_open در PHP استفاده کنیم ، با من همراه شوید در معرفی این کلاس کاربردی PHP !
در ابتدا ffmpeg را روی ویندوز و یا لینوکس خود نصب کنید منظور همان ست کردن environment variables هست خوب حالا بریم سراغ کد زیر .
/** * Live Transcoding Video Stream +ffmpeg * * @author Amirhossein@tavousi * @link http://tavousi.name */ $file = "Deadpool.mp4"; /* just a very simple debug logger */ function dbg($message) { file_put_contents('error-output.txt', time() . ": $message\n", FILE_APPEND); } /* script execution settings */ set_time_limit(0); ignore_user_abort(true); /* do not terminate script execution if disconnect */ $start = 0; $size = filesize($file); $end = $size - 1; header("HTTP/1.1 206 Partial Content"); header("Connection: close"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache"); header("Pragma: no-cache"); header("Content-Type: video/flv"); header("Accept-Ranges: bytes"); header("Content-Length: " . $size); header("Content-Range: bytes 0-" . $end); header("Content-Disposition: inline; filename=".$file); /* parse path arguments */ $args = array(); $path_info = explode('/', $_SERVER["PATH_INFO"]); $arguments = explode(';', $path_info[1]); //return var_dump($arguments); foreach ($arguments as $argument) { $key = substr($argument, 0, strpos($argument, ':')); $value = substr($argument, strpos($argument, ':') + 1); $args[$key] = $value; } $cmd = 'ffmpeg'; if (!empty($_GET['start'])) $cmd .= ' -ss ' . (!empty($_GET['start']) ? $_GET['start'] : 0); $cmd .= ' -i "'.$file.'" -c:a copy -c:v copy'; if (!empty($args['duration'])) $cmd .= ' -t ' . $args['duration']; $cmd .= ' -f flv pipe:1'; /* execute transcode */ $descriptorspec = array( 0 => array( "pipe", "r" ), // stdin is a pipe that the child will read from 1 => array( "pipe", "w" ), // stdout is a pipe that the child will write to 2 => array( "file", "error-output.txt", "a" ) // stderr is a file to write to ); $process = proc_open($cmd, $descriptorspec, $pipes); dbg("Started transcode process.\nCommand Line: $cmd"); $stdout_size = 0; if (is_resource($process)) { while (!feof($pipes[1])) { $chunk = fread($pipes[1], 500 * 1024); $stdout_size += strlen($chunk); if ($chunk !== false && !empty($chunk)) { echo $chunk; /* flush output */ if (ob_get_length()) { @ob_flush(); @flush(); @ob_end_flush(); } @ob_start(); dbg("Chunk sent to browser and flush output buffers"); } if (connection_aborted()) { dbg("Connection aborted."); break; } } dbg("Finished reading from stdout."); fclose($pipes[1]); if ($stdout_size == 0) { /* not read anything from stdout indicates error */ $stderr = stream_get_contents($pipes[2]); dbg("An Error Occured. Stderr: " . $stderr); } fclose($pipes[2]); /* this should quit the encoding process */ fwrite($pipes[0], "q\r\n"); fclose($pipes[0]); dbg("Closing process..."); $return_value = proc_close($process); dbg("Process closed with return value: " . $return_value); }خوب من خروجی را تو videojs روی وب تست کردم خیلی خوب جواب میده اما اگر ویدیو جایی هنوز لود نشده باشه نمایش داده نمیشه نمیره برای لود که من -ss رو توی کد قرار دادم ولی تست نکردم هر چند نیاز به یسری تغییرات در header ها هم هست برای این کار این از این اما تو محیط ویندوز هم که فایل رو دانلود کردم 4و5 ثانیه اخر ویدیو پخش نمیشه حالا اگه بازهم روش کار کردم کد رو بروز می کنم . شاد و سرزنده باشید.