-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
277 lines (244 loc) · 11.6 KB
/
main.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
import logging
import os
import sys
import time
from queue import Queue
from PySide6.QtCore import (QObject, QProcess, Qt,
QThread)
from PySide6.QtGui import QStandardItem, QStandardItemModel
# import subprocess
from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, QLabel,
QMainWindow, QMessageBox)
from helpui import Ui_Dialog
from mainui import Ui_MainWindow
jobQ = Queue()
logging.basicConfig(level=logging.NOTSET)
class helpWindow(QDialog, Ui_Dialog):
def __init__(self):
super(helpWindow, self).__init__()
self.setupUi(self)
class logStatus(QThread):
def __init__(self, jobListFile, joblist, jobtableviewIndex, parent=None):
super().__init__()
self.joblist = joblist
self.jobListFile = jobListFile
self.jobtableviewIndex = jobtableviewIndex
def run(self):
self.processStatus = ""
self.logdict = {}
logging.info(self.jobListFile+".proc")
with open(self.jobListFile+".proc", 'r') as logFile:
jobStatus = QStandardItem("开始处理")
jobStatus.setTextAlignment(Qt.AlignCenter)
self.joblist.setItem(self.jobtableviewIndex, 1, jobStatus)
while self.processStatus != "end":
time.sleep(0.5)
loglist = logFile.readlines()
for logvalue in loglist:
logvalueN = logvalue.replace("\n", "")
key, value = logvalueN.split("=", maxsplit=1)
self.logdict[key] = value
self.processSpeed = self.logdict['speed']
self.processStatus = self.logdict['progress']
jobSpeed = QStandardItem(self.processSpeed)
jobSpeed.setTextAlignment(Qt.AlignCenter)
self.joblist.setItem(self.jobtableviewIndex, 1, jobSpeed)
jobStatus = QStandardItem("已解决")
jobStatus.setTextAlignment(Qt.AlignCenter)
self.joblist.setItem(self.jobtableviewIndex, 1, jobStatus)
self.exit(0)
self.exit(1)
class watchjob(QObject):
def __init__(self, joblist, ui):
super(watchjob, self).__init__()
self.joblist = joblist
self.ui = ui
def btn(self):
while True:
time.sleep(2)
jobStatus = self.joblist.item(self.joblist.rowCount()-1, 1).text()
# 判断最后一行第二列文字内容是否为“已解决”,如果都解决了就退出。不过这里的判断有个BUG,
# 就是如果在运行途中加入任务,那么新加入的任务不会被执行,永远都是未解决死循环
# 看了几个月前的代码后,根本忘了逻辑了,也懒得想,直接禁止中途加入任务了……
logging.info("jobstatus:"+jobStatus)
if jobStatus == "已解决":
self.ui.btnStartJob.setEnabled(True)
self.ui.btnJoinFile.setEnabled(True)
self.ui.btnSaveFile.setEnabled(True)
break
class job(QObject):
def __init__(self, joblist):
super(job, self).__init__()
self.joblist = joblist
def res(self, exitCode, exitStatus):
logging.info("res code:")
logging.info(exitStatus)
def runJoinP(self):
while True:
if jobQ.empty():
break
jobQlist = jobQ.get()
jobQ.task_done()
jobListFile = jobQlist[0]
jobOutFile = jobQlist[1]
jobtableviewIndex = jobQlist[2]
self.process = QProcess()
self.process.setProgram("ffmpeg")
# ffmpeg.exe -y -hwaccel cuda -f concat -safe 0 -i job1.txt -c copy 14.mp4 -progress job1out.txt
self.process.setArguments(['-y', '-f', 'concat', '-safe', '0', '-i',
jobListFile, '-c', 'copy', jobOutFile, '-progress', jobListFile+".proc", '-loglevel', 'quiet'])
# self.process.startDetached()
# logging.info(str(self.process.arguments()))
# self.process.finished.connect(self.res)
self.process.start()
time.sleep(1)
if os.path.exists(jobListFile):
self._logStatus = logStatus(
jobListFile, self.joblist, jobtableviewIndex)
self._logStatus.start()
self._logStatus.wait()
self.process.waitForFinished(-1)
logging.info(self.process.state())
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.cwd = os.getcwd()
self.ui.btnJoinFile.clicked.connect(self.joinFile)
self.ui.btnFileUp.clicked.connect(self.fileUp)
self.ui.btnFileDown.clicked.connect(self.fileDown)
self.ui.btnSaveFile.clicked.connect(self.saveFile)
self.ui.btnStartJob.clicked.connect(self.startJob)
self.joblist = QStandardItemModel()
self.joblist.setHorizontalHeaderLabels(['任务', '速度/进度'])
self.ui.tableView.setModel(self.joblist)
self.header = self.ui.tableView.horizontalHeader()
self.header.setSectionResizeMode(0, self.header.Stretch)
self.header.setSectionResizeMode(1, self.header.Fixed)
self.header.resizeSection(1, 100)
self.header.setStyleSheet(
"QHeaderView::section{"
"border-top:0px solid #D8D8D8;"
"border-left:0px solid #D8D8D8;"
"border-right:1px solid #D8D8D8;"
"border-bottom: 1px solid #D8D8D8;"
"background-color:white;"
"padding:4px;"
"}")
aboutmeText = '''<a href="https://dvd3.net/2021/03/cdffmpeg/" style="color:gray">如果觉得有用给老哥点个赞>></a>'''
self.ui.aboutlink = QLabel(aboutmeText)
self.ui.aboutlink.setOpenExternalLinks(True)
self.ui.statusbar.setStyleSheet("QStatusBar::item{border: 0px}")
self.ui.statusbar.addPermanentWidget(self.ui.aboutlink)
self.helpWindows = helpWindow()
self.ui.btnAbout.clicked.connect(self.helpWindows.exec)
def joinFile(self):
fileName_choose, filetype = QFileDialog.getOpenFileNames(
self, "文件保存", "", "视频文件 (*.mp4 *.avi *.mkv *.wmv)")
if len(fileName_choose) > 0:
self.ui.fileListWidget.addItems(fileName_choose)
# logging.info(fileName_choose)
def startJob(self):
fineNum = 0
self.ui.btnStartJob.setEnabled(False)
self.ui.btnJoinFile.setEnabled(False)
self.ui.btnSaveFile.setEnabled(False)
QApplication.processEvents()
if self.joblist.rowCount() == 0:
QMessageBox.warning(self, "你有压力", "得先添加任务才能开始任务")
self.ui.btnStartJob.setEnabled(True)
else:
for i in range(self.joblist.rowCount()):
jobStatus = self.joblist.item(i, 1).text()
logging.info("join job:"+jobStatus)
if jobStatus == "未解决":
jobListFile = str(i+1)+".job"
jobOutFile = self.joblist.item(i, 0).text()
jobtableviewIndex = i
jobQlist = []
jobQlist.append(jobListFile)
jobQlist.append(jobOutFile)
jobQlist.append(jobtableviewIndex)
jobQ.put(jobQlist)
fineNum = fineNum+1
if fineNum == 0:
QMessageBox.warning(self, "你有压力", "当前所有任务已经完成,再戳也不会开始了")
self.ui.btnStartJob.setEnabled(True)
else:
# 启动任务线程
self.tJob = job(self.joblist)
self.tJobThread = QThread()
self.tJob.moveToThread(self.tJobThread)
self.tJobThread.started.connect(self.tJob.runJoinP)
self.tJobThread.start()
self.tJobThread.exit(0)
# 启动任务状态监视线程,实现按钮可用性调整
self.watchJob = watchjob(self.joblist, self.ui)
self.watchJobT = QThread()
self.watchJob.moveToThread(self.watchJobT)
self.watchJobT.started.connect(self.watchJob.btn)
self.watchJobT.start()
self.watchJobT.exit(0)
# self.ui.btnStartJob.setEnabled(True)
QApplication.processEvents()
def saveFile(self):
fileName_choose, filetype = QFileDialog.getSaveFileName(self,
"文件保存",
self.cwd,
"视频文件 (*.mp4 *.avi *.mkv *.wmv)")
jobstat = QStandardItem("未解决")
jobstat.setTextAlignment(Qt.AlignCenter)
if fileName_choose != "":
jobfilename = QStandardItem(fileName_choose)
self.joblist.appendRow([jobfilename, jobstat])
jobtaskname = str(self.joblist.rowCount())+".job"
with open(jobtaskname, "w", encoding='utf-8') as jobfile:
jobfile.writelines("#begin\n")
listCount = self.ui.fileListWidget.count()
for i in range(listCount):
mediaFile = "file "+"\'" + \
self.ui.fileListWidget.item(i).text()+"\'"
# logging.info(mediaFile)
jobfile.write(mediaFile+"\r")
self.ui.fileListWidget.clear()
def fileUp(self):
currentRow = self.ui.fileListWidget.currentRow()
currentItem = self.ui.fileListWidget.takeItem(currentRow)
if currentRow > 0:
newRow = currentRow-1
else:
newRow = currentRow
self.ui.fileListWidget.insertItem(newRow, currentItem)
self.ui.fileListWidget.setCurrentRow(newRow)
def fileDown(self):
currentRow = self.ui.fileListWidget.currentRow()
currentItem = self.ui.fileListWidget.takeItem(currentRow)
if currentRow < self.ui.fileListWidget.count():
newRow = currentRow+1
else:
newRow = currentRow
self.ui.fileListWidget.insertItem(newRow, currentItem)
self.ui.fileListWidget.setCurrentRow(newRow)
def closeEvent(self, e):
if self.joblist.rowCount() != 0:
res = QMessageBox.question(
self, "你有压力", "退出后未完成的任务列表将被清空,是否继续退出?", QMessageBox.Cancel | QMessageBox.Ok)
if res == QMessageBox.Ok:
fileList = os.scandir('.')
for sFile in fileList:
if sFile.is_file() and sFile.name.split(".")[-1].lower() == "job" and sFile.name.split(".")[0].lower().isdigit():
logging.info(os.getcwd()+"\\"+sFile.name)
jobfile = os.getcwd()+"\\"+sFile.name
jobfilelog = os.getcwd()+"\\"+sFile.name+".proc"
os.remove(jobfile)
if os.path.exists(jobfilelog):
os.remove(jobfilelog)
e.accept()
else:
e.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())