Mit*_*van 9 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);
Run Code Online (Sandbox Code Playgroud)
我在Windows 10上使用PHP 5.5.31.我在PHP网站上看到了一些有关此类事情的错误报告,但他们建议最新版本修复它.我不太了解阻塞流的概念,但我已经尝试过PHP v7.0.3无济于事.
我正在使用命令行运行此脚本: php file.php
我在 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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
367 次 |
| 最近记录: |