当urllib无法下载时,QMediaContent如何能够显示视频?

2024-09-29 23:19:01 发布

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

我有一个Qt应用程序,可以通过以下方式显示URL中的视频:

player = QMediaPlayer()
...
player.setMedia(QMediaContent(QUrl(video.url)))
...

但无法使用与urllib.request相同的url下载视频,响应代码始终为200,但Content-Length为零

from urllib.request import urlopen, Request
rq = Request(video.url)
rp = urlopen(rq)
rp.headers["Content-Length"] # always 0

当im无法下载时,Qt如何显示视频

MWE

from PyQt5.QtWidgets import QApplication
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtCore import QUrl
from urllib.request import urlopen
import sys

class Test(QVideoWidget):
    def __init__(self, *args, **kwargs):
        QVideoWidget.__init__(self, *args, **kwargs)

        self.player = QMediaPlayer()
        self.player.setVideoOutput(self)
        self.player.mediaStatusChanged.connect(self.statusChanged)

        self.url = "https://api16-normal-c-useast1a.tiktokv.com/aweme/v1/play/?video_id=v09044190000brfkq160bkbi3ui1oko0&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&logo_name=tiktok_m&quality_type=11&source=PackSourceEnum_AWEME_DETAIL"
        self.player.setMedia(QMediaContent(QUrl(self.url)))


    def statusChanged(self, status):
        if status == QMediaPlayer.LoadedMedia:
            self.player.play()
        elif status == QMediaPlayer.EndOfMedia:
            self.player.play()

    
    def download(self):
        rp = urlopen(self.url)
        if int(rp.headers["Content-Length"]) != 0:
            with open("test.mp4", "wb") as mp4:
                while True:
                    chunk = rp.read(1024)
                    if not chunk: break
                    mp4.write(chunk)
        else:
            raise Exception("Content-Length is Zero")



if __name__ == "__main__":
    app = QApplication(sys.argv)
    test = Test()
    test.show()
    # uncomment to download
    # test.download()
    sys.exit(app.exec_())

Tags: fromtestimportselfurl视频ifcontent
1条回答
网友
1楼 · 发布于 2024-09-29 23:19:01

经过几次尝试,我发现您必须设置“用户代理”。Qt不直接处理QMediaPlayer请求,而是在Linux上处理像gstreamer这样的后端,这些后端设置了用户代理,使其正常工作

我还发现,你不应该成为像“Mozilla/5.0…”这样的浏览器的用户代理,这些浏览器可能会被拒绝作为一种保护手段

from urllib.request import Request, urlopen

# ...

class Test(QVideoWidget):
    # ...
    def download(self):
        rq = Request(self.url, headers={"user-agent": "MyApp/1.0"})
        with urlopen(rq) as rp:
            with open("test.mp4", "wb") as mp4:
                while True:
                    chunk = rp.read(1024)
                    if not chunk:
                        break
                    mp4.write(chunk)

相关问题 更多 >

    热门问题