s3manager-gtk/videoplayer.py

155 lines
5.3 KiB
Python

import sys
import time
import threading
from PyQt6.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QSlider
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtCore import QUrl, Qt, QEvent
class VideoPlayer(QMainWindow):
def __init__(self):
super().__init__()
self.fswindow = 0
self.dclick = False
self.setWindowTitle("Video Player")
self.setGeometry(100, 100, 640, 480)
#self.showFullScreen()
self.media_player = QMediaPlayer(None)
self.video_widget = QVideoWidget()
self.installEventFilter(self)
self.start_button = QPushButton("▷||")
self.start_button.setFixedSize(33, 33)
self.start_button.clicked.connect(self.start_video)
self.stop_button = QPushButton("")
self.stop_button.setFixedSize(33, 33)
self.stop_button.clicked.connect(self.stop_video)
self.slider = QSlider(Qt.Orientation.Horizontal)
self.slider.sliderMoved.connect(self.set_position)
self.media_player.setVideoOutput(self.video_widget)
self.media_player.setSource(QUrl("https://video.asgardius.company/download/videos/41bf7df4-8f6c-45e9-96ef-a8dd57c865c8-480.mp4"))
self.media_player.positionChanged.connect(self.position_changed)
self.media_player.durationChanged.connect(self.duration_changed)
self.audio_output = QAudioOutput()
self.audio_output.setVolume(100)
self.media_player.setAudioOutput(self.audio_output)
layout = QVBoxLayout()
controls = QHBoxLayout()
controls.addWidget(self.start_button)
controls.addWidget(self.stop_button)
controls.addWidget(self.slider)
layout.addWidget(self.video_widget)
layout.addLayout(controls)
#layout.addWidget(self.slider)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
#self.set_layout()
def set_layout(self):
self.video_widget = QVideoWidget()
self.start_button = QPushButton("▷||")
self.start_button.setFixedSize(60, 33)
self.start_button.clicked.connect(self.start_video)
self.stop_button = QPushButton("")
self.stop_button.setFixedSize(60, 33)
self.stop_button.clicked.connect(self.stop_video)
self.slider = QSlider(Qt.Orientation.Horizontal)
self.slider.sliderMoved.connect(self.set_position)
self.media_player.setVideoOutput(self.video_widget)
layout = QVBoxLayout()
controls = QHBoxLayout()
controls.addWidget(self.start_button)
controls.addWidget(self.stop_button)
controls.addWidget(self.slider)
layout.addWidget(self.video_widget)
layout.addLayout(controls)
#layout.addWidget(self.slider)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def start_video(self):
if self.media_player.isPlaying():
self.media_player.pause()
#self.start_button.setText("Play")
else:
self.media_player.play()
#self.start_button.setText("Pause")
def stop_video(self):
self.media_player.stop()
def set_position(self, position):
if not self.fswindow:
self.media_player.setPosition(position)
def position_changed(self, position):
if not self.fswindow:
self.slider.setValue(position)
def duration_changed(self, duration):
if not self.fswindow:
self.slider.setRange(0, duration)
def toglefs(self):
if not self.fswindow == 0:
self.showNormal()
if self.fswindow == 1:
self.showMaximized()
self.set_layout()
self.fswindow = 0
else:
if self.isMaximized():
self.fswindow = 1
#print("maximized")
else:
self.fswindow = 2
self.showFullScreen()
self.setCentralWidget(self.video_widget)
def dclickthr(self):
self.dclick = True
#print(self.dclick)
time.sleep(0.5)
self.dclick = False
#print(self.dclick)
def eventFilter(self, obj, event):
if event.type() == 3:
if self.dclick:
self.toglefs()
else:
dclickth = threading.Thread(target=self.dclickthr, name="Double Click")
dclickth.start()
#print(event.type())
elif event.type() == QEvent.Type.KeyPress:
if event.key() == 83:
self.media_player.stop()
elif event.key() == 68:
self.media_player.setPosition(self.media_player.position()+5000)
elif event.key() == 65:
self.media_player.setPosition(self.media_player.position()-5000)
elif event.key() == 87:
self.start_video()
elif event.key() == 70:
self.toglefs()
#print(event.key())
return True
if __name__ == "__main__":
app = QApplication(sys.argv)
video_player = VideoPlayer()
video_player.show()
sys.exit(app.exec())