Repository navigation
Expand file tree
/
Copy pathrender.py
More file actions
159 lines (152 loc) · 6.21 KB
/
Copy pathrender.py
File metadata and controls
159 lines (152 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import sys,pygame
from PySide6.QtWidgets import (
QApplication, QMainWindow, QVBoxLayout, QWidget,QHBoxLayout,QSlider,
QPushButton, QLabel, QStackedWidget, QFileDialog, QMessageBox, QListWidget, QScrollArea
)
from PySide6.QtGui import (
QImage,QPixmap
)
from PySide6.QtCore import (
QSize,Qt,QUrl,QTimer
)
from moviepy.editor import VideoFileClip,AudioFileClip
from PySide6.QtMultimedia import (
QMediaFormat,QMediaPlayer,QAudioOutput,QSoundEffect
)
# f = QMediaFormat()
# print(QMediaFormat.supportedFileFormats(f,QMediaFormat.Decode))
#plan: after done, change to video part into a new class, leave the original video part with a button instead
# which shows pop-out window of playing the video.
class MediaPlayer(QWidget):
def __init__(self,flashnum,answer=False):
super().__init__()
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.renderflashcard(flashnum,answer)
def renderfile(self,flashnum,answer=False):
"""
Takes in flashcard number (starting from 1)
Choose to render question(False) or answer(True)
Returns a QVBoxLayout that contains the flashcard question and true/false indicating media
"""
with open("./current/metadata.txt", "r", encoding="utf-8") as f:
try:
if answer:
self.fileloc = "./current/flashcards/"+f.readlines()[flashnum+1][0:-1].split(":")[1]
else:
self.fileloc = "./current/flashcards/"+f.readlines()[flashnum+1][0:-1].split(":")[0]
except:
self.label = QLabel(" ") ## doesn't exist?
self.scroll = QScrollArea()
self.scroll.setFixedHeight(300)
self.scroll.setWidget(self.label)
return self.scroll
try:
extension = self.fileloc[-4:].lower()
print("moved on",extension,self.fileloc)
if extension == ".txt":
with open(self.fileloc,"r") as f:
text = "".join(f.readlines())
print("rendering text:", text)
self.label = QLabel(text)
self.scroll = QScrollArea()
self.scroll.setFixedHeight(300)
self.scroll.setWidget(self.label)
return self.scroll
elif extension in (".gif,.jpg,.jpeg,.png"):
self.label = QLabel()
pixmap = QPixmap(self.fileloc)
self.size = QSize()
self.size.setHeight(300)
self.size.setWidth(500)
pixmap = pixmap.scaled(self.size,Qt.KeepAspectRatio)
self.label.setPixmap(pixmap)
print("rendering image!")
return self.label
elif extension in (".mp4,.avi,.mov"): ##video
self.playing = QPushButton("Play The Video")
self.playing.setMinimumHeight(300)
self.playing.clicked.connect(self.playvideo)
return self.playing
elif extension in (".mp3, .wav"):
self.playing = QPushButton("Play The Audio")
self.playing.setMinimumHeight(270)
self.playing.clicked.connect(self.playaudio)
self.stop_playing = QPushButton("Stop Playing")
self.stop_playing.clicked.connect(self.stopplay)
return self.playing,self.stop_playing
else: ##unknown type
self.label = QLabel(" ")
self.scroll = QScrollArea()
self.scroll.setFixedHeight(300)
self.scroll.setWidget(self.label)
return self.scroll
except Exception as e:
print(e)
self.label = QLabel(" ")
self.scroll = QScrollArea()
self.scroll.setFixedHeight(300)
self.scroll.setWidget(self.label)
return self.scroll ## no file exists?
def renderflashcard(self,flashnum,answer=False):
try:
f = open("./current/metadata.txt", "r", encoding="utf-8")
except:
print("Metadata doesn't exist yet! renderquestion() will need to be run again when flashcards are properly loaded.")
self.layout.addWidget(QLabel("No existing flashcard yet!"))
return self.layout
if flashnum > int(f.readlines()[1]):
self.layout.addWidget(QLabel("No existing flashcard yet!"))
return self.layout
try:
f.close()
except:
pass
if answer:
try:
desc = open("./current/flashcards/"+str(flashnum)+"_answer_des.txt")
text = "".join(desc.readlines())
self.layout.addWidget(QLabel(text))
desc.close()
except: ## no desc
pass
output = self.renderfile(flashnum,True)
self.layout.addWidget(output)
return self.layout
else:
try:
desc = open("./current/flashcards/"+str(flashnum)+"_des.txt")
text = "".join(desc.readlines())
self.layout.addWidget(QLabel(text))
desc.close()
except: ## no desc
pass
output = self.renderfile(flashnum)
#print(output)
try:
#print("variable 'output' has a datatype of:",type(output))
self.layout.addWidget(output)
except Exception as e:
print(e)
for components in output:
self.layout.addWidget(components)
def playvideo(self):
#print("button is clicked!")
pygame.init()
video = VideoFileClip(self.fileloc)
video.preview()
pygame.quit()
def playaudio(self):
self.player = QMediaPlayer()
self.audio = QAudioOutput()
self.player.setAudioOutput(self.audio)
self.player.setSource(QUrl.fromLocalFile(self.fileloc))
self.audio.setVolume(50)
self.player.play()
def stopplay(self):
self.player.stop()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MediaPlayer(2)
window.show()
sys.exit(app.exec())