youtube-dl and php exec
Asked Answered
T

1

7

I have installed youtube-dl on my CentOS 6 / Plesk 10 Dedicated server, and via SSH, everything works like a charm !

The thing is I'm trying to write a php script which takes a POST parameter in containing the URL of the video I want to copy to the server, copy it and then process it with ffmpeg.

I thought I would use the exec() function.

I can get an output if I echo the results of youtube-dl --help, but everytime I ask php to execute a command which actually does something with the video, it returns a status of '1', and outputs nothing.

Any ideas on what I'm doing wrong ?

Here is my php code:

<?php 
    $result = array();
    $status;
    $url = $_POST['src'];
    $string = 'youtube-dl "'.$url.'" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s"';
    $string2 = 'youtube-dl --help';
    exec($string, $result, $status);
    echo json_encode(array('status' => $status, 'url_orginal'=>$url, 'url' => $result));
?>

When I execute $string2, I get status: "0" and "url": [youtube-dl help text lines]

But when I execute $string, nothing happens, I get "status": "1" and nothing else, no video is downloaded. I've tried also a simulation with the "-g" parameter, and variants but as soon as youtube-dl has to fetch the video, it breaks.

Thank you in advance !

EDIT

I edited my code so it looks like this :

<?php 
    $result = array();
    $status;
    $url = $_POST['src'];
    $string = 'youtube-dl "'.$url.'" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s"';
    $string2 = 'youtube-dl --help';
    exec($string, $result, $status);
    echo json_encode(array('status' => $status, 'url_orginal'=>$url, 'url' => $result, 'command' => $string));
?>

and the result, which I didn't get yesterday now is :

command: "youtube-dl "http://www.youtube.com/watch?v=coq9klG41R8" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s""
status: 1
url: 
    0: "[youtube] Setting language"
    1: "[youtube] coq9klG41R8: Downloading video info webpage"
    2: "[youtube] coq9klG41R8: Extracting video information"
url_orginal: "http://www.youtube.com/watch?v=coq9klG41R8"

Which is weird, considering a) that yesterday I got an empty url[], and that b) even if now I get what apparently looks like a normal youtube-dl return, it only contains the 3 first lines, and I cannot see any video file in the specified path... Any ideas ?

Translate answered 1/3, 2013 at 8:7 Comment(2)
can you post the command string that is produced to download the video? (var_dump($string));Constrain
I edited my question with new info, thank you for your help !Translate
A
10

exec reads only stdout, so you're missing the error message on stderr, whatever that may be.

Use the following code to get stderr as well:

$url = 'http://www.youtube.com/watch?v=coq9klG41R8';
$template = '/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/' .
            'downloads/%(id)s.%(ext)s';
$string = ('youtube-dl ' . escapeshellarg($url) . ' -f 18 -o ' .
          escapeshellarg($template));

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin
   1 => array("pipe", "w"),  // stdout
   2 => array("pipe", "w"),  // stderr
);
$process = proc_open($string, $descriptorspec, $pipes);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$ret = proc_close($process);
echo json_encode(array('status' => $ret, 'errors' => $stderr,
                       'url_orginal'=>$url, 'output' => $stdout,
                       'command' => $string));

Most likely, you don't have permission to write to that directory. To test that theory, check whether

touch /var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/test

works. You can use chmod and chown to change permissions.

Artilleryman answered 1/3, 2013 at 11:17 Comment(6)
Thanks a lot ! The folder was in fact writable, but now I can see the error. It says "format not available for video", so I modified the $string so that it would only tell me the formats available : ('youtube-dl ' . escapeshellarg($url) . ' -F'), and then it tells me -F is not a valid parameter, when it actually is, isn't it ? BTW I tried that command in the ssh terminal, and got the list of available formats as expected, and the format n°18 was actually in that list.Translate
@Translate That sounds incredibly weird. When you're in your ssh terminal, are you logged in as the same user that php runs as? Also, what do you get for which youtube-dl in both session and php? Finally, check that ~/.config/youtube-dl.conf doesn't differ for php and you ssh session.Artilleryman
I log in as root in ssh which I don't think is the case for php. You were right, which youtube-dl returns /usr/local/bin/youtube-dl in ssh as root while it returns /usr/bin/youtube-dl in php. I guess the youtube-dl used by php is deprecated, but when I update via ssh, it updates the first one and not the second... Any pointers ? Thanks a lotTranslate
I also can't find any file named youtube-dl.conf, how can I access it ? I tried to update via the php script, by changing $string = 'youtube-dl -U';, but it says -U is not a valid parameter... So I'm kinda stuck here...Translate
Actually it works now, I had a version from 2009 in my /usr/bin, haha ! I guess the repo was not updated... So I installed the latest version for php as well and now it works great ! Thank you soooo much for the help, and of course for maintaining this amazing tool !!Translate
phihag have a look at my question plz #31675918Whale

© 2022 - 2024 — McMap. All rights reserved.