-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVideoInterface.py
280 lines (241 loc) · 9.65 KB
/
VideoInterface.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
# coding:utf-8
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap, QPainter, QColor
from PyQt5.QtWidgets import QWidget, QFileDialog
from qfluentwidgets import InfoBarIcon, InfoBar, PushButton, setTheme, Theme, FluentIcon, InfoBarPosition, InfoBarManager
from functools import partial
import os
from threading import Thread
# 读取配置文件
import configparser
conf = configparser.ConfigParser()
conf.read('config.ini')
Scroll = conf.get('DEFAULT', 'ScrollUI')
if(Scroll == "0"):
from UI.Ui_video import Ui_Video
elif(Scroll == "1"):
from UI_test.Ui_video import Ui_Video
class VideoInterface(QWidget, Ui_Video):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
# 编码选项
self.EncoderType.addItem('x264')
self.EncoderType.addItem('x265')
self.DepthChoice.addItem('压制音频')
self.DepthChoice.addItem('复制音频')
self.DepthChoice.addItem('无音频')
self.DepthChoice.currentIndexChanged.connect(self.AudioChange)
# 编码参数
self.KbpsLabel.setVisible(0)
self.ParmsNum.setValue(24)
self.ButtonCRF.clicked.connect(partial(self.EncodeParms, 0))
self.ButtonVBR.clicked.connect(partial(self.EncodeParms, 1))
self.Button2pass.clicked.connect(partial(self.EncodeParms, 1))
# 文件选项
self.InputButton.clicked.connect(
partial(self.FileSelect, self.InputLine))
self.Outputbutton.clicked.connect(
partial(self.FileSelect, self.OutputLine))
self.Outputbutton_2.clicked.connect(
partial(self.FileSelect, self.TextLine))
# 文件自动填充
self.InputLine.textChanged.connect(
partial(self.AutoFill, self.InputLine, self.OutputLine))
# 音频选项
self.AudioSetTitle.setText("音频码率")
self.KbpsLabel1.setText("Kbps")
self.AudioNum.setValue(128)
self.AudioNum.setDecimals(0)
# 分辨率选项
self.WidthNum.setDisabled(1)
self.HeightNum.setDisabled(1)
self.IfEnableSwitch.checkedChanged.connect(self.ResolutionChange)
# 开始压制
self.StartButton.clicked.connect(self.ProcessFunc)
self.StartButton.setWindowIconText("")
self.StartButton.windowIconTextChanged.connect(self.ProcessComplte)
self.ProgressBar.setVisible(0)
# 硬件加速
self.HardAccler.addItem("软解")
self.HardAccler.addItem("Nvidia")
self.HardAccler.addItem("AMD")
self.HardAccler.addItem("Intel")
# CRF不支持硬件编码
self.HardAccler.setDisabled(1)
# 文件选择函数
'''
输入: 选择文件的目标LineEdit
输出: 无输出
描述: 选择文件函数, 与界面上的浏览按钮绑定, 用于把资源管理器读取的地址传回输入框
'''
def FileSelect(self, TargetLine):
dir = QFileDialog()
dir.setDirectory(os.getcwd())
if dir.exec_(): # 判断是否选择了文件
FilePath = dir.selectedFiles()
TargetLine.setText(FilePath[0])
# 自动填充函数
'''
输入: 选择文件的源LineEdit, 自动同步的目标LineEdit
输出: 无输出
描述: 根据输入框内容自动填充输出框
'''
def AutoFill(self, SourceLine, TargetLine):
FilePath = SourceLine.text()
if FilePath == "":
return
FileExt = os.path.splitext(FilePath)[1]
FilePath = os.path.splitext(FilePath)[0]
NewFilePath = FilePath + '_output.mp4'
TargetLine.setText(NewFilePath)
# 自定义分辨率控制
def ResolutionChange(self):
if (self.IfEnableSwitch.checked):
self.WidthNum.setDisabled(0)
self.HeightNum.setDisabled(0)
else:
self.WidthNum.setDisabled(1)
self.HeightNum.setDisabled(1)
# 编码参数控制
'''
输入: 编码模式选择, 0是CRF模式, 1是VBR模式
输出: 无输出
'''
def EncodeParms(self, choice):
# 0是CRF模式
if(choice == 0):
self.KbpsLabel.setVisible(0)
self.ParmsSetTitle.setText("CRF")
# CRF默认24,小数两位
self.ParmsNum.setValue(24)
self.ParmsNum.setDecimals(2)
# CRF不支持硬件编码
self.HardAccler.setCurrentIndex(0)
self.HardAccler.setDisabled(1)
# 1是VBR模式
elif(choice == 1):
self.KbpsLabel.setVisible(1)
self.ParmsSetTitle.setText("目标比特率")
# VBR默认5000,没有小数
self.ParmsNum.setValue(5000)
self.ParmsNum.setDecimals(0)
# VBR支持硬件编码
self.HardAccler.setDisabled(0)
# 音频参数控制
'''
输入: 无输入
输出: 无输出
'''
def AudioChange(self):
# 压制音频控制码率
if(self.DepthChoice.text() == "压制音频"):
self.AudioNum.setDisabled(0)
pass
# 其他模式不可控制
else:
self.AudioNum.setDisabled(1)
# 压制控制
def ProcessFunc(self):
# 地址缺失
if(self.InputLine.text() == '' or self.OutputLine.text() == ''):
InfoBar.error(
title='未定义视频地址',
content="请确认你是否已经设定了正确的输入输出视频地址",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.BOTTOM_RIGHT,
duration=6000,
parent=self
)
return
ProcessCmd = "tools\\ffmpeg -hide_banner -i \"" + self.InputLine.text() + \
"\" -y "
ProcessCmd0 = ""
# 设置音频
if(self.DepthChoice.text() == "压制音频"):
ProcessCmd += "-b:a " + str(self.AudioNum.value()) +"k "
elif(self.DepthChoice.text() == "复制音频"):
ProcessCmd += "-c:a copy "
else:
ProcessCmd += "-an "
# 硬件加速编码器
if(self.EncoderType.text() == 'x264'):
if(self.HardAccler.text() == "软解"):
ProcessCmd += "-vcodec libx264 "
elif(self.HardAccler.text() == "Nvidia"):
ProcessCmd += "-vcodec h264_nvenc "
elif(self.HardAccler.text() == "AMD"):
ProcessCmd += "-vcodec h264_amf "
elif(self.HardAccler.text() == "Intel"):
ProcessCmd += "-vcodec h264_qsv "
else:
if(self.HardAccler.text() == "软解"):
ProcessCmd += "-vcodec libx265 "
elif(self.HardAccler.text() == "Nvidia"):
ProcessCmd += "-vcodec hevc_nvenc "
elif(self.HardAccler.text() == "AMD"):
ProcessCmd += "-vcodec hevc_amf "
elif(self.HardAccler.text() == "Intel"):
ProcessCmd += "-vcodec hevc_qsv "
# 自定义分辨率
if(self.IfEnableSwitch.checked):
ProcessCmd += "-s " + \
str(self.WidthNum.value()) + "x" + \
str(self.HeightNum.value()) + " "
# 按帧数截取视频
if(self.TotalFrameNum.value() != 0):
ProcessCmd += "-vf \"select=between(n\\," + str(
self.StartFrameNum.value()) + "\\,"+str(self.TotalFrameNum.value())+")\" "
# 切换CRF和VBR参数
if(self.ButtonCRF.isChecked()):
ProcessCmd += "-crf " + str(self.ParmsNum.value()) + " "
elif(self.ButtonVBR.isChecked()):
ProcessCmd += "-b:v " + str(self.ParmsNum.value()) + "K "
else:
ProcessCmd0 = ProcessCmd
ProcessCmd0 += "-b:v " + \
str(self.ParmsNum.value()) + "K -pass 1 -an -f rawvideo -y NUL"
ProcessCmd += "-b:v " + str(self.ParmsNum.value()) + "K -pass 2 "
thread_01 = Thread(target=self.CmdThread,
args=(ProcessCmd0, ProcessCmd))
thread_01.start()
# 多线程编码函数
'''
输入: 2Pass使用的第一次Pass处理指令(为空则不适用), 处理指令
输出: 无输出
'''
def CmdThread(self, ProcessCmd0, ProcessCmd):
self.ProgressBar.setVisible(1)
self.StartButton.setText("正在压制...")
self.StartButton.setWindowIconText(" ")
self.StartButton.setDisabled(1)
if(self.TextLine.text() != ''):
ProcessCmd += "-vf \"subtitles=\'" + \
self.TextLine.text().replace(":", "\:") + "\'\""
# 按帧数截取视频
if(self.TotalFrameNum.value != 0):
ProcessCmd += ",\"select=between(n\\," + str(
self.StartFrameNum.value()) + "\\,"+str(self.TotalFrameNum.value())+")\" "
ProcessCmd += " \"" + self.OutputLine.text() + "\""
if(ProcessCmd0 != ""):
os.system(ProcessCmd0)
os.system(ProcessCmd)
self.ProgressBar.setVisible(0)
self.StartButton.setText("开始压制")
self.StartButton.setDisabled(0)
self.StartButton.setWindowIconText("")
if(self.AutoPowerOffButton.isChecked()):
os.system('shutdown -s -t 2')
# 压制完成提示
def ProcessComplte(self):
if(self.StartButton.text() == "开始压制"):
InfoBar.success(
title='任务执行完成',
content="请确认是否压制成功",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.BOTTOM_RIGHT,
duration=6000,
parent=self
)