目标: 从终端获取实时更新而无需换行。目前我正在使用这个
header('Content-Encoding: none;');
set_time_limit(0);
$handle = popen("python -m youtube_dl https://www.youtube.com/watch?v=S2JOicnUh1s", "r");
if (ob_get_level() == 0)
ob_start();
while(!feof($handle)) {
$buffer = fgets($handle);
$buffer = trim(htmlspecialchars($buffer));
echo "<pre>";
echo $buffer ;
echo str_pad('', 4096);
echo "</pre>";
ob_flush();
flush();
sleep(1);
}
pclose($handle);
ob_end_flush();
此代码的输出
[youtube] S2JOicnUh1s: Downloading webpage
[youtube] S2JOicnUh1s: Downloading video info webpage
[download] 0.0% of 3.26MiB at 12.82KiB/s ETA 04:20
[download] 0.1% of 3.26MiB at 38.46KiB/s ETA 01:26
[download] 0.2% of 3.26MiB at 89.74KiB/s ETA 00:37
[download] 0.4% of 3.26MiB at 79.79KiB/s ETA 00:41
[download] 0.9% of 3.26MiB at 79.28KiB/s ETA 00:41
....................................................
....................................................
[download] 98.1% of 3.26MiB at 72.50KiB/s ETA 00:00
[download] 100.0% of 3.26MiB at 72.66KiB/s ETA 00:00
[download] 100% of 3.26MiB in 00:45
它在下载结束时只输出一次(起初它只在我不知道为什么会发生这种情况时才给我实时更新)。但是在我的 Windows 命令提示符中,我在一行中获得了百分比和速度,而没有换行符。为此,我尝试了此代码
function execute($cmd,$stdin=null){
$proc=proc_open($cmd,array(0=>array('pipe','r'),1=>array('pipe','w'),2=>array('pipe','w')),$pipes);
fwrite($pipes[0],$stdin); fclose($pipes[0]);
$stdout=stream_get_contents($pipes[1]); fclose($pipes[1]);
$stderr=stream_get_contents($pipes[2]); fclose($pipes[2]);
$return=proc_close($proc);
return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$return );
}
echo '<pre>';
print_r(execute('python -m youtube_dl https://www.youtube.com/watch?v=S2JOicnUh1s'));
echo '</pre>';
失败了......结果就像是空的
输出
Array
(
[stdout] =>
[stderr] => C:\Python27\python.exe: No module named youtube_dl
[return] => 1
)
那么任何人都可以提供任何建议来修改这些代码或新代码吗?谢谢。