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

Use youtube-dl in a PhP script. #711

Closed
doctorbinsibar opened this issue Feb 24, 2013 · 23 comments
Closed

Use youtube-dl in a PhP script. #711

doctorbinsibar opened this issue Feb 24, 2013 · 23 comments
Labels
php

Comments

@doctorbinsibar
Copy link

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

For a Script that i want to develop i want to download youtube videos from a PHP script.
If i call it with shell_exec(); it doesnt work.

youtube-dl --max-quality=FLV "https://www.youtube.com/watch?v=hGgr8ZbeNgQ"
i call this in shell browser works fine.

I make this in php script doesnt work:
echo exec('youtube-dl --max-quality=FLV "https://www.youtube.com/watch?v=hGgr8ZbeNgQ" ');

Please help

@glisignoli
Copy link
Contributor

@glisignoli glisignoli commented Feb 24, 2013

This isn't an issue with youtube-dl. Also, why do you have an "echo" in the front of that line.
Make sure youtube-dl has excutable rights by whatever process is calling it.
Make sure you use the full path to youtube-dl 'just-in-case'
Output of you apache-error as well

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

shell_exec('cd /home/ftp8/public_html/temp && /usr/local/bin/youtube-dl --max-quality FLV "https://www.youtube.com/watch?v=GMuZdN84PJg');

in php doesnt work any solution for this ?

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

how you run youtube-dl from php ? you use it in scripts if yes give me the command

@phihag
Copy link
Contributor

@phihag phihag commented Feb 24, 2013

Thank your for reporting this issue, @doctorbinsibar . In your second example, you're missing a double quote at the end. Most likelky, that's the cause of the problem.

Can you copy and paste the actual code (or even better, upload it) you're using? And it would help immensely if you'd describe doesn't work in detail - what does the program output, what do you see in php's error_log, and what on your filesystem?

For reference, this program:

<?php
$url = 'https://www.youtube.com/watch?v=GMuZdN84PJg';
$cmd = 'youtube-dl -o "/home/phihag/div/tmp/%(id)s.%(ext)s" ' . escapeshellarg($url);
exec($cmd, $output, $ret);
echo 'output: ';
var_export($output);
echo "\nret: ";
var_export($ret);

works fine on my system (with php 5.4.4), and outputs:

output: array (
  0 => '[youtube] Setting language',
  1 => '[youtube] GMuZdN84PJg: Downloading video webpage',
  2 => '[youtube] GMuZdN84PJg: Downloading video info webpage',
  3 => '[youtube] GMuZdN84PJg: Extracting video information',
  4 => '[download] Destination: /home/phihag/div/tmp/GMuZdN84PJg.flv',
[download] 100.0% of 1.48M at   86.11k/s ETA 00:00',
)
ret: 0
@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

its a simple script that i have build for experiments.
I have a form where i add youtube url and at this moment i test it and check if the video is in the folder but on every test i run it looks on the frontend as it worked, when i check the video folder its empty.

must youtube-dl be the same folder as the script ?

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

now look at this : http://ftp8.org/tes.php

This php file has only this inside:

$cmd = 'youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" --max-quality flv" "https://www.youtube.com/watch?v=GMuZdN84PJg"';
shell_exec($cmd);

the video must downloaded to http://ftp8.org/temp/

no luck !

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

$cmd = 'youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" --max-quality flv https://www.youtube.com/watch?v=GMuZdN84PJg';
shell_exec($cmd);

Working with Shell absolutly fine but in php no chance

when i use it in puddy
youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" --max-quality flv https://www.youtube.com/watch?v=GMuZdN84PJg

working nice !!!

@phihag
Copy link
Contributor

@phihag phihag commented Feb 24, 2013

$cmd = 'youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" --max-quality flv" "https://www.youtube.com/watch?v=GMuZdN84PJg"';
shell_exec($cmd);

will just output that. php files with php code in them must start with <?php. Please copy and paste the entire file verbatim.

In your first code example, you've got one quote too much, right after flv.

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

<?php
$cmd = 'youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" --max-quality flv https://www.youtube.com/watch?v=GMuZdN84PJg';
shell_exec($cmd);
?>

Run this File: http://ftp8.org/tes.php (its the above code)

Temp Folder chmod 777
http://ftp8.org/temp/

@phihag
Copy link
Contributor

@phihag phihag commented Feb 24, 2013

@doctorbinsibar It looks like youtube-dl is not in your PATH. What output do you get if you replace youtube-dl with /usr/local/bin/youtube-dl?

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

output: array ( ) ret: 1

Error Log:

[24-Feb-2013 17:23:31 Canada/Central] PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20100525/ffmpeg.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20100525/ffmpeg.so: cannot open shared object file: No such file or directory in Unknown on line 0

added PHP Info on the Page anything missing for this command ?
http://ftp8.org/tes.php

@phihag
Copy link
Contributor

@phihag phihag commented Feb 24, 2013

That looks a lot better; youtube-dl is being executed now.

Without the full output one would get with proc_open, I can just guess, but I'd be guessing that your version of Python is not current. Have you installed Python 2.6, 2.7, 3.3, or newer? What do you get when you set

<?php
$cmd = `/usr/bin/env python --version`
passthru($cmd);
@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 24, 2013

Python 2.6.6 just installed it because of youtube-dl

i have added your last code about python version in the php file, looks like the same problem.
But that dont happens when i use wget other other shell commands.

@phihag
Copy link
Contributor

@phihag phihag commented Feb 24, 2013

@doctorbinsibar What exact output do you get when you run

<?php
$cmd = `/usr/bin/env python --version`
passthru($cmd);

on your server?

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 25, 2013

hope you can help me out the third part is now to reach under
http://ftp8.org/tes.php

<?php
//test script
$cmd = '/usr/local/bin/youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" --max-quality flv https://www.youtube.com/watch?v=GMuZdN84PJg';
echo exec($cmd);

// your example script
$url = 'https://www.youtube.com/watch?v=GMuZdN84PJg';
$cmd = '/usr/local/bin/youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" ' . escapeshellarg($url);
exec($cmd, $output, $ret);
echo 'output: ';
var_export($output);
echo "\nret: ";
var_export($ret);


//Test Python version If i put this in shell command via putty the output is:
// Python 2.6.6
$cmd = '/usr/bin/env python --version';
passthru($cmd);
?>
@phihag
Copy link
Contributor

@phihag phihag commented Feb 25, 2013

Quite obviously, that's not the output of the script you posted - it ends with Hello World! instead of a Python number.

But let's stop guessing and modify the example script to include stderr output:

<?php
$url = 'https://www.youtube.com/watch?v=GMuZdN84PJg';
$cmd = 'youtube-dl -o "/home/phihag/div/tmp/%(id)s.%(ext)s" ' . escapeshellarg($url);

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin
   1 => array("pipe", "w"),  // stdout
   2 => array("pipe", "w"),  // stderr
);
$process = proc_open($cmd, $descriptorspec, $pipes);
echo "\nstdout: \n";
var_export(stream_get_contents($pipes[1]));
fclose($pipes[1]);
echo "\nstderr: \n";
var_export(stream_get_contents($pipes[2]));
fclose($pipes[2]);
$ret = proc_close($process);
echo "\nret: ";
var_export($ret);
echo "\n";

Modify the path in $cmd as needed.

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 25, 2013

output: array ( ) ret: 1 stdout: '' stderr: 'sh: youtube-dl: command not found ' ret: 127
$cmd = 'youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" ' . escapeshellarg($url);

if i change the $cmd to
$cmd = '/usr/local/bin/youtube-dl -o "/home/ftp8/public_html/temp/%(id)s.%(ext)s" ' . escapeshellarg($url);

stdout: '' stderr: 'sys:1: DeprecationWarning: Non-ASCII character \'\\xcc\' in file /usr/local/bin/youtube-dl on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details File "/usr/local/bin/youtube-dl", line 2 PK���Ì·ÙTe“Ï mµÐÍ�ÊØ†à¤ó¶K�v“g¼b��š–�»­ó�[å0åŠeys;�·k°Uа�éxÓòL̺�³t]A—¦�ŠTдfmÚ\\7 {U²uÕ�¦- ·¨ÎD�ªn² ^ SyntaxError: invalid syntax ' ret: 1

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 25, 2013

updated python to 3.3
reinstalled youtube-dl

here is the output:

stdout: '' stderr: 'sys:1: DeprecationWarning: Non-ASCII character '\xcc' in file /usr/local/bin/youtube-dl on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details File "/usr/local/bin/youtube-dl", line 2 PK���Ì·ÙTe“Ï mµÐÍ�ÊØ†à¤ó¶K�v“g¼b��š–�»­ó�[å0åŠeys;�·k°Uа�éxÓòL̺�³t]A—¦�ŠTдfmÚ\7 {U²uÕ�¦- ·¨ÎD�ªn² ^ SyntaxError: invalid syntax ' ret: 1

@phihag
Copy link
Contributor

@phihag phihag commented Feb 25, 2013

Ah, thanks, that's precisely what I guessed: You have an old version of Python. If you add

$cmd = '/usr/bin/env python --version'

you'll see that your webserver's Python is not your user's. But fret not, you can simply call

/usr/bin/python3.3 /usr/local/bin/youtube-dl -o ....

that should work, as long as your Python is installed in /usr/bin/python3.3

@ankita1990
Copy link

@ankita1990 ankita1990 commented Feb 26, 2013

hey @phihag

i also have an issue related to youtube-dl

My issue is that youtube-dl is perfectly download video from all the mentioned websites except YOUTUBE.

Some days ago youtube-dl works flawless but from past 4 days it doesn't download videos from youtube.

i used your code, written @ #711 (comment)

and i got output is given below:-

stdout: '[youtube] Setting language [youtube] mnUvNeUqh_E: Downloading video webpage ' stderr: 'WARNING: unable to set language: HTTP Error 402: Payment Required ERROR: unable to download video webpage: HTTP Error 402: Payment Required ' ret: 1

can you please explain... why i got this message and what it means...??

Thanks in advance

@phihag
Copy link
Contributor

@phihag phihag commented Feb 26, 2013

@ankita1990 As mentioned in the FAQ, that message means that youtube blocked your IP address for excessive connections. Are you, by any chance, using a downloader such as axel, or downloading thousands of videos? In any case, as documented in the FAQ feel free to propose code to solve the CAPTCHA (i.e. show it to the user in some way / give it to a program that does that). You may also want to use multiple IP addresses.

@phihag
Copy link
Contributor

@phihag phihag commented Feb 26, 2013

@doctorbinsibar Has updating to a newer Python/calling youtube-dl with that Python version solved your problem?

@doctorbinsibar
Copy link
Author

@doctorbinsibar doctorbinsibar commented Feb 26, 2013

thank you phihag, the problem was solved after using the path of the python file

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
4 participants
You can’t perform that action at this time.