youtube dl python库文档

2024-04-27 21:47:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在项目中是否有使用youtube dl作为python库的文档?

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

import youtube_dl

ydl = youtube_dl.YoutubeDL(params)

... ?

Tags: 项目文档import示例youtubeparamsdlydl
3条回答

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

在源中检查此文件:^{}

您需要选项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

如果您从github下载一个版本,您可以生成sphinx文档,这对开发非常有用。然后使用pythons help函数通常可以知道函数的目的是什么

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

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

%edit yt.main 

将直接带您到主函数的源代码。

关于嵌入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'])

是一个很好的最小程序。

相关问题 更多 >