【问题标题】:youtube-dl python library documentationyoutube-dl python 库文档
【发布时间】:2014-12-17 05:22:48
【问题描述】:

是否有任何文档可以在项目中将 youtube-dl 用作 python 库?

我知道我可以使用主类,但是我找不到任何文档或示例...

import youtube_dl

ydl = youtube_dl.YoutubeDL(params)

... ?

【问题讨论】:

  • 遗憾的是仍然没有 YoutubeDL.py 的文档

标签: python youtube-dl


【解决方案1】:

如果您从github 下载版本,您可以生成对开发很有用的 sphinx-docs。然后使用 python 的帮助函数通常可以了解函数的目的是什么

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

此外,我发现 ipython 是使用 %edit 魔法探索代码的有用工具。

%edit yt.main 

会直接带你到 main 函数的源代码。

【讨论】:

    【解决方案2】:

    类似问题:How to use youtube-dl from a python program

    在来源中检查此文件:https://github.com/rg3/youtube-dl/blob/master/youtube_dl/__init__.py

    你需要 options dict(最初使用从命令行接收的参数生成):

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

    然后创建YoutubeDL实例并调用一些自描述名称的方法:

    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
    

    【讨论】:

    • 请不要复制所有__init__.py;其中大部分用处很小。相反,请为此目的遵循官方文档,如 in my answer 所述。
    【解决方案3】:

    关于嵌入 youtube-dl 的 official documentation now contains a section。你走在正确的轨道上;

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

    是一个很好的最小程序。

    【讨论】:

      【解决方案4】:

      您可以使用main source code 中引用的这些选项

      另外一个官方的选项资源有一个简短的解释 -> HERE

         ydl_opts = {
              'usenetrc': opts.usenetrc,
              'username': opts.username,
              'password': opts.password,
              'twofactor': opts.twofactor,
              'videopassword': opts.videopassword,
              'ap_mso': opts.ap_mso,
              'ap_username': opts.ap_username,
              'ap_password': opts.ap_password,
              'quiet': (opts.quiet or any_getting or any_printing),
              'no_warnings': opts.no_warnings,
              'forceurl': opts.geturl,
              'forcetitle': opts.gettitle,
              'forceid': opts.getid,
              'forcethumbnail': opts.getthumbnail,
              'forcedescription': opts.getdescription,
              'forceduration': opts.getduration,
              'forcefilename': opts.getfilename,
              'forceformat': opts.getformat,
              'forcejson': opts.dumpjson or opts.print_json,
              'dump_single_json': opts.dump_single_json,
              'simulate': opts.simulate or any_getting,
              'skip_download': opts.skip_download,
              'format': opts.format,
              'listformats': opts.listformats,
              'outtmpl': outtmpl,
              'outtmpl_na_placeholder': opts.outtmpl_na_placeholder,
              'autonumber_size': opts.autonumber_size,
              'autonumber_start': opts.autonumber_start,
              'restrictfilenames': opts.restrictfilenames,
              'ignoreerrors': opts.ignoreerrors,
              'force_generic_extractor': opts.force_generic_extractor,
              'ratelimit': opts.ratelimit,
              'nooverwrites': opts.nooverwrites,
              'retries': opts.retries,
              'fragment_retries': opts.fragment_retries,
              'skip_unavailable_fragments': opts.skip_unavailable_fragments,
              'keep_fragments': opts.keep_fragments,
              'buffersize': opts.buffersize,
              'noresizebuffer': opts.noresizebuffer,
              'http_chunk_size': opts.http_chunk_size,
              'continuedl': opts.continue_dl,
              'noprogress': opts.noprogress,
              'progress_with_newline': opts.progress_with_newline,
              'playliststart': opts.playliststart,
              'playlistend': opts.playlistend,
              'playlistreverse': opts.playlist_reverse,
              'playlistrandom': opts.playlist_random,
              'noplaylist': opts.noplaylist,
              'logtostderr': opts.outtmpl == '-',
              'consoletitle': opts.consoletitle,
              'nopart': opts.nopart,
              'updatetime': opts.updatetime,
              'writedescription': opts.writedescription,
              'writeannotations': opts.writeannotations,
              'writeinfojson': opts.writeinfojson,
              'writethumbnail': opts.writethumbnail,
              'write_all_thumbnails': opts.write_all_thumbnails,
              'writesubtitles': opts.writesubtitles,
              'writeautomaticsub': opts.writeautomaticsub,
              'allsubtitles': opts.allsubtitles,
              'listsubtitles': opts.listsubtitles,
              'subtitlesformat': opts.subtitlesformat,
              'subtitleslangs': opts.subtitleslangs,
              'matchtitle': decodeOption(opts.matchtitle),
              'rejecttitle': decodeOption(opts.rejecttitle),
              'max_downloads': opts.max_downloads,
              'prefer_free_formats': opts.prefer_free_formats,
              'verbose': opts.verbose,
              'dump_intermediate_pages': opts.dump_intermediate_pages,
              'write_pages': opts.write_pages,
              'test': opts.test,
              'keepvideo': opts.keepvideo,
              'min_filesize': opts.min_filesize,
              'max_filesize': opts.max_filesize,
              'min_views': opts.min_views,
              'max_views': opts.max_views,
              'daterange': date,
              'cachedir': opts.cachedir,
              'youtube_print_sig_code': opts.youtube_print_sig_code,
              'age_limit': opts.age_limit,
              'download_archive': download_archive_fn,
              'cookiefile': opts.cookiefile,
              'nocheckcertificate': opts.no_check_certificate,
              'prefer_insecure': opts.prefer_insecure,
              'proxy': opts.proxy,
              'socket_timeout': opts.socket_timeout,
              'bidi_workaround': opts.bidi_workaround,
              'debug_printtraffic': opts.debug_printtraffic,
              'prefer_ffmpeg': opts.prefer_ffmpeg,
              'include_ads': opts.include_ads,
              'default_search': opts.default_search,
              'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
              'encoding': opts.encoding,
              'extract_flat': opts.extract_flat,
              'mark_watched': opts.mark_watched,
              'merge_output_format': opts.merge_output_format,
              'postprocessors': postprocessors,
              'fixup': opts.fixup,
              'source_address': opts.source_address,
              'call_home': opts.call_home,
              'sleep_interval': opts.sleep_interval,
              'max_sleep_interval': opts.max_sleep_interval,
              'external_downloader': opts.external_downloader,
              'list_thumbnails': opts.list_thumbnails,
              'playlist_items': opts.playlist_items,
              'xattr_set_filesize': opts.xattr_set_filesize,
              'match_filter': match_filter,
              'no_color': opts.no_color,
              'ffmpeg_location': opts.ffmpeg_location,
              'hls_prefer_native': opts.hls_prefer_native,
              'hls_use_mpegts': opts.hls_use_mpegts,
              'external_downloader_args': external_downloader_args,
              'postprocessor_args': postprocessor_args,
              'cn_verification_proxy': opts.cn_verification_proxy,
              'geo_verification_proxy': opts.geo_verification_proxy,
              'config_location': opts.config_location,
              'geo_bypass': opts.geo_bypass,
              'geo_bypass_country': opts.geo_bypass_country,
              'geo_bypass_ip_block': opts.geo_bypass_ip_block,
              # just for deprecation check
              'autonumber': opts.autonumber if opts.autonumber is True else None,
              'usetitle': opts.usetitle if opts.usetitle is True else None,
          }
      

      【讨论】:

        猜你喜欢
        • 2020-05-26
        • 2012-09-22
        • 1970-01-01
        • 2019-05-21
        • 1970-01-01
        • 2021-01-03
        • 2011-05-15
        • 2018-10-24
        • 2021-03-02
        相关资源
        最近更新 更多