YouTube-dl Python库文档

16

有没有关于如何在项目中使用youtube-dl作为Python库的文档?

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

import youtube_dl

ydl = youtube_dl.YoutubeDL(params)

... ?

4
很遗憾,目前还没有适用于YoutubeDL.py的文档。 - forzagreen
4个回答

9

9
类似的问题:如何在Python程序中使用youtube-dl 请查看源文件:https://github.com/rg3/youtube-dl/blob/master/youtube_dl/__init__.py 您需要 options 字典(最初由从命令行接收到的参数生成):
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

1
请不要复制整个 __init__.py 文件,因为其中大部分内容都没有什么用处。相反,请按照我的答案中所述的官方文档进行操作。 - anon

9
如果您从Github下载版本,则可以生成Sphinx-docs,这对开发非常有用。然后使用Python的帮助函数通常会给出一些关于函数目的的想法。
>>> import youtube_dl as yt
>>> help(yt)

此外,我发现使用 %edit 魔法命令来探索代码时,ipython 是一个有用的工具。
%edit yt.main 

会直接带您到主要功能的源头。

6

您可以使用这些选项,这些选项来自于主要源代码

另外还有一份官方的选项资源,并带有简短的说明->此处

   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,
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接