2014-10-21 19 views
5

có tài liệu nào để sử dụng youtube-dl làm thư viện python trong dự án không?Tài liệu thư viện python youtube-dl

tôi biết rằng tôi có thể sử dụng các lớp học chính, nhưng tôi không thể tìm thấy bất kỳ tài liệu hoặc ví dụ ...

import youtube_dl 

ydl = youtube_dl.YoutubeDL(params) 

... ? 

Trả lời

4

Nếu bạn tải một phiên bản từ github bạn có thể tạo nhân sư-docs đó là hữu ích để phát triển. Sau đó sử dụng con trăn giúp đỡ chức năng thường đưa ra một số ý tưởng gì mục đích của một hàm là

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

Hơn nữa tôi thấy ipython một công cụ hữu ích để khám phá mã sử dụng %edit kỳ diệu.

%edit yt.main 

sẽ đưa bạn trực tiếp đến nguồn của chức năng chính.

2

câu hỏi tương tự: How to use youtube-dl from a python program

Kiểm tra tập tin này trong các nguồn: https://github.com/rg3/youtube-dl/blob/master/youtube_dl/__init__.py

Bạn cần lựa chọn dict (ban đầu được tạo ra sử dụng các thông số nhận được từ dòng lệnh):

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

và sau đó tạo YoutubeDL dụ và gọi một số phương pháp có tên tự mô tả:

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

Vui lòng không sao chép tất cả '__init __. Py'; hầu hết nó là rất ít sử dụng. Thay vào đó, hãy làm theo các tài liệu chính thức cho mục đích này, như được trình bày [trong câu trả lời của tôi] (http://stackoverflow.com/a/27495114/35070). – phihag

3

official documentation now contains a section về cách nhúng youtube-dl. Bạn đang đi đúng hướng;

import youtube_dl 

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

là chương trình tối thiểu tốt.

Các vấn đề liên quan