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

Import youtube-dl for own script #308

Closed
orschiro opened this issue Mar 14, 2012 · 13 comments
Closed

Import youtube-dl for own script #308

orschiro opened this issue Mar 14, 2012 · 13 comments

Comments

@orschiro
Copy link

@orschiro orschiro commented 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

@FiloSottile
Copy link
Collaborator

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

@orschiro
Copy link
Author

@orschiro orschiro commented 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()
@FiloSottile
Copy link
Collaborator

@FiloSottile 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.
https://gist.github.com/2047687

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

@FiloSottile
Copy link
Collaborator

@FiloSottile FiloSottile commented Mar 16, 2012

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

@orschiro
Copy link
Author

@orschiro orschiro commented 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
Copy link
Author

@orschiro orschiro commented 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?

@FiloSottile
Copy link
Collaborator

@FiloSottile FiloSottile commented Mar 29, 2012

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

@orschiro
Copy link
Author

@orschiro orschiro commented Mar 29, 2012

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

Regards,

Robert

@cryzed
Copy link
Contributor

@cryzed cryzed commented 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.

@orschiro
Copy link
Author

@orschiro orschiro commented 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!

@np1
Copy link

@np1 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!
https://github.com/np1/pafy

@evamvid
Copy link

@evamvid evamvid commented Jun 24, 2014

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

@jaimeMF
Copy link
Collaborator

@jaimeMF 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.