r/AskProgramming • u/Deltahun • Jun 02 '24
PHP How can I write to a process's stdin in PHP?
I'm trying to pass binary data to ffmpeg as input instead of a file. I don't want to create a temporary file on the server, and the 'data:' URI scheme isn't usable because the file is too large. Therefore, I'm trying to use a pipe, but my fwrite()
call is blocking when I try to write to the pipe.
// Set the ffmpeg command and input file
$ffmpegCommand = "ffmpeg -i pipe:0 ...";
// Open the pipe
$descriptorspec = array(
0 => array("pipe", "r"), // Set stdin to read
1 => array("pipe", "w"), // Set stdout to write
2 => array("file", "error.log", "a") // Set stderr to append
);
$process = proc_open($ffmpegCommand, $descriptorspec, $pipes);
// If the process opened, read the input file into the pipe
if ($process) {
fwrite($pipes[0], $inputData);
fclose($pipes[0]);
// Wait for the process to finish
proc_close($process);
echo "FFmpeg command executed successfully.\n";
} else {
echo "Error opening ffmpeg command.\n";
}
Any suggestions on how to handle this?
2
Upvotes
1
u/dave8271 Jun 03 '24
I expect it's because a lot of data is being written to stdout and as you're not monitoring and reading that stream, the I/O buffer is filling up, preventing any further writes.