I need to read ffmpeg progress output with php, but since ffmpeg started using chunked transfer I am not able to make it work
Can anyone help me or let me know if what I want to do is just not possible with php?
nginx block
#FFmpeg Report Progress
location = /progress.php
{
allow 127.0.0.1;
deny all;
chunked_transfer_encoding on;
fastcgi_keep_conn on;
fastcgi_pass fpm-php;
include fastcgi_params;
fastcgi_ignore_client_abort on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}I tried with this code to read it, but it doesn't work. Very old versions of ffmpeg that did not use chunked transfer worked fine
<?php
ignore_user_abort(true);
//set_time_limit(0);
// Validate the client IP and required query parameters
if ($_SERVER["REMOTE_ADDR"] != "127.0.0.1" || empty($_GET["stream_id"])) {
http_response_code(403);
die('Unauthorized access');
}
$streamId = intva
l($_GET["stream_id"]);
$filePath = "/var/log/ffmpeg/{$streamId}_.progress";
// Open the PHP input stream and file for writing
$handle = fopen("php://input", "r");
$file = fopen($filePath, "w");
// Initialize data block
$dataBlock = [];
// Continue to read from the input stream as long as the connection is alive
while (!feof($handle)) {
$line = fgets($handle);
if ($line === false || trim($line) === '') {
continue; // Skip any empty reads or handle whitespace
}
// Check if line contains '=' to ensure it's a key-value pair
if (strpos($line, '=') === false) {
// Optionally log or handle lines without '='
// For example, you could write to an error log or just ignore:
// error_log("Invalid line format: $line");
continue;
}
// Parse line by line, extracting key-value pairs
list($key, $value) = explode("=", $line, 2);
$key = trim($key);
$value = trim($value);
if (!empty($key)) {
$dataBlock[$key] = $value;
// Write updates to file as they come
fwrite($file, json_encode([$key => $value]) . "\n");
fflush($file);
}
}
fclose($handle);
fclose($file);
?>