2014-10-21 10 views
5

c'è qualche documentazione per usare youtube-dl come libreria python in un progetto?documentazione libreria youtube-dl python

So che posso usare la classe principale, ma non riesco a trovare alcuna documentazione o esempio ...

import youtube_dl 

ydl = youtube_dl.YoutubeDL(params) 

... ? 

risposta

4

Se si scarica una versione da github è possibile generare sfinge-doc, che è utile per lo sviluppo. Poi funzione di aiuto utilizzando pitoni di solito dà una certa idea di quale sia lo scopo di una funzione è

>>> import youtube_dl as yt 
>>> help(yt) 

Inoltre trovo ipython un utile strumento per esplorare il codice utilizzando la magia %edit.

%edit yt.main 

porterebbe direttamente alla fonte della funzione principale.

2

domanda simile: How to use youtube-dl from a python program

Controllare questo file nelle fonti: https://github.com/rg3/youtube-dl/blob/master/youtube_dl/__init__.py

È necessario opzioni dict (originariamente generati utilizzando parametri ricevuti dalla riga di comando):

ydl_opts = { 
    'usenetrc': opts.usenetrc, 
    'username': opts.username, 
    'password': opts.password, 
    # ... all options list available in sources 
    'exec_cmd': opts.exec_cmd, 
} 

e quindi creare YoutubeDL esempio e chiama alcuni metodi con nomi auto-descritti:

with YoutubeDL(ydl_opts) as ydl: 
    ydl.print_debug_header() 
    ydl.add_default_info_extractors() 

    # PostProcessors 
    # Add the metadata pp first, the other pps will copy it 
    if opts.addmetadata: 
     ydl.add_post_processor(FFmpegMetadataPP()) 
    if opts.extractaudio: 
     ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites)) 
    if opts.recodevideo: 
     ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo)) 
    if opts.embedsubtitles: 
     ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat)) 
    if opts.xattrs: 
     ydl.add_post_processor(XAttrMetadataPP()) 
    if opts.embedthumbnail: 
     if not opts.addmetadata: 
      ydl.add_post_processor(FFmpegAudioFixPP()) 
     ydl.add_post_processor(AtomicParsleyPP()) 


    # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way. 
    # So if the user is able to remove the file before your postprocessor runs it might cause a few problems. 
    if opts.exec_cmd: 
     ydl.add_post_processor(ExecAfterDownloadPP(
      verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd)) 

    # Update version 
    if opts.update_self: 
     update_self(ydl.to_screen, opts.verbose) 

    # Remove cache dir 
    if opts.rm_cachedir: 
     ydl.cache.remove() 

    # Maybe do nothing 
    if (len(all_urls) < 1) and (opts.load_info_filename is None): 
     if not (opts.update_self or opts.rm_cachedir): 
      parser.error(u'you must provide at least one URL') 
     else: 
      sys.exit() 

    try: 
     if opts.load_info_filename is not None: 
      retcode = ydl.download_with_info_file(opts.load_info_filename) 
     else: 
      retcode = ydl.download(all_urls) 
    except MaxDownloadsReached: 
     ydl.to_screen(u'--max-download limit reached, aborting.') 
     retcode = 101 
+1

Si prega di non copiare tutto __init __. Py'; la maggior parte è di pochissimo uso. Invece, si prega di seguire la documentazione ufficiale per questo scopo, come illustrato [nella mia risposta] (http://stackoverflow.com/a/27495114/35070). – phihag

3

official documentation now contains a section sull'incorporazione di youtube-dl. Sei sulla strada giusta;

import youtube_dl 

with youtube_dl.YoutubeDL({}) as ydl: 
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc']) 

è un buon programma minimo.