我正在尝试播放存储在 PYQT5 应用程序的 Azure Blob 存储中的视频。我不想让它在本地下载。我做了很多搜索,发现你可以执行 download_blob().read() 或 readall() 或 readinto() ,这允许你将其读入内存,而无需下载实际文件。
我尝试使用 .read() .readall() 和 readinto() 但不完全确定我在做什么。我希望用户在选择视频后能够立即观看该视频。我尝试了 IOBytes 但没有成功。我还尝试使用 PYQT MediaContent 小部件并尝试 set_media()。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import QBuffer, QUrl
from azure.storage.blob import BlobServiceClient
class VideoPlayerWindow(QMainWindow):
def __init__(self):
super().__init__()
# Initialize Azure Blob Storage client
connection_string = "DefaultEndpointsProtocol=https;AccountNameN/GrGcA==;EndpointSuffix=core.windows.net"
self.blob_service_client = BlobServiceClient.from_connection_string(connection_string)
self.container_name = "testing"
self.video_path = "video/short-test-92734b6d9f2e/out_0.mp4"
self.setWindowTitle("Azure Video Player")
self.setGeometry(100, 100, 800, 600)
# Create layout
layout = QVBoxLayout()
# Create video widget
self.video_widget = QVideoWidget()
layout.addWidget(self.video_widget)
# Create play button
self.play_button = QPushButton("Play")
self.play_button.clicked.connect(self.play_video)
layout.addWidget(self.play_button)
# Set layout
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
# Initialize video player
self.media_player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.media_player.setVideoOutput(self.video_widget)
def play_video(self):
# Get video blob from Azure Blob Storage
blob_client = self.blob_service_client.get_blob_client(container=self.container_name, blob=self.video_path)
print("WAIT")
# Download video content into memory
# video_data = blob_client.download_blob().readall()
print("WAIT")
# Create a buffer from the video data
# buffer = QBuffer(video_data)
# buffer.open(QBuffer.ReadOnly)
videoUrl = QUrl.fromLocalFile('azureBlobVolume/out_0.mp4')
mediaContent = QMediaContent(videoUrl)
self.media_player.setMedia(mediaContent)
# Set media content to media player and play
# self.media_player.setMedia(QMediaContent(), buffer)
self.media_player.play()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = VideoPlayerWindow()
window.show()
sys.exit(app.exec_())
这是我正在使用的代码。正如您在上面看到的,我也尝试从缓冲区读取视频。
我正在尝试播放存储在 Azure Blob 存储中的视频 在我的 PYQT5 应用程序中。我不想让它在本地下载。
您可以使用下面的代码播放 Azure Blob 存储中的视频文件,而无需在 PYQT5 应用程序中将其本地下载。
代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import QBuffer, QUrl
class VideoPlayerWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Azure Video Player")
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
self.video_widget = QVideoWidget()
layout.addWidget(self.video_widget)
self.play_button = QPushButton("Play")
self.play_button.clicked.connect(self.play_video)
layout.addWidget(self.play_button)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.media_player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.media_player.setVideoOutput(self.video_widget)
def play_video(self):
# Blob URL + ?SAS token (if your storage account container is private)
blob_url ="https://venkat789.blob.core.windows.net/demo/sample-file-sd.mp4?sp=r&st=2024-02-21T06:56:05Z&se=2024-02-21T14:56:05Z&spr=https&sv=2022-11-02&sr=b&sig=FcQr%2B5jJssI8Ijxxxxxxxx"
print("WAIT")
print("WAIT")
content = QMediaContent(QUrl.fromUserInput(blob_url))
self.media_player.setMedia(content)
self.media_player.play()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = VideoPlayerWindow()
window.show()
sys.exit(app.exec_())
上面的代码播放带有 SAS 令牌的 URL 中的视频。它使用 URL 创建一个 QMediaContent 对象并将其设置为媒体播放器。最后,它使用媒体播放器播放视频。
输出: