Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

youtube-dl in with PHP script #1925

Closed
avignat opened this issue Dec 9, 2013 · 12 comments
Closed

youtube-dl in with PHP script #1925

avignat opened this issue Dec 9, 2013 · 12 comments

Comments

@avignat
Copy link

@avignat avignat commented Dec 9, 2013

Hi, I'm creating a PHP script that use youtube-dl

if(isset($_GET['url']))
{
    $url = 'https://www.youtube.com/watch?v=GMuZdN84PJg';
    $cmd = 'youtube-dl -o /srv/http/youtube/$(title)s-%(uploader)s.$(ext)s" ' . escapeshellarg($url);
    exec($cmd, $output, $ret);
    echo 'output: ';
    var_export($output);
    echo "\nret: ";
    var_export($ret);
}

But I always get this output: array ( ) ret: 1
Permission are normaly correct

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Dec 9, 2013

My PHP knowledge is really really limited but it seems that you're missing the leading " in the -o option, shouldn't it be '-o "/srv/http/youtube/$(title)s-%(uploader)s.$(ext)s" '?

@avignat
Copy link
Author

@avignat avignat commented Dec 9, 2013

--' (shame)

@avignat avignat closed this Dec 9, 2013
@phihag
Copy link
Contributor

@phihag phihag commented Dec 9, 2013

Apart from the missing quote, you need to use percent, not dollar signs in the filename format. You may also want to escape the pattern, as in

$cmd = 'youtube-dl -o ' . escapeshellarg('/srv/http/youtube/%(title)s-%(uploader)s.%(ext)s') . ' ' . escapeshellarg($url);
@avignat
Copy link
Author

@avignat avignat commented Dec 9, 2013

<?php
     if(isset($_GET['url']) && !empty($_GET['url']))
     {
         $cmd = 'youtube-dl -o ' . escapeshellarg('/srv/http/youtube/%(title)s-%(uploader)s.%(ext)s') . ' ' . escapeshellarg($url);
         exec($cmd, $output, $ret);
         echo 'Output : ';
         var_export($output);
         echo "\nReturn : ";
         var_export($ret);
    }
    else{
         echo '<form action="youtube.php" method="get"><label>Lien de la video : </label><input type="text" id="url" name="url"><input type="submit" value="Telecharger"></form>';
    }
 ?>

With this code I also get this : Output : array ( ) Return value : 1
PHP is executed with http group and the group can write in.
drwxrwxr-x 2 p1rox http 4.0K Dec 9 18:10 youtube/

@avignat avignat reopened this Dec 9, 2013
@phihag
Copy link
Contributor

@phihag phihag commented Dec 9, 2013

Can you post the stderr output that you get? Simply end cmd with 2>&1. Note that if you want to seriously call a command-line application in php, you should use proc_open in lieu of exec.

@avignat
Copy link
Author

@avignat avignat commented Dec 9, 2013

Maybe but I don't know how to use proc_open and if I look to the PHP doc for this that seems complicated to use just for a little script that download youtube videos.

And with 2&>1 nothing new append.

@phihag
Copy link
Contributor

@phihag phihag commented Dec 9, 2013

Not receiving anything on stderr is highly suspicious, there seems to be something fundamental wrong there. Just to verify, what output do you get for

$cmd = 'youtube-dl_php_api 2>&1';

Also, can you post the entire code you're using somewhere, if possible reduced to a directly runable example?

The following code works fine for me and will always output something, even if youtube-dl is changed to fail horribly:

<?php
$url = 'http://www.youtube.com/watch?v=BaW_jenozKc';
$cmd = 'youtube-dl -o ' . escapeshellarg('/srv/http/youtube/%(title)s-%(uploader)s.%(ext)s') . ' ' . escapeshellarg($url) . ' 2>&1 ';
exec($cmd, $output, $ret);
echo 'Output : ';
var_export($output);
echo "\nReturn : ";
var_export($ret);
@avignat
Copy link
Author

@avignat avignat commented Dec 9, 2013

I get this : Output : array ( 0 => 'sh: youtube-dl_php_api: command not found', ) Return value : 127

Entire code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Youtube-dl</title>
    </head>
    <body>
<?php
    if(isset($_GET['url']) && !empty($_GET['url']))
    {
        $cmd = 'youtube-dl_php_api 2>&1';

        //        $cmd = 'youtube-dl -o ' . escapeshellarg('/srv/http/youtube/%(title)s-%(uploader)s.%(ext)s') . ' ' . escapeshellarg($url) . ' &2>1';
        exec($cmd, $output, $ret);
        echo 'Output : ';
        var_export($output);
        echo "\nReturn value : ";
        var_export($ret);
    }
    else{
        echo '<form action="youtube.php" method="get">
            <label>Lien de la video : </label><input type="text" id="url" name="url"><input type="submit" value="Telecharger">
            </form>';
    }
?>
    </body>
</html>

@phihag
Copy link
Contributor

@phihag phihag commented Dec 9, 2013

There is a typo in the commented youtube-dl line. It should end in 2>&1, not in &1>2. Unfortunately, both are valid expressions, but with completely different meanings. Can you post the output you get with the fixed youtube-dl line?

Sorry, that was my mistake in the original post.

@avignat
Copy link
Author

@avignat avignat commented Dec 9, 2013

Answer :
Output : array ( 0 => 'WARNING: Assuming --restrict-filenames since file system encoding cannot encode all charactes. Set the LC_ALL environment variable to fix this.', 1 => 'WARNING: The url doesn't specify the protocol, trying with http', 2 => 'Traceback (most recent call last):', 3 => ' File "/usr/sbin/youtube-dl", line 9, in ', 4 => ' load_entry_point('youtube-dl==2013.12.08.1', 'console_scripts', 'youtube-dl')()', 5 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/init.py", line 726, in main', 6 => ' _real_main(argv)', 7 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/init.py", line 716, in _real_main', 8 => ' retcode = ydl.download(all_urls)', 9 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/YoutubeDL.py", line 835, in download', 10 => ' self.extract_info(url)', 11 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/YoutubeDL.py", line 446, in extract_info', 12 => ' return self.process_ie_result(ie_result, download, extra_info)', 13 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/YoutubeDL.py", line 480, in process_ie_result', 14 => ' extra_info=extra_info)', 15 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/YoutubeDL.py", line 430, in extract_info', 16 => ' ie_result = ie.extract(url)', 17 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/extractor/common.py", line 134, in extract', 18 => ' return self._real_extract(url)', 19 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/extractor/generic.py", line 149, in _real_extract', 20 => ' new_url = self._test_redirect(url)', 21 => ' File "/usr/lib/python3.3/site-packages/youtube_dl/extractor/generic.py", line 131, in test_redirect', 22 => ' response = opener.open(HeadRequest(url))', 23 => ' File "/usr/lib/python3.3/urllib/request.py", line 467, in open', 24 => ' req = meth(req)', 25 => ' File "/usr/lib/python3.3/urllib/request.py", line 1172, in do_request', 26 => ' raise URLError('no host given')', 27 => 'urllib.error.URLError: ', ) Return value : 1

@phihag
Copy link
Contributor

@phihag phihag commented Dec 9, 2013

Thanks, that clears up a lot. Your URL is invalid. You can add the -v flag to youtube-dl to see it in the output. I believe you are simply missing $url = $_GET['url']; in your code.

Also, you want to define an LC_ALL environment variable. Without it, youtube-dl cannot know the filesystem charset and will use a very limited character set. Simply start $cmd with $cmd = 'LC_ALL=en_US.UTF-8 youtube-dl ...' (or a suitable other encoding, depending on your preferences).

@phihag phihag closed this Dec 9, 2013
@avignat
Copy link
Author

@avignat avignat commented Dec 9, 2013

Thanks, PEBKAC for me --'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.