Search code examples
phppythonexecsystemyoutube-dl

php system() does not return the last line of output


I have a script that runs youtube-dl . I run that through a php file using system() like so:

$output = system(getenv("YOUTUBEDL_EXE") . " --no-color -f \"mp4\" \"5uR4JucaZ5I\" -o \"/tmp/%(id)s-%(format)s.%(ext)s\"",  $retVal);

The id of the video is not correct (on purpose) and the output I get when I run it on my terminal (linux) is this:

[youtube] 5uR4JucaZ5I: Downloading webpage
[youtube] 5uR4JucaZ5I: Downloading video info webpage
ERROR: 5uR4JucaZ5I: YouTube said: This video does not exist.

BUT, when I run it through system, the $output variable returns the second line, not the last one

[youtube] 5uR4JucaZ5I: Downloading video info webpage

I still get proper exit codes (that it is not completed successfully) but cannot get the last line with the ERROR: tag so I can parse the error. Unfortunately this is the only way to get the error as it only returns 1 on any error type.

(youtube-dl is written in python and its executable starts with :

#!/usr/bin/env python

and a zip header (PK ....) )

Any pointers in how to get the last line are massively welcomed!

Thanks


Solution

  • The last line is an error and gets written to STDERR and not STDOUT. You need to redirect the STDERR to STDOUT if you want to get errors with PHP, you can do that by adding 2>&1 at the end of your command.