Import youtube-dl for own script #242

Closed
opened 2026-02-20 21:06:38 -05:00 by deekerman · 13 comments
Owner

Originally created by @orschiro on GitHub (Mar 14, 2012).

Hello,

It's not really regarded as an issue but I'd really appreciate a slight point of help in the right direction.

Learning and understanding Python a bit better I wanted to write a custom script that takes name and playlist URL out of a text file and should pass that to youtube-dl with the parameters 'cit'.

This is my small code snippet:

root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists'

def download():
files = open('Playlists.txt').readlines()
for playlist in files:
p = playlist.split(';')
# Now run youtube-dl, pass the URL from p[1] and the destination folder from p[0] which should reside in root_folder.

download()

Playlist.txt looks like this:

Acoustic;http://www.youtube.com/playlist?list=PL5547214A7D5E1A26&feature=view_all

How do I run youtube-dl from within my script?

Regards

Originally created by @orschiro on GitHub (Mar 14, 2012). Hello, It's not really regarded as an issue but I'd really appreciate a slight point of help in the right direction. Learning and understanding Python a bit better I wanted to write a custom script that takes name and playlist URL out of a text file and should pass that to youtube-dl with the parameters 'cit'. This is my small code snippet: root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists' def download(): files = open('Playlists.txt').readlines() for playlist in files: p = playlist.split(';') # Now run youtube-dl, pass the URL from p[1] and the destination folder from p[0] which should reside in root_folder. download() Playlist.txt looks like this: Acoustic;http://www.youtube.com/playlist?list=PL5547214A7D5E1A26&feature=view_all How do I run youtube-dl from within my script? Regards
Author
Owner

@FiloSottile commented on GitHub (Mar 14, 2012):

Please see #217 and #218
At the moment, you best shot is to use the subprocess python module and call the script as you would do from the command line.

@FiloSottile commented on GitHub (Mar 14, 2012): Please see #217 and #218 At the moment, you best shot is to use the `subprocess` python module and call the script as you would do from the command line.
Author
Owner

@orschiro commented on GitHub (Mar 15, 2012):

Thanks for your comment. That works nicely. However, how can I determine the flv video after it was downloaded and move it with shutil.move into my desired directory?

EDIT: I think I found a solution using glob.

Here is the code.

import shutil
import os
import sys
import subprocess
# Settings
root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/'

def download():
    files = open('Playlists.txt').readlines()

    for playlist in files:
        p = playlist.split(';')

        # Create the directory for the playlist if it does not exist yet
        my_path = os.path.join(root_folder, p[0])
        if not os.path.exists (my_path):
            os.makedirs(my_path)

        # Download every single video from the given playlist
        download_videos = subprocess.Popen([sys.executable, 'youtube-dl.py', ['-cit'], [p[1]]])        
        download_videos.wait()

       # After downloading all videos move them into the destination folder
       # Use glob to get only those with flv ending
       for videos in glob.glob("*.flv"):
           shutil.move(videos, my_path)

download()
@orschiro commented on GitHub (Mar 15, 2012): Thanks for your comment. That works nicely. However, how can I determine the flv video after it was downloaded and move it with shutil.move into my desired directory? EDIT: I think I found a solution using glob. Here is the code. ``` import shutil import os import sys import subprocess # Settings root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/' def download(): files = open('Playlists.txt').readlines() for playlist in files: p = playlist.split(';') # Create the directory for the playlist if it does not exist yet my_path = os.path.join(root_folder, p[0]) if not os.path.exists (my_path): os.makedirs(my_path) # Download every single video from the given playlist download_videos = subprocess.Popen([sys.executable, 'youtube-dl.py', ['-cit'], [p[1]]]) download_videos.wait() # After downloading all videos move them into the destination folder # Use glob to get only those with flv ending for videos in glob.glob("*.flv"): shutil.move(videos, my_path) download() ```
Author
Owner

@FiloSottile commented on GitHub (Mar 15, 2012):

Hi, glad to hear that it is working.
I would warn you against that approach because not all videos are downloaded in flv format (see "format" section of the README) and that way you have to make assumptions on your current working directory (no other flv...).
One way might be to use the -o option. You will also want to follow the API-fication at #152.

Anyway, I moved your code to a gist and I am modifying it to read the destination from the program output. That would be a far more robust approach.
https://gist.github.com/2047687

I look forward to you publishing your work, feel free to ask if in need.

@FiloSottile commented on GitHub (Mar 15, 2012): Hi, glad to hear that it is working. I would warn you against that approach because not all videos are downloaded in flv format (see "format" section of the README) and that way you have to make assumptions on your current working directory (no other flv...). One way might be to use the `-o` option. You will also want to follow the API-fication at #152. Anyway, I moved your code to a gist and I am modifying it to read the destination from the program output. That would be a far more robust approach. https://gist.github.com/2047687 I look forward to you publishing your work, feel free to ask if in need.
Author
Owner

@FiloSottile commented on GitHub (Mar 15, 2012):

Done on https://gist.github.com/2047687
Take a look, I use the output of def report_destination

@FiloSottile commented on GitHub (Mar 15, 2012): Done on https://gist.github.com/2047687 Take a look, I use the output of `def report_destination`
Author
Owner

@orschiro commented on GitHub (Mar 16, 2012):

Thanks for your input!

I came up with another idea. Why not downloading the videos in the directory of the playlist?

This would solve the problem as well.

Have a look at the version here: https://github.com/orschiro/YYTubeGrabber/blob/master/yytubegrabber.py

Still a bit messy is that I have to copy youtube-dl into every playlist directory. Better would be to check if it is installed before doing the copy process. I do not know how to check that though.

Furthermore a kind of synchronisation with the playlist would be nice. Hence, delete the videos that are no longer part of the playlist on YouTube. However, I guess that's impossible to check.

Regards

@orschiro commented on GitHub (Mar 16, 2012): Thanks for your input! I came up with another idea. Why not downloading the videos in the directory of the playlist? This would solve the problem as well. Have a look at the version here: https://github.com/orschiro/YYTubeGrabber/blob/master/yytubegrabber.py Still a bit messy is that I have to copy youtube-dl into every playlist directory. Better would be to check if it is installed before doing the copy process. I do not know how to check that though. Furthermore a kind of synchronisation with the playlist would be nice. Hence, delete the videos that are no longer part of the playlist on YouTube. However, I guess that's impossible to check. Regards
Author
Owner

@orschiro commented on GitHub (Mar 28, 2012):

I have to come back to my issue.

Current code: https://github.com/orschiro/YYTubeGrabber/blob/master/yytubegrabber.py
Playlist to fetch from: https://github.com/orschiro/YYTubeGrabber/blob/master/Playlists.txt

In this case only Playlist Lessons should be fetched since the others are commented out.

However, after downloading all videos from that playlist youtube-dl continues to download videos which are not even on that playlist but belong to my uploads of my account. I cannot reproduce that behaviour.

Any ideas why this happens?

@orschiro commented on GitHub (Mar 28, 2012): I have to come back to my issue. Current code: https://github.com/orschiro/YYTubeGrabber/blob/master/yytubegrabber.py Playlist to fetch from: https://github.com/orschiro/YYTubeGrabber/blob/master/Playlists.txt In this case only Playlist Lessons should be fetched since the others are commented out. However, after downloading all videos from that playlist youtube-dl continues to download videos which are not even on that playlist but belong to my uploads of my account. I cannot reproduce that behaviour. Any ideas why this happens?
Author
Owner

@FiloSottile commented on GitHub (Mar 29, 2012):

You are affected by #277 because you've taken youtube-dl.py before 7151f63a5f.
You should consider automatically downloading the youtube-dl script from
https://raw.github.com/rg3/youtube-dl/master/youtube-dl

See urllib.urlretrieve

@FiloSottile commented on GitHub (Mar 29, 2012): You are affected by #277 because you've taken youtube-dl.py before 7151f63a5f3820a322ba8bf61eebe8d9f75d6ee5. You should consider automatically downloading the youtube-dl script from https://raw.github.com/rg3/youtube-dl/master/youtube-dl See [urllib.urlretrieve](http://docs.python.org/library/urllib.html#urllib.urlretrieve)
Author
Owner

@orschiro commented on GitHub (Mar 29, 2012):

And I was believing my code is somehow wrong. Thanks for that hint, it works flawlessly now!

Regards,

Robert

@orschiro commented on GitHub (Mar 29, 2012): And I was believing my code is somehow wrong. Thanks for that hint, it works flawlessly now! Regards, Robert
Author
Owner

@cryzed commented on GitHub (Mar 29, 2012):

Hey there orschiro! I think I've actually written exactly what you are searching for: batch-youtube-dl.py. I'm not claiming that my code is perfect but maybe you can use or possibly learn something from it.

A possible use case would be having a directory structure similar to this: YouTube/<Author>/<Playlist>/batch-file.txt for each author or playlist you want to download and then running $ batch-youtube-dl.py --arguments="--auto-number" in the directory you want to recursively scan for the batch files and download the videos contained in the playlist/video URLs within. Adding the --auto-number argument is of course completely optional, but it makes sense in case you are downloading a playlist and want to play back the downloaded contents in the correct order.

Please note that the script requires you to have the youtube-dl.py in the same directory.

@cryzed commented on GitHub (Mar 29, 2012): Hey there orschiro! I think I've actually written exactly what you are searching for: [batch-youtube-dl.py](https://github.com/cryzed/Sandbox/blob/master/batch-youtube-dl.py). I'm not claiming that my code is perfect but maybe you can use or possibly learn something from it. A possible use case would be having a directory structure similar to this: `YouTube/<Author>/<Playlist>/batch-file.txt` for each author or playlist you want to download and then running `$ batch-youtube-dl.py --arguments="--auto-number"` in the directory you want to recursively scan for the batch files and download the videos contained in the playlist/video URLs within. Adding the `--auto-number` argument is of course completely optional, but it makes sense in case you are downloading a playlist and want to play back the downloaded contents in the correct order. Please note that the script requires you to have the `youtube-dl.py` in the same directory.
Author
Owner

@orschiro commented on GitHub (Mar 30, 2012):

Wow nice. I'll definitely take this as inspiration since my grabber is just a project to learn understanding Python better. Thanks!

@orschiro commented on GitHub (Mar 30, 2012): Wow nice. I'll definitely take this as inspiration since my grabber is just a project to learn understanding Python better. Thanks!
Author
Owner

@np1 commented on GitHub (May 19, 2013):

Hi, I realise this is an old thread but I came here a few days ago looking for a solution to the same problem. Not content with using subprocess I wrote a small api for youtube downloads. I would have contributed it directly to this project but the codebase is hard for me to follow! Please feel free to steal / improve / correct / criticise etc!
https://github.com/np1/pafy

@np1 commented on GitHub (May 19, 2013): Hi, I realise this is an old thread but I came here a few days ago looking for a solution to the same problem. Not content with using subprocess I wrote a small api for youtube downloads. I would have contributed it directly to this project but the codebase is hard for me to follow! Please feel free to steal / improve / correct / criticise etc! https://github.com/np1/pafy
Author
Owner

@evamvid commented on GitHub (Jun 24, 2014):

Thanks! This is exactly what I was hoping for here...

@evamvid commented on GitHub (Jun 24, 2014): Thanks! This is exactly what I was hoping for here...
Author
Owner

@jaimeMF commented on GitHub (Jan 26, 2015):

This has been possible for some time, it's documented in the README. If you have any problem/suggestion feel free to open a new issue.
Thanks for the report!

@jaimeMF commented on GitHub (Jan 26, 2015): This has been possible for some time, [it's documented in the README](https://github.com/rg3/youtube-dl/blob/master/README.md#embedding-youtube-dl). If you have any problem/suggestion feel free to open a new issue. Thanks for the report!
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#242
No description provided.