为什么在将4096个字节写入以proc_open开头的进程后,PHP挂起?

时间:2016-02-28 13:16:52

标签: php stdin vlc proc-open

对于任何想知道的人来说,离开它几个小时之后它现在完美无缺。

我正在尝试使用PHP将视频文件传递给VLC作为即将推出的项目提案的概念证明。

我设法通过创建文件来显示它的工作原理< 4KB(灰色10秒)并测试我的剧本,但我很好奇为什么会发生这种情况。

这是一个示例脚本,看看我的意思:

$filepath = 'Path/to/your/video';
$vlcpath = 'Path/to/your/VLC executable';

$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout
    2 => array("pipe", "w")   // stderr
);

$vlc = proc_open($vlcpath . ' -', $descriptorspec, $pipes, null, null, ['bypass_shell' => true]);

$file = fopen($filepath, 'r');
stream_copy_to_stream($file, $pipes[0]);
fclose($file);
proc_close($vlc);

我在Windows 10上使用PHP 5.5.31。我在PHP网站上看到了一些有关此类事情的错误报告,但他们建议最新版本修复它。我不太了解阻塞流的概念,但我已经尝试过PHP v7.0.3无效。

我正在使用命令行运行此脚本:php file.php

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,试图在Windows上使用LAME进行WAV到MP3转换,但无法找到可行的解决方案。

我尝试了很多东西,包括阻塞/非阻塞写入,写入小(<1k)数据块,休眠和尝试写入,但它永远无法写入所有数据。在它失败之前我所能写的大约是40kb(fwrite失败总是返回0并且永远不会向流写入更多数据,无论我等多久;无论之前写的块的大小。我甚至尝试在写入之间等待几秒钟,它们总是会成功地达到大约30-40kb并且永远不会写更多)。

最终我放弃了,幸运的是LAME可以从文件中读取输入而不是STDIN,所以我只是选择将数据写入临时文件,调用LAME,然后删除临时文件。

以下是相关代码:

// file descriptors for reading and writing to the Lame process
$descriptors = array(
        0 => array('pipe', 'r'), // stdin
        1 => array('pipe', 'w'), // stdout
        2 => array('pipe', 'a'), // stderr
);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    // workaround for Windows conversion
    // writing to STDIN seems to hang indefinitely after writing approximately 0xC400 bytes
    $wavinput = tempnam(sys_get_temp_dir(), 'wav');
    if (!$wavinput) {
        throw new Exception('Failed to create temporary file for WAV to MP3 conversion');
    }
    file_put_contents($wavinput, $data);
    $size = 0;
} else {
    $wavinput = '-'; // stdin
}
// Mono, variable bit rate, 32 kHz sampling rate, read WAV from stdin, write MP3 to stdout
$cmd  = sprintf("%s -m m -v -b 32 %s -", self::$lame_binary_path, $wavinput);
$proc = proc_open($cmd, $descriptors, $pipes);
if (!is_resource($proc)) {
    throw new Exception('Failed to open process for MP3 encoding');
}
stream_set_blocking($pipes[0], 0); // set stdin to be non-blocking
for ($written = 0; $written < $size; $written += $len) {
    // write to stdin until all WAV data is written
    $len = fwrite($pipes[0], substr($data, $written, 0x20000));
    if ($len === 0) {
        // fwrite wrote no data, make sure process is still alive, otherwise wait for it to process
        $status = proc_get_status($proc);
        if ($status['running'] === false) break;
        usleep(25000);
    } else if ($written < $size) {
        // couldn't write all data, small pause and try again
        usleep(10000);
    } else if ($len === false) {
        // fwrite failed, should not happen
        break;
    }
}
fclose($pipes[0]);
$data = stream_get_contents($pipes[1]);
$err  = trim(stream_get_contents($pipes[2]));
fclose($pipes[1]);
fclose($pipes[2]);
$return = proc_close($proc);
if ($wavinput != '-') unlink($wavinput); // delete temp file on Windows
if ($return !== 0) {
    throw new Exception("Failed to convert WAV to MP3.  Shell returned ({$return}): {$err}");
} else if ($written < $size) {
    throw new Exception('Failed to convert WAV to MP3.  Failed to write all data to encoder');
}
return $data;