Skip to content
This repository

Import youtube-dl for own script #308

Open
orschiro opened this Issue · 12 comments

5 participants

Robert Orzanna Filippo Valsorda cryzed nagev Evamvid Sharma
Robert Orzanna

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

Filippo Valsorda
Collaborator

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.

Robert Orzanna

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()
Filippo Valsorda
Collaborator

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.

Filippo Valsorda
Collaborator

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

Robert Orzanna

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

Robert Orzanna

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?

Filippo Valsorda
Collaborator

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

See urllib.urlretrieve

Robert Orzanna

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

Regards,

Robert

cryzed

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.

Robert Orzanna

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

nagev
np1 commented

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

Evamvid Sharma

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.