如何用python从youtube dl获取信息??
我正在如何用python从youtube dl获取信息??,python,youtube-dl,Python,Youtube Dl,我正在tkinter&python中为youtube dl制作API,需要知道: 如何从youtube dl实时获取信息(速度、完成百分比、文件大小等) 我试过: import subprocess def execute(command): process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) # Poll process for new output until finished
tkinter
&python
中为youtube dl制作API
,需要知道:
- 如何从youtube dl实时获取信息(速度、完成百分比、文件大小等)
import subprocess
def execute(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
# Poll process for new output until finished
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() != None:
break
sys.stdout.write(nextline.decode('utf-8'))
sys.stdout.flush()
output = process.communicate()[0]
exitCode = process.returncode
if (exitCode == 0):
return output
else:
raise ProcessException(command, exitCode, output)
execute("youtube-dl.exe www.youtube.com/watch?v=9bZkp7q19f0 -t")
从
但它必须等到下载完成后才能给我信息;也许有一种方法可以从youtube dl源代码中获取信息
子流程
;您可以像往常一样直接使用python模块;请参阅:
这需要下载源代码,而不仅仅是将应用程序安装到系统中子流程
;应添加以下参数:系统输出的最后一行
;我建议使用这个未经测试的简单示例:for i in execute("sudo apt-get update"):
print i
在任何情况下,都不要预先更新您的版本。尝试以下方法:
from youtube_dl import YoutubeDL
video = "http://www.youtube.com/watch?v=BaW_jenozKc"
with YoutubeDL(youtube_dl_opts) as ydl:
info_dict = ydl.extract_info(video, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
你现在可能已经知道了,但它可能会帮助其他人。我知道这是一个老问题,但youtube dl有一些钩子,你可以很容易地实时提取这些信息 文件:
progress_hooks: A list of functions that get called on download
progress, with a dictionary with the entries
* status: One of "downloading", "error", or "finished".
Check this first and ignore unknown values.
If status is one of "downloading", or "finished", the
following properties may also be present:
* filename: The final filename (always present)
* tmpfilename: The filename we're currently writing to
* downloaded_bytes: Bytes on disk
* total_bytes: Size of the whole file, None if unknown
* total_bytes_estimate: Guess of the eventual file size,
None if unavailable.
* elapsed: The number of seconds since download started.
* eta: The estimated time in seconds, None if unknown
* speed: The download speed in bytes/second, None if
unknown
* fragment_index: The counter of the currently
downloaded video fragment.
* fragment_count: The number of fragments (= individual
files that will be merged)
Progress hooks are guaranteed to be called at least once
(with status "finished") if the download is successful.
如何使用它:
def下载功能(url):
ydl_选项={
...
“进度挂钩”:[可调用挂钩],
...
}
使用youtube_dl.YoutubeDL(ydl_选项)作为ydl:
下载([url])
def可调用挂钩(响应):
如果响应[“状态”]=“下载”:
速度=响应[“速度”]
下载百分比=(响应[“下载的字节数”]*100)/响应[“总字节数”]
...
你能用一个可运行的代码更新你的帖子吗?@Payam30已经有一段时间了,但我认为这段代码可以编译(至少在2015年是这样)。不过我看到有一个导入语句缺失了。我的意思是速度、下载百分比等可以是:video_url=info_dict['requested_formats'][0]['url']audio_url=info_dict['requested_formats'][1]['url']@Payam30只需将youtube_dl_opts={}定义为空字典即可。
from youtube_dl import YoutubeDL
video = "http://www.youtube.com/watch?v=BaW_jenozKc"
with YoutubeDL(youtube_dl_opts) as ydl:
info_dict = ydl.extract_info(video, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
progress_hooks: A list of functions that get called on download
progress, with a dictionary with the entries
* status: One of "downloading", "error", or "finished".
Check this first and ignore unknown values.
If status is one of "downloading", or "finished", the
following properties may also be present:
* filename: The final filename (always present)
* tmpfilename: The filename we're currently writing to
* downloaded_bytes: Bytes on disk
* total_bytes: Size of the whole file, None if unknown
* total_bytes_estimate: Guess of the eventual file size,
None if unavailable.
* elapsed: The number of seconds since download started.
* eta: The estimated time in seconds, None if unknown
* speed: The download speed in bytes/second, None if
unknown
* fragment_index: The counter of the currently
downloaded video fragment.
* fragment_count: The number of fragments (= individual
files that will be merged)
Progress hooks are guaranteed to be called at least once
(with status "finished") if the download is successful.