0

Objective: Getting real time update from terminal without new line break. Currently I am using this

    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();

Output of this code

            [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 

It outputs only one time at the end of download (at first it gave me real time update later only once i dont know why this happen). But in my windows command promt I am getting the percentage and speed in one line without new line break. To do that I have tried this code

        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>';

and failed... It results like empty

Output

Array
  (
    [stdout] => 
    [stderr] => C:\Python27\python.exe: No module named youtube_dl

    [return] => 1
  )

So can anyone provide any suggestion to modify these code or new one? Thanks.

user2978381
  • 145
  • 1
  • 2
  • 14

1 Answers1

0

This is not a php problem. As you can see in the stderr output, the module youtube_dl cannot be loaded. Most likely, the youtube_dl directory is not in your PYTHONPATH. Try either extending the PYTHONPATH or moving the youtube_dl directory into your current working directory.