如何从 python 程序使用 youtube-dl? [英] How to use youtube-dl from a python program?

查看:61
本文介绍了如何从 python 程序使用 youtube-dl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问以下shell命令的结果,

I would like to access the result of the following shell command,

youtube-dl -g "www.youtube.com/..."

从python程序中将其输出direct url打印到文件中.这是我尝试过的:

to print its output direct url to a file, from within a python program. This is what I have tried:

import youtube-dl
fromurl="www.youtube.com/..."
geturl=youtube-dl.magiclyextracturlfromurl(fromurl)

这可能吗?我试图了解源代码中的机制但迷路了:youtube_dl/__init__.pyyoutube_dl/youtube_DL.pyinfo_extractors ...

Is that possible? I tried to understand the mechanism in the source but got lost: youtube_dl/__init__.py, youtube_dl/youtube_DL.py, info_extractors ...

推荐答案

这并不困难 实际记录:

import youtube_dl

ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})

with ydl:
    result = ydl.extract_info(
        'http://www.youtube.com/watch?v=BaW_jenozKc',
        download=False # We just want to extract the info
    )

if 'entries' in result:
    # Can be a playlist or a list of videos
    video = result['entries'][0]
else:
    # Just a video
    video = result

print(video)
video_url = video['url']
print(video_url)

这篇关于如何从 python 程序使用 youtube-dl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆