s3manager-gtk/videoplayer.py

156 lines
5.3 KiB
Python
Raw Normal View History

2024-08-17 22:16:12 +02:00
import sys
2024-08-27 21:50:56 +02:00
import time
import threading
2024-08-17 22:16:12 +02:00
from PyQt6.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QSlider
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtMultimediaWidgets import QVideoWidget
2024-08-27 08:26:58 +02:00
from PyQt6.QtCore import QUrl, Qt, QEvent
2024-08-17 22:16:12 +02:00
class VideoPlayer(QMainWindow):
def __init__(self):
super().__init__()
2024-08-27 21:50:56 +02:00
self.fswindow = 0
self.dclick = False
2024-08-17 22:16:12 +02:00
self.setWindowTitle("Video Player")
self.setGeometry(100, 100, 640, 480)
#self.showFullScreen()
self.media_player = QMediaPlayer(None)
self.video_widget = QVideoWidget()
2024-08-27 08:26:58 +02:00
self.installEventFilter(self)
2024-08-17 22:16:12 +02:00
2024-09-09 21:03:09 +02:00
self.start_button = QPushButton("▷||")
self.start_button.setFixedSize(33, 33)
2024-08-17 22:16:12 +02:00
self.start_button.clicked.connect(self.start_video)
2024-09-09 21:03:09 +02:00
self.stop_button = QPushButton("")
self.stop_button.setFixedSize(33, 33)
2024-08-17 22:16:12 +02:00
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)
2024-08-18 00:19:47 +02:00
self.media_player.setSource(QUrl("https://video.asgardius.company/download/videos/41bf7df4-8f6c-45e9-96ef-a8dd57c865c8-480.mp4"))
2024-08-17 22:16:12 +02:00
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)
2024-09-09 21:03:09 +02:00
controls.addWidget(self.slider)
2024-08-17 22:16:12 +02:00
layout.addWidget(self.video_widget)
layout.addLayout(controls)
2024-09-09 21:03:09 +02:00
#layout.addWidget(self.slider)
2024-08-17 22:16:12 +02:00
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
2024-08-27 08:26:58 +02:00
#self.set_layout()
def set_layout(self):
self.video_widget = QVideoWidget()
2024-09-09 21:03:09 +02:00
self.start_button = QPushButton("▷||")
2024-08-27 08:26:58 +02:00
self.start_button.setFixedSize(60, 33)
self.start_button.clicked.connect(self.start_video)
2024-09-09 21:03:09 +02:00
self.stop_button = QPushButton("")
2024-08-27 08:26:58 +02:00
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)
2024-09-09 21:03:09 +02:00
controls.addWidget(self.slider)
2024-08-27 08:26:58 +02:00
layout.addWidget(self.video_widget)
layout.addLayout(controls)
2024-09-09 21:03:09 +02:00
#layout.addWidget(self.slider)
2024-08-27 08:26:58 +02:00
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
2024-08-17 22:16:12 +02:00
def start_video(self):
2024-09-09 21:03:09 +02:00
if self.media_player.isPlaying():
self.media_player.pause()
#self.start_button.setText("Play")
else:
self.media_player.play()
#self.start_button.setText("Pause")
2024-08-17 22:16:12 +02:00
def stop_video(self):
self.media_player.stop()
def set_position(self, position):
2024-08-27 08:26:58 +02:00
if not self.fswindow:
self.media_player.setPosition(position)
2024-08-17 22:16:12 +02:00
def position_changed(self, position):
2024-08-27 08:26:58 +02:00
if not self.fswindow:
self.slider.setValue(position)
2024-08-17 22:16:12 +02:00
def duration_changed(self, duration):
2024-08-27 08:26:58 +02:00
if not self.fswindow:
self.slider.setRange(0, duration)
2024-08-27 21:50:56 +02:00
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
2024-09-09 21:03:09 +02:00
#print("maximized")
2024-08-27 21:50:56 +02:00
else:
self.fswindow = 2
self.showFullScreen()
self.setCentralWidget(self.video_widget)
def dclickthr(self):
self.dclick = True
2024-09-09 21:03:09 +02:00
#print(self.dclick)
2024-08-27 21:50:56 +02:00
time.sleep(0.5)
self.dclick = False
2024-09-09 21:03:09 +02:00
#print(self.dclick)
2024-08-27 08:26:58 +02:00
2024-08-27 21:50:56 +02:00
def eventFilter(self, obj, event):
2024-08-27 08:26:58 +02:00
2024-08-27 21:50:56 +02:00
if event.type() == 3:
if self.dclick:
self.toglefs()
2024-08-27 08:26:58 +02:00
else:
2024-08-27 21:50:56 +02:00
dclickth = threading.Thread(target=self.dclickthr, name="Double Click")
dclickth.start()
#print(event.type())
2024-09-09 21:03:09 +02:00
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())
2024-08-27 08:26:58 +02:00
return True
2024-08-17 22:16:12 +02:00
if __name__ == "__main__":
app = QApplication(sys.argv)
video_player = VideoPlayer()
video_player.show()
sys.exit(app.exec())