Import youtube-dl for own script #308

orschiro opened this Issue Mar 14, 2012 · 13 comments


None yet
6 participants


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.


Playlist.txt looks like this:


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



FiloSottile commented 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.

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):

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

       # 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)


FiloSottile commented 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.

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


FiloSottile commented Mar 16, 2012

Done on
Take a look, I use the output of def report_destination

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:

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.


I have to come back to my issue.

Current code:
Playlist to fetch from:

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?


FiloSottile commented Mar 29, 2012

You are affected by #277 because you've taken before 7151f63.
You should consider automatically downloading the youtube-dl script from

See urllib.urlretrieve

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




cryzed commented Mar 29, 2012

Hey there orschiro! I think I've actually written exactly what you are searching for: 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 $ --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 in the same directory.

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

np1 commented 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!

evamvid commented Jun 24, 2014

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


jaimeMF commented 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 jaimeMF closed this Jan 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment