Problem while running youtube-dl as a Python script #121

Closed
opened 2026-02-20 22:25:59 -05:00 by deekerman · 12 comments
Owner

Originally created by @samrat on GitHub (Sep 26, 2011).

When I try to run youtube-dl as a Python script using
python youtubedl.py
(youtube.dl.py is a Python file with youtube-dl's source code), the download completes successfully but the Python script fails to terminate. How can this problem be solved?

Originally created by @samrat on GitHub (Sep 26, 2011). When I try to run youtube-dl as a Python script using `python youtubedl.py` (youtube.dl.py is a Python file with youtube-dl's source code), the download completes successfully but the Python script fails to terminate. How can this problem be solved?
Author
Owner

@phihag commented on GitHub (Sep 26, 2011):

Sorry, I can't reproduce this problem on my machine. Can you include the full command-line which reproduces the problem? (Or does it occur when literally executing youtube-dl without arguments?) Also, a screenshot would be nice.

What operating system and Python version are you on?

@phihag commented on GitHub (Sep 26, 2011): Sorry, I can't reproduce this problem on my machine. Can you include the full command-line which reproduces the problem? (Or does it occur when literally executing youtube-dl without arguments?) Also, a screenshot would be nice. What operating system and Python version are you on?
Author
Owner

@samrat commented on GitHub (Sep 26, 2011):

phihad- python youtubedl.py -o 'filename.flv' http://youtube.com/watch?v=VIDEOID

As I mentioned before, the video downloads fully, its just that youtube-dl doesn't close, so that the flow of my program can continue.

@samrat commented on GitHub (Sep 26, 2011): phihad- python youtubedl.py -o 'filename.flv' http://youtube.com/watch?v=VIDEOID As I mentioned before, the video downloads fully, its just that youtube-dl doesn't close, so that the flow of my program can continue.
Author
Owner

@phihag commented on GitHub (Sep 26, 2011):

Sorry samrat, but I need a little more information than that. A screenshot would be nice, and it should also contain the output of

python youtubedl.py --version
python --version

For example, your above command shows the following result for me on debian sid (x64):

$ python youtubedl.py -o 'filename.flv' http://youtube.com/watch?v=VIDEOID
[youtube] Setting language
[youtube] VIDEOID: Downloading video webpage
ERROR: unable to download video webpage: HTTP Error 404: Not Found
$ # youtube-dl stopped here.

Also, if you're executing youtube-dl this in another program, could you share the relevant code portion? It might be a bug in there.

@phihag commented on GitHub (Sep 26, 2011): Sorry samrat, but I need a little more information than that. A screenshot would be nice, and it should also contain the output of ``` python youtubedl.py --version python --version ``` For example, your above command shows the following result for me on debian sid (x64): ``` $ python youtubedl.py -o 'filename.flv' http://youtube.com/watch?v=VIDEOID [youtube] Setting language [youtube] VIDEOID: Downloading video webpage ERROR: unable to download video webpage: HTTP Error 404: Not Found $ # youtube-dl stopped here. ``` Also, if you're executing youtube-dl this in another program, could you share the relevant code portion? It might be a bug in there.
Author
Owner

@samrat commented on GitHub (Sep 26, 2011):

Here's the code I used to execute youtube-dl:
download_command = "python2.7 youtubedl.py -o '../data/%s' %s;" % (filename, youtube_url) os.system(download_command)

and the output:

[youtube] Setting language
[youtube] rIkVeAb56Z4: Downloading video webpage
[youtube] rIkVeAb56Z4: Downloading video info webpage
[youtube] rIkVeAb56Z4: Extracting video information
[download] Destination: ../data/hello.flv
[download] 100.0% of 1.68M at   21.11k/s ETA 00:00 ```





@samrat commented on GitHub (Sep 26, 2011): Here's the code I used to execute youtube-dl: `download_command = "python2.7 youtubedl.py -o '../data/%s' %s;" % (filename, youtube_url) os.system(download_command)` and the output: `````` [youtube] Setting language [youtube] rIkVeAb56Z4: Downloading video webpage [youtube] rIkVeAb56Z4: Downloading video info webpage [youtube] rIkVeAb56Z4: Extracting video information [download] Destination: ../data/hello.flv [download] 100.0% of 1.68M at 21.11k/s ETA 00:00 ``` ``````
Author
Owner

@phihag commented on GitHub (Sep 26, 2011):

os.system executes youtube-dl in a subshell. In this subshell, an ampersand (&) in the URL makes youtube-dl run in the background. Instead, us the subprocess module.

import subprocess
subprocess.Popen(['python2.7', 'youtubedl.py', '-o','../data' + filename, youtube_url]).communicate()

You could also put the URL in quotes, but that will fail (and allow anyone who enters the URL shell access) if it contains quotes.

@phihag commented on GitHub (Sep 26, 2011): `os.system` executes youtube-dl in a subshell. In this subshell, an ampersand (`&`) in the URL makes youtube-dl run in the background. Instead, us the [subprocess](http://docs.python.org/library/subprocess.html#replacing-os-system) module. ``` import subprocess subprocess.Popen(['python2.7', 'youtubedl.py', '-o','../data' + filename, youtube_url]).communicate() ``` You could also put the URL in quotes, but that will fail (and allow anyone who enters the URL shell access) if it contains quotes.
Author
Owner

@samrat commented on GitHub (Sep 26, 2011):

phihag- I tried using subprocess too, the problem still persists.

@samrat commented on GitHub (Sep 26, 2011): phihag- I tried using subprocess too, the problem still persists.
Author
Owner

@phihag commented on GitHub (Sep 26, 2011):

@samrat It's not about using subprocess in general, but the way you construct the command. Can you post an extract of the subprocess version of your code?

@phihag commented on GitHub (Sep 26, 2011): @samrat It's not about using subprocess in general, but the way you construct the command. Can you post an extract of the subprocess version of your code?
Author
Owner

@samrat commented on GitHub (Sep 26, 2011):

def download(youtube_url): filename = 'hello.flv' download_command = "python2.7 youtubedl.py -o '../data/%s' %s;" % (filename, youtube_url.encode('utf8')) args = shlex.split(download_command) subprocess.Popen(args).communicate() return filename

@samrat commented on GitHub (Sep 26, 2011): `def download(youtube_url): filename = 'hello.flv' download_command = "python2.7 youtubedl.py -o '../data/%s' %s;" % (filename, youtube_url.encode('utf8')) args = shlex.split(download_command) subprocess.Popen(args).communicate() return filename`
Author
Owner

@phihag commented on GitHub (Sep 26, 2011):

@samrat This variant has the exact same problem, only with spaces instead of the ampersand in the URL. Why are you constructing the command as a string instead of a list, as I suggested?

Also, can you reproduce the problem in an interactive shell? If not, can you give us (or upload) a small demo program that reproduces the issue without requiring any user input?

@phihag commented on GitHub (Sep 26, 2011): @samrat This variant has the exact same problem, only with spaces instead of the ampersand in the URL. Why are you constructing the command as a string instead of a list, as I suggested? Also, can you reproduce the problem in an interactive shell? If not, can you give us (or [upload](http://pastebin.com)) a small demo program that reproduces the issue without requiring any user input?
Author
Owner

@samrat commented on GitHub (Sep 26, 2011):

@phihag- Thank you, constructing the command as a list seems to solve the problem.

@samrat commented on GitHub (Sep 26, 2011): @phihag- Thank you, constructing the command as a list seems to solve the problem.
Author
Owner

@ghost commented on GitHub (Nov 19, 2012):

Can you please post how you solved the issue?

@ghost commented on GitHub (Nov 19, 2012): Can you please post how you solved the issue?
Author
Owner

@phihag commented on GitHub (Nov 19, 2012):

@Anilmorab See my previous comment for the solution.

@phihag commented on GitHub (Nov 19, 2012): @Anilmorab See [my previous comment](https://github.com/rg3/youtube-dl/issues/177#issuecomment-2199223) for the solution.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/youtube-dl-ytdl-org#121
No description provided.