forked from WithHades/banfoStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainWindow.py
470 lines (413 loc) · 16.3 KB
/
mainWindow.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import math
import os.path
import shutil
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QMovie, QStandardItemModel, QStandardItem
from moviepy.video.VideoClip import ImageClip
from moviepy.video.io.VideoFileClip import VideoFileClip
from conf import DoutulaButton, BaiduButton
from utils import resizeImg
class clickedButton(QtWidgets.QPushButton):
"""
搜索表情包按钮事件,主要可以区分哪个按钮
"""
clicked = QtCore.pyqtSignal(int)
def __init__(self, button, parent=None):
super(clickedButton, self).__init__(parent)
self.button = button
def mouseReleaseEvent(self, QMouseEvent):
self.clicked.emit(self.button)
class clickedLabel(QtWidgets.QLabel):
"""
标签类,主要增加了点击事件,用于图片缓存区,用户点击了图片后可知道点击了哪张图
"""
clicked = QtCore.pyqtSignal(int)
def __init__(self, index: int, parent=None):
"""
:param index: 图片索引
:param parent:
"""
super(clickedLabel, self).__init__(parent)
self.index = index
def mouseReleaseEvent(self, QMouseEvent):
self.clicked.emit(self.index)
class Ui_MainWindow(object):
def __init__(self):
# 所有缓存的图片
self.img = []
# 当前视频预览区gif
self.gif = None
# 当前视频预览区图片
self.videoImg = None
def setupUi(self, MainWindow):
# 初始设置窗口信息
MainWindow.setObjectName('MainWindow')
MainWindow.resize(1890, 702)
MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
MainWindow.setWindowTitle('半佛风格视频生成')
self.mainWindow = MainWindow
self.centralwidget = QtWidgets.QWidget(MainWindow)
# 输入文件名标签
self.fileLabel = QtWidgets.QLabel(self.centralwidget)
self.fileLabel.setGeometry(QtCore.QRect(10, 23, 121, 16))
self.fileLabel.setText('导出视频名称: ')
# 文件名
self.filenName = QtWidgets.QLineEdit(self.centralwidget)
self.filenName.setGeometry(QtCore.QRect(140, 16, 361, 31))
self.filenName.setText('{}.mp4'.format(int(time.time())))
# 输入文件名确定按钮
self.filenameButton = QtWidgets.QPushButton(self.centralwidget)
self.filenameButton.setGeometry(QtCore.QRect(501, 16, 60, 31))
self.filenameButton.setText('设置')
self.filenameButton.clicked.connect(MainWindow.setFilename)
# 给输入框加个分组box
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(0, 53, 561, 618))
self.groupBox.setTitle('在此输入/编辑文案')
# 文案以表格的形式展示
self.model = QStandardItemModel(0, 0)
self.model.itemChanged.connect(MainWindow.tableItemChange)
# 设置水平方向四个头标签文本内容
self.model.setHorizontalHeaderLabels(['文案&字幕'])
self.row = 0
self.tableView = QtWidgets.QTableView(self.groupBox)
self.tableView.setGeometry(QtCore.QRect(10, 20, 541, 542))
self.tableView.setShowGrid(True)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.tableView.clicked.connect(MainWindow.jumpToIndex)
# 打开文案/前增一句/后增一句/删除一句/修改一句/导出文案按钮布局
self.tableButtonWidget = QtWidgets.QWidget(self.groupBox)
self.tableButtonWidget.setGeometry(QtCore.QRect(10, 561, 541, 55))
# 水平分布
self.hbox = QtWidgets.QHBoxLayout()
self.hbox.setGeometry(QtCore.QRect())
self.hbox.setContentsMargins(0, 0, 0, 0)
# 打开文案
self.openText = QtWidgets.QPushButton()
self.openText.setText('打开')
self.openText.clicked.connect(MainWindow.loadText)
self.hbox.addWidget(self.openText)
# 在前面增加一句
self.addFrontText = QtWidgets.QPushButton()
self.addFrontText.setText('前增一句')
self.addFrontText.clicked.connect(MainWindow.addFrontText)
self.hbox.addWidget(self.addFrontText)
# 在后面增加一句
self.addBehindText = QtWidgets.QPushButton()
self.addBehindText.setText('后增一句')
self.addBehindText.clicked.connect(MainWindow.addBehindText)
self.hbox.addWidget(self.addBehindText)
# 删除该句
self.delText = QtWidgets.QPushButton()
self.delText.setText('删除该句')
self.delText.clicked.connect(MainWindow.delText)
self.hbox.addWidget(self.delText)
# 导出文案
self.exportText = QtWidgets.QPushButton()
self.exportText.setText('导出文案')
self.exportText.clicked.connect(MainWindow.exportText)
self.hbox.addWidget(self.exportText)
self.tableButtonWidget.setLayout(self.hbox)
# 单句字幕
self.singleText = QtWidgets.QLineEdit(self.centralwidget)
self.singleText.setGeometry(QtCore.QRect(660, 620, 611, 50))
self.singleText.textChanged.connect(MainWindow.changeThePicText)
# 搜索框文本
self.searchText = QtWidgets.QLineEdit(self.centralwidget)
self.searchText.setGeometry(QtCore.QRect(1360, 17, 421, 41))
# 斗图啦/百度搜索按钮
self.searchDou = clickedButton(DoutulaButton, self.centralwidget)
self.searchDou.setGeometry(QtCore.QRect(1785, 17, 45, 41))
self.searchDou.setText('斗图')
self.searchDou.clicked.connect(MainWindow.search)
self.searchBai = clickedButton(BaiduButton, self.centralwidget)
self.searchBai.setGeometry(QtCore.QRect(1834, 17, 45, 41))
self.searchBai.setText('百度')
self.searchBai.clicked.connect(MainWindow.search)
# 视频背景图
self.videoBackgroud = QtWidgets.QLabel(self.centralwidget)
self.videoBackgroud.setGeometry(QtCore.QRect(560, 16, 800, 600))
self.videoBackgroud.setPixmap(QtGui.QPixmap('background.png'))
self.videoBackgroud.setScaledContents(True)
# 视频字幕
self.addSubtitleLayout()
# 上一句按钮
self.last = QtWidgets.QPushButton(self.centralwidget)
self.last.setGeometry(QtCore.QRect(560, 620, 101, 51))
self.last.setText('上一句')
self.last.clicked.connect(MainWindow.last)
# 下一句按钮
self.next = QtWidgets.QPushButton(self.centralwidget)
self.next.setGeometry(QtCore.QRect(1270, 620, 91, 51))
self.next.setText('下一句')
self.next.clicked.connect(MainWindow.next)
# 生成视频按钮
self.genVideo = QtWidgets.QPushButton(self.centralwidget)
self.genVideo.setGeometry(QtCore.QRect(1360, 620, 521, 51))
self.genVideo.setText('生成视频')
self.genVideo.clicked.connect(MainWindow.genVideo)
# 表情包图片位置布局
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(1349, 48, 540, 579))
self.topFiller = QtWidgets.QWidget()
self.scroll = QtWidgets.QScrollArea()
self.scroll.setWidget(self.topFiller)
self.vbox = QtWidgets.QVBoxLayout()
self.vbox.addWidget(self.scroll)
self.widget.setLayout(self.vbox)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def addSubtitleLayout(self) -> None:
"""
视频字幕设置布局
:return:
"""
# 视频字幕
self.videoText = QtWidgets.QLabel(self.centralwidget)
self.videoText.setGeometry(QtCore.QRect(560, 526, 801, 51))
# 视频字幕字体颜色
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
self.videoText.setPalette(palette)
# 视频字幕字体设置
font = QtGui.QFont()
font.setFamily('华文楷体')
font.setPointSize(20)
self.videoText.setFont(font)
self.videoText.setTextFormat(QtCore.Qt.PlainText)
self.videoText.setScaledContents(False)
self.videoText.setAlignment(QtCore.Qt.AlignCenter)
self.videoText.setWordWrap(False)
def delVideoImg(self) -> None:
"""
清空视频预览区图片
:return: None
"""
if self.videoImg is not None:
self.videoImg.deleteLater()
self.videoImg = None
if self.gif is not None:
self.gif.deleteLater()
self.gif = None
def getResizedOfVideoImg(self, path) -> (int, int):
"""
获取路径图片重新调整过的大小
:param path: 图片路径
:return: (width, height)
"""
try:
clip = VideoFileClip(path)
except:
clip = ImageClip(path)
width, height = clip.size
return resizeImg(width, height)
def changeVideoImg(self, path: str) -> None:
"""
加载图片到视频预览区
:param path: 图片路径
:return: None
"""
# 如果图片不是在tmp目录保存,则转到tmp目录再加载,不然重命名工程会有问题
if not path.startswith('tmp' + os.path.sep):
newPath = os.path.join('tmp', os.path.basename(path))
if not os.path.exists(newPath):
shutil.copyfile(path, newPath)
path = newPath
self.delVideoImg()
self.videoImg = QtWidgets.QLabel(self.centralwidget)
wight, height = self.getResizedOfVideoImg(path)
self.videoImg.setGeometry(QtCore.QRect(int(960 - wight / 2), int(316 - height / 2), wight, height))
self.videoImg.setScaledContents(True)
self.gif = QMovie(path)
self.videoImg.setMovie(self.gif)
self.videoImg.setAlignment(QtCore.Qt.AlignCenter)
self.gif.start()
self.videoImg.show()
self.centralwidget.show()
def addImg(self, path: str) -> None:
"""
给图片缓存区加一张图片
:param path: 图片路径
:return: None
"""
# 计算图片框位置
row = math.ceil((len(self.img) + 1) / 3) - 1
col = len(self.img) % 3
img_label = clickedLabel(len(self.img), self.topFiller)
img_label.setGeometry(QtCore.QRect(1370, 60, 151, 151))
gif = QMovie(path)
img_label.setMovie(gif)
img_label.setScaledContents(True)
gif.start()
img_label.move(col * (151 + 10) + 10, row * (151 + 10) + 10)
self.img.append((img_label, gif, path))
self.topFiller.setMinimumSize(490, (row + 1) * (151 + 10))
self.scroll.setWidget(self.topFiller)
img_label.clicked.connect(self.mainWindow.imgClicked)
img_label.show()
self.topFiller.show()
self.widget.show()
def delImg(self) -> None:
"""
清空从网络获取的所有的表情包
:return: None
"""
for imgLabel, gif, _ in self.img:
imgLabel.deleteLater()
gif.deleteLater()
self.img = []
self.widget.show()
def getImgPathByIndex(self, index: int) -> str:
"""
通过表情包缓存区的索引获取表情包的实际路径
:param index: 表情包缓存区的索引
:return: 表情包的实际位置
"""
return self.img[index][2]
def addRow(self, text: str) -> None:
"""
向表中添加一行新的数据
:param text:
:return:
"""
item = QStandardItem(text)
self.model.setItem(self.row, 0, item)
self.row += 1
def delRow(self, row: int) -> None:
"""
删除一行表中的数据
:param row: 删除索引
:return:
"""
self.model.removeRow(row)
def delAllRow(self) -> None:
"""
删除所有行
:return:
"""
for index in range(self.model.rowCount())[::-1]:
self.delRow(index)
def msgBox(self, msg: str, hasQuery: bool = False) -> bool:
"""
:param msg: 消息
:param hasQuery: 是否带询问
:return: 如果hasQuery为True,返回值为用户是否点击了确定
"""
if not hasQuery:
QtWidgets.QMessageBox.information(self.mainWindow, '提示', msg, QtWidgets.QMessageBox.Ok,
QtWidgets.QMessageBox.Ok)
return True
reply = QtWidgets.QMessageBox.question(self.mainWindow, '提示', msg,
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Cancel)
return reply == QtWidgets.QMessageBox.Yes
def getCurrentSelected(self) -> int:
"""
获取当前选中的表格单元索引
:return: 前选中的表格单元索引
"""
selectedIndex = self.tableView.selectedIndexes()
if len(selectedIndex) > 0:
return selectedIndex[0].row()
return -1
def insertRow(self, index: int) -> None:
"""
增加一行空白行
:index: 增加的位置索引
:return: None
"""
self.model.insertRow(index)
def getSubtitle(self) -> str:
"""
获取当前编辑的字幕信息
:return: 前编辑的字幕
"""
return self.singleText.text()
def getSearchText(self) -> str:
"""
获取搜索框文本信息
:return: 搜索框文本信息
"""
return self.searchText.text()
def setVideoText(self, text: str) -> None:
"""
设置视频预览区字幕信息
:param text: 字幕信息
:return: None
"""
self.videoText.setText(text)
def setFileName(self, fileName: str) -> None:
"""
设置文件名
:param fileName: 文件名
:return: None
"""
self.filenName.setText(fileName)
def windowIsVisible(self) -> bool:
"""
当前窗口是否可见,即是否被关闭
:return: 返回当前窗口是否可见,即是否被关闭
"""
return self.centralwidget.isVisible()
def setSubTileText(self, text: str) -> None:
"""
设置字幕编辑框内容
:param text: 字幕
:return: None
"""
self.singleText.setText(text)
self.singleText.home(False)
def setSearchText(self, text: str) -> None:
"""
设置搜索框内容
:param text: 搜索文字
:return: None
"""
self.searchText.setText(text)
self.searchText.home(False)
def getFileName(self) -> str:
"""
获取文件名
:return: 文件名
"""
return self.filenName.text()
def setRowText(self, row: int, text: str) -> None:
"""
设置某一行的文案信息
:param row: 行索引
:param text: 文本
:return:
"""
item = self.model.item(row, 0)
if item is not None:
item.setText(text)
def subtitleHasFocus(self) -> bool:
"""
单句字幕编辑框是否有焦点
:return: 单句字幕编辑框是否有焦点
"""
return self.singleText.hasFocus()
def setGenVideoText(self, text: str) -> None:
"""
设置生成视频按钮的文本
:param text: 文本
:return: None
"""
self.genVideo.setText(text)