将图像作为临时文件下载并将其设置为PixMap
我正在下载YouTube视频缩略图并将其保存为tempfile。然后,我从那个tempfile中得到一个pixmap,后来我可以将其设置为标签,它将显示为YouTube视频缩略图。 我的问题是,相同的方法对YouTube #Shorts缩略图不起作用,它不会引发任何错误,但标签将是空的。我不知道为什么
这是YouTube视频缩略图图像URL的一个示例:
url =“ https://i.ytimg.com/vi/mtn1ynol46q/hqdefault.jpg?sqp = -oaymwejcoadei4cfryequln> LF_ZTYFJ1I9VW9HQEM5D5D5ZL9Q “
这是YouTube短路缩略图图像URL:
url =“ https://i.ytimg.com/vi/vt_7ubaf3vc/2.jpg”
这是代码,适用于YouTube视频缩略图,但不适合短裤,
from PyQt5.QtGui import QIcon, QPixmap
from urllib.request import urlopen
import base64
import tempfile
url = .......
image = base64.b64encode(urlopen(url).read()).decode("ascii")
imgdata = base64.b64decode(image)
with tempfile.NamedTemporaryFile(mode="wb") as img:
img.write(imgdata)
pixmap = QPixmap(img.name)
我该如何使其适用于YouTube短裤图像URL?
I'm downloading a YouTube video thumbnail and saving it as a tempfile. I'm then getting a pixmap out of that tempfile and later I can set it to a label, it will appear for the YouTube video thumbnail.
My problem is that the same method won't work for YouTube #shorts thumbnail, it wont throw any error but the label will be empty. I dont know why
This is an example of a YouTube video thumbnail image url:
url = "https://i.ytimg.com/vi/MtN1YnoL46Q/hqdefault.jpg?sqp=-oaymwEjCOADEI4CSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDlF_ZTyfJ1i9Vw9HqEm5d5D5zL9Q"
This is an example of a YouTube shorts thumbnail image url:
url = "https://i.ytimg.com/vi/VT_7UBAf3Vc/2.jpg"
This is the code that works for the YouTube video thumbnails but not for the shorts
from PyQt5.QtGui import QIcon, QPixmap
from urllib.request import urlopen
import base64
import tempfile
url = .......
image = base64.b64encode(urlopen(url).read()).decode("ascii")
imgdata = base64.b64decode(image)
with tempfile.NamedTemporaryFile(mode="wb") as img:
img.write(imgdata)
pixmap = QPixmap(img.name)
How can I make it work for YouTube shorts image URLs too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用urllib.parse模块从URL末端删除参数/args。
use the urllib.parse module to remove the parameters/args from the end of the url.