假设我有一个脚本,如下所示
import youtube_dl
options = {
"quiet": True,
"skip_download": True,
"forcetitle": True,
"forceurl": True,
"format": "bestaudio[ext=m4a]/best[ext=mp4]/best[ext=webm]/best"}
with youtube_dl.YoutubeDL(options) as ydl:
metadata = ydl.extract_info(f"ytsearch1: Never gonna give you up Rick Astley")
title = metadata["entries"][0]["title"]
url = metadata["entries"][0]["formats"][0]["url"]
watch_id = metadata["entries"][0]["display_id"]
extension = metadata["entries"][0]["ext"]
print("\n")
print("Results")
print(f"title: {title}")
print(f"url: {url}")
# do other stuff
其输出显示如下
Rick Astley - Never Gonna Give You Up (Video)
https://r2---sn-nqojvhh-4nql.googlevideo.com/videoplayback?expire=1615178090&ei=ClVFYLXAB_Wmir4P4OmvsAc&ip=142.0.14.91&id=o-AI7PyUbW58VrMxM9IFNqTlw2BbD8mwMoV53-wSMcs_mb&itag=140&source=youtube&requiressl=yes&mh=7c&mm=31%2C29&mn=sn-nqojvhh-4nql%2Csn-qxoedn7z&ms=au%2Crdu&mv=m&mvi=2&pcm2cms=yes&pl=20&gcr=us&initcwndbps=1832500&vprv=1&mime=audio%2Fmp4&ns=1H8LUy6hbPlWLg1PHgmu0xUF&gir=yes&clen=3433514&dur=212.091&lmt=1608803698687376&mt=1615156117&fvip=4&keepalive=yes&fexp=24001374%2C24007246&c=WEB&txp=5411222&n=Bh0GIV8cVRIBUneuBU&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgYpoaNgPdxLh4inaXa9CrRiDglT_DKUS3uEn4Qku-uVwCIQD6nELqExsGMNZQoRqEpnU669WCHXqCfM35K-TfQtrqHw%3D%3D&sig=AOq0QJ8wRgIhAOtlhCnk7UdstxOQBiTZ29aK4EYyjKwXcf4hmGwxiBBOAiEA0bTm2mBy5Z41KKdYXCgWZbZ0S-S8jUaxyA4JXwYjG6g=
Results
title: Rick Astley - Never Gonna Give You Up (Video)
url: https://r2---sn-nqojvhh-4nql.googlevideo.com/videoplayback?expire=1615178090&ei=ClVFYLXAB_Wmir4P4OmvsAc&ip=142.0.14.91&id=o-AI7PyUbW58VrMxM9IFNqTlw2BbD8mwMoV53-wSMcs_mb&itag=249&source=youtube&requiressl=yes&mh=7c&mm=31%2C29&mn=sn-nqojvhh-4nql%2Csn-qxoedn7z&ms=au%2Crdu&mv=m&mvi=2&pcm2cms=yes&pl=20&gcr=us&initcwndbps=1832500&vprv=1&mime=audio%2Fwebm&ns=1H8LUy6hbPlWLg1PHgmu0xUF&gir=yes&clen=1232526&dur=212.061&lmt=1578935068173432&mt=1615156117&fvip=4&keepalive=yes&fexp=24001374%2C24007246&c=WEB&txp=5531432&n=Bh0GIV8cVRIBUneuBU&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgbLq604Q9G-SMe7ukgbbvLk3Fnml4eSYmuTxC37fCNUoCIHWnPYA70q5nZiDY0OgDbZHqTFIkjtLZBPJqV4i5bFXa&sig=AOq0QJ8wRQIgP1Nj9IqPpv4qVbPGf6edCJMipGq3pralKXL7ypBANGICIQDAjnJ8QTio0kl11edjg4Pk1ZzLfm-YOE2hJFPnQiUnbQ==
通过将
"quiet": True
传递到 ydl.extract_info
,我想我可以抑制所有输出。虽然这确实抑制了大部分输出,但我不想显示任何输出。ydl.extract_info
的 all输出?
感谢您的帮助!
这是一个小临时方案,只需导入 os 并在最后放置一行,说明 os.sys(“cls”) 或 os.system(“cls”)
伊索克:
with YoutubeDL({'quiet': True, 'no_warnings': True, 'verbose': False}) as ydl:
pass