-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·528 lines (474 loc) · 23.7 KB
/
app.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import ttk
import subprocess
import threading
import time
import re
import os
import platform
import urllib.request
import zipfile
import shutil
import locale
import json
import gettext
class FFmpegGUI:
def __init__(self, root):
self.root = root
self.root.title("VideoSlimmer")
# 初始化国际化
self.init_i18n()
# 初始化变量
self.input_files = []
self.duration = 0
self.is_running = False
self.process = None
self.start_time = None
self.current_file_index = 0
self.total_files = 0
self.presets = self.load_presets()
# 创建菜单栏
self.create_menu()
# 输入文件
self.input_label = tk.Label(root, text=_("输入文件:"))
self.input_label.grid(row=0, column=0, padx=5, pady=5, sticky='e')
self.input_entry = tk.Entry(root, width=50)
self.input_entry.grid(row=0, column=1, padx=5, pady=5)
self.input_button = tk.Button(root, text=_("浏览"), command=self.browse_input)
self.input_button.grid(row=0, column=2, padx=5, pady=5)
# 输出目录
self.output_label = tk.Label(root, text=_("输出目录:"))
self.output_label.grid(row=1, column=0, padx=5, pady=5, sticky='e')
self.output_entry = tk.Entry(root, width=50)
self.output_entry.grid(row=1, column=1, padx=5, pady=5)
self.output_button = tk.Button(root, text=_("浏览"), command=self.browse_output)
self.output_button.grid(row=1, column=2, padx=5, pady=5)
# 预设选项
self.preset_label = tk.Label(root, text=_("选择预设:"))
self.preset_label.grid(row=2, column=0, padx=5, pady=5, sticky='e')
self.preset_var = tk.StringVar(value=_('自定义'))
self.preset_option = ttk.Combobox(root, textvariable=self.preset_var, values=self.get_presets())
self.preset_option.grid(row=2, column=1, padx=5, pady=5, sticky='w')
self.preset_option.bind("<<ComboboxSelected>>", self.apply_preset)
# 编码器选项
self.encoder_label = tk.Label(root, text=_("选择视频编码器:"))
self.encoder_label.grid(row=3, column=0, padx=5, pady=5, sticky='e')
self.encoder_var = tk.StringVar(value='av1_nvenc')
self.encoder_option = ttk.Combobox(root, textvariable=self.encoder_var, values=['av1_nvenc', 'h264_nvenc', 'hevc_nvenc', 'libx264', 'libx265'])
self.encoder_option.grid(row=3, column=1, padx=5, pady=5, sticky='w')
# CQ 值
self.cq_label = tk.Label(root, text=_("视频 CQ (恒定质量):"))
self.cq_label.grid(row=4, column=0, padx=5, pady=5, sticky='e')
self.cq_entry = tk.Entry(root, width=10)
self.cq_entry.insert(0, "20")
self.cq_entry.grid(row=4, column=1, padx=5, pady=5, sticky='w')
# 封装格式选项
self.container_label = tk.Label(root, text=_("选择封装格式:"))
self.container_label.grid(row=5, column=0, padx=5, pady=5, sticky='e')
self.container_var = tk.StringVar(value='mp4')
self.container_option = ttk.Combobox(root, textvariable=self.container_var, values=['mp4', 'mov', 'mkv', 'flv'])
self.container_option.grid(row=5, column=1, padx=5, pady=5, sticky='w')
# 保留字幕选项
self.subtitle_var = tk.BooleanVar()
self.subtitle_check = tk.Checkbutton(root, text=_("保留字幕"), variable=self.subtitle_var)
self.subtitle_check.grid(row=6, column=1, padx=5, pady=5, sticky='w')
# 音频编码器选项
self.audio_encoder_label = tk.Label(root, text=_("选择音频编码器:"))
self.audio_encoder_label.grid(row=7, column=0, padx=5, pady=5, sticky='e')
self.audio_encoder_var = tk.StringVar(value='copy')
self.audio_encoder_option = ttk.Combobox(root, textvariable=self.audio_encoder_var, values=['copy', 'aac', 'mp3', 'libopus'])
self.audio_encoder_option.grid(row=7, column=1, padx=5, pady=5, sticky='w')
# 音频比特率
self.audio_bitrate_label = tk.Label(root, text=_("音频比特率 (kbps):"))
self.audio_bitrate_label.grid(row=8, column=0, padx=5, pady=5, sticky='e')
self.audio_bitrate_entry = tk.Entry(root, width=10)
self.audio_bitrate_entry.insert(0, "128")
self.audio_bitrate_entry.grid(row=8, column=1, padx=5, pady=5, sticky='w')
# 音频采样率
self.audio_sample_rate_label = tk.Label(root, text=_("音频采样率 (Hz):"))
self.audio_sample_rate_label.grid(row=9, column=0, padx=5, pady=5, sticky='e')
self.audio_sample_rate_entry = tk.Entry(root, width=10)
self.audio_sample_rate_entry.insert(0, "44100")
self.audio_sample_rate_entry.grid(row=9, column=1, padx=5, pady=5, sticky='w')
# 音频声道数
self.audio_channels_label = tk.Label(root, text=_("音频声道数:"))
self.audio_channels_label.grid(row=10, column=0, padx=5, pady=5, sticky='e')
self.audio_channels_entry = tk.Entry(root, width=10)
self.audio_channels_entry.insert(0, "2")
self.audio_channels_entry.grid(row=10, column=1, padx=5, pady=5, sticky='w')
# 开始和取消按钮
self.button_frame = tk.Frame(root)
self.button_frame.grid(row=11, column=0, columnspan=3, pady=5)
self.start_button = tk.Button(self.button_frame, text=_("开始"), command=self.start_encoding)
self.start_button.pack(side='left', padx=5)
self.cancel_button = tk.Button(self.button_frame, text=_("取消"), command=self.cancel_encoding, state='disabled')
self.cancel_button.pack(side='left', padx=5)
# 进度条
self.progress = ttk.Progressbar(root, orient='horizontal', length=400, mode='determinate')
self.progress.grid(row=12, column=0, columnspan=3, padx=5, pady=5)
# 预计剩余时间
self.time_label = tk.Label(root, text=_("预计剩余时间: N/A"))
self.time_label.grid(row=13, column=0, columnspan=3, padx=5, pady=5)
# 状态标签
self.status_label = tk.Label(root, text=_("状态: 空闲"))
self.status_label.grid(row=14, column=0, columnspan=3, padx=5, pady=5)
def init_i18n(self):
# 初始化国际化
lang = locale.getdefaultlocale()[0]
if lang is None:
lang = 'en_US'
lang = lang.split('_')[0]
locales_dir = os.path.join(os.path.dirname(__file__), 'locales')
self.trans = gettext.translation('app', localedir=locales_dir, languages=[lang], fallback=True)
self.trans.install()
global _
_ = self.trans.gettext
def create_menu(self):
# 创建菜单栏
self.menu_bar = tk.Menu(self.root)
# 文件菜单
file_menu = tk.Menu(self.menu_bar, tearoff=0)
file_menu.add_command(label=_("打开输入文件"), command=self.browse_input)
file_menu.add_command(label=_("选择输出目录"), command=self.browse_output)
file_menu.add_separator()
file_menu.add_command(label=_("退出"), command=self.root.quit)
self.menu_bar.add_cascade(label=_("文件"), menu=file_menu)
# 设置菜单
settings_menu = tk.Menu(self.menu_bar, tearoff=0)
settings_menu.add_command(label=_("环境检查"), command=self.check_environment)
self.menu_bar.add_cascade(label=_("设置"), menu=settings_menu)
# 语言菜单
language_menu = tk.Menu(self.menu_bar, tearoff=0)
language_menu.add_command(label="English", command=lambda: self.change_language('en'))
language_menu.add_command(label="中文", command=lambda: self.change_language('zh'))
self.menu_bar.add_cascade(label=_("语言"), menu=language_menu)
# 帮助菜单
help_menu = tk.Menu(self.menu_bar, tearoff=0)
help_menu.add_command(label=_("关于"), command=self.show_about)
self.menu_bar.add_cascade(label=_("帮助"), menu=help_menu)
self.root.config(menu=self.menu_bar)
def change_language(self, lang_code):
# 更改语言
locales_dir = os.path.join(os.path.dirname(__file__), 'locales')
self.trans = gettext.translation('app', localedir=locales_dir, languages=[lang_code], fallback=True)
self.trans.install()
global _
_ = self.trans.gettext
# 重新加载界面文本
self.reload_ui_text()
def reload_ui_text(self):
# 重新设置界面上的文本
self.root.title(_("FFmpeg GUI"))
self.input_label.config(text=_("输入文件:"))
self.input_button.config(text=_("浏览"))
self.output_label.config(text=_("输出目录:"))
self.output_button.config(text=_("浏览"))
self.preset_label.config(text=_("选择预设:"))
self.preset_option['values'] = self.get_presets()
self.encoder_label.config(text=_("选择视频编码器:"))
self.cq_label.config(text=_("视频 CQ (恒定质量):"))
self.container_label.config(text=_("选择封装格式:"))
self.subtitle_check.config(text=_("保留字幕"))
self.audio_encoder_label.config(text=_("选择音频编码器:"))
self.audio_bitrate_label.config(text=_("音频比特率 (kbps):"))
self.audio_sample_rate_label.config(text=_("音频采样率 (Hz):"))
self.audio_channels_label.config(text=_("音频声道数:"))
self.start_button.config(text=_("开始"))
self.cancel_button.config(text=_("取消"))
self.time_label.config(text=_("预计剩余时间: N/A"))
self.status_label.config(text=_("状态: 空闲"))
# 更新菜单栏
self.create_menu()
def show_about(self):
# 显示关于对话框
messagebox.showinfo(_("关于"), _("FFmpeg GUI\n版本: 1.0\n作者: OpenAI Assistant"))
def browse_input(self):
filenames = filedialog.askopenfilenames()
if filenames:
self.input_files = list(filenames)
self.input_entry.delete(0, tk.END)
self.input_entry.insert(0, "; ".join(filenames))
# 计算总文件数量
self.total_files = len(self.input_files)
def browse_output(self):
directory = filedialog.askdirectory()
if directory:
self.output_entry.delete(0, tk.END)
self.output_entry.insert(0, directory)
def get_presets(self):
preset_names = ['自定义']
for preset in self.presets:
preset_names.append(preset['preset_name'])
return preset_names
def load_presets(self):
presets = []
preset_dir = os.path.join(os.getcwd(), 'presets')
if not os.path.exists(preset_dir):
os.makedirs(preset_dir)
for file_name in os.listdir(preset_dir):
if file_name.endswith('.json'):
file_path = os.path.join(preset_dir, file_name)
try:
with open(file_path, 'r', encoding='utf-8') as f:
preset = json.load(f)
presets.append(preset)
except Exception as e:
print(f"加载预设时出错:{e}")
return presets
def apply_preset(self, event=None):
preset_name = self.preset_var.get()
if preset_name == '自定义':
return
for preset in self.presets:
if preset['preset_name'] == preset_name:
# 设置视频参数
self.encoder_var.set(preset.get('video_encoder', 'av1_nvenc'))
self.cq_entry.delete(0, tk.END)
self.cq_entry.insert(0, preset.get('cq_value', '20'))
self.container_var.set(preset.get('container', 'mp4'))
# 设置音频参数
self.audio_encoder_var.set(preset.get('audio_encoder', 'copy'))
self.audio_bitrate_entry.delete(0, tk.END)
self.audio_bitrate_entry.insert(0, preset.get('audio_bitrate', '128'))
self.audio_sample_rate_entry.delete(0, tk.END)
self.audio_sample_rate_entry.insert(0, preset.get('audio_sample_rate', '44100'))
self.audio_channels_entry.delete(0, tk.END)
self.audio_channels_entry.insert(0, preset.get('audio_channels', '2'))
# 设置字幕选项
self.subtitle_var.set(preset.get('keep_subtitles', False))
break
def start_encoding(self):
if self.is_running:
return
input_files = self.input_files
output_dir = self.output_entry.get()
cq_value = self.cq_entry.get()
encoder = self.encoder_var.get()
container = self.container_var.get()
keep_subtitles = self.subtitle_var.get()
audio_encoder = self.audio_encoder_var.get()
audio_bitrate = self.audio_bitrate_entry.get()
audio_sample_rate = self.audio_sample_rate_entry.get()
audio_channels = self.audio_channels_entry.get()
if not input_files:
self.status_label.config(text="状态: 输入文件无效")
return
if not output_dir or not os.path.isdir(output_dir):
self.status_label.config(text="状态: 输出目录无效")
return
if not cq_value.isdigit():
self.status_label.config(text="状态: 视频 CQ 值无效")
return
if audio_encoder != 'copy' and not audio_bitrate.isdigit():
self.status_label.config(text="状态: 音频比特率无效")
return
if audio_encoder != 'copy' and not audio_sample_rate.isdigit():
self.status_label.config(text="状态: 音频采样率无效")
return
if audio_encoder != 'copy' and not audio_channels.isdigit():
self.status_label.config(text="状态: 音频声道数无效")
return
self.is_running = True
self.cancel_button.config(state='normal')
self.start_button.config(state='disabled')
self.current_file_index = 0
threading.Thread(target=self.encode_files, args=(
input_files, output_dir, cq_value, encoder, container, keep_subtitles,
audio_encoder, audio_bitrate, audio_sample_rate, audio_channels
)).start()
def encode_files(self, input_files, output_dir, cq_value, encoder, container, keep_subtitles,
audio_encoder, audio_bitrate, audio_sample_rate, audio_channels):
total_files = len(input_files)
for index, input_file in enumerate(input_files):
if not self.is_running:
break
self.current_file_index = index + 1
self.status_label.config(text=f"状态: 正在编码 {self.current_file_index}/{total_files}")
self.progress['value'] = 0
self.time_label.config(text="预计剩余时间: N/A")
self.start_time = time.time()
# 构建输出文件名
filename = os.path.basename(input_file)
name, _ = os.path.splitext(filename)
output_file = os.path.join(output_dir, f"{name}.{container}")
# 检查输出文件是否存在
if os.path.exists(output_file):
overwrite = messagebox.askyesno("文件已存在", f"文件 '{output_file}' 已存在。是否要覆盖?")
if not overwrite:
self.status_label.config(text=f"状态: 已跳过 {self.current_file_index}/{total_files}")
continue
# 获取文件时长
self.duration = self.get_duration(input_file)
# 构建 ffmpeg 命令
cmd = [
'ffmpeg',
'-y', # 自动覆盖已存在的文件
'-i', input_file,
'-c:v', encoder,
'-preset', 'p7',
'-rc', 'constqp',
'-cq', cq_value,
'-bf', '2',
'-rc-lookahead', '32',
'-spatial_aq', '1',
'-temporal_aq', '1',
]
# 处理音频编码器
if audio_encoder == 'copy':
cmd.extend(['-c:a', 'copy'])
else:
cmd.extend(['-c:a', audio_encoder])
cmd.extend(['-b:a', f'{audio_bitrate}k'])
cmd.extend(['-ar', audio_sample_rate])
cmd.extend(['-ac', audio_channels])
# 处理字幕选项
if keep_subtitles:
if container == 'mp4':
cmd.extend(['-c:s', 'mov_text'])
else:
cmd.extend(['-c:s', 'copy'])
else:
cmd.extend(['-sn'])
# 添加封装格式和输出文件
cmd.extend(['-f', container, output_file])
# 运行 ffmpeg
self.run_ffmpeg(cmd)
self.is_running = False
self.cancel_button.config(state='disabled')
self.start_button.config(state='normal')
if self.is_running:
self.status_label.config(text="状态: 完成")
else:
self.status_label.config(text="状态: 已取消")
def run_ffmpeg(self, cmd):
self.process = subprocess.Popen(cmd, stderr=subprocess.PIPE, universal_newlines=True)
duration = self.duration
time_pattern = re.compile(r'time=(\d+):(\d+):(\d+\.\d+)')
while True:
if self.process.poll() is not None:
break
line = self.process.stderr.readline()
if not line:
continue
print(line.strip())
if 'time=' in line:
match = time_pattern.search(line)
if match:
hours = float(match.group(1))
minutes = float(match.group(2))
seconds = float(match.group(3))
elapsed = hours * 3600 + minutes * 60 + seconds
progress_percent = (elapsed / duration) * 100
self.progress['value'] = progress_percent
# 计算预计剩余时间
elapsed_time = time.time() - self.start_time
if progress_percent > 0:
total_estimated_time = elapsed_time / (progress_percent / 100)
remaining_time = total_estimated_time - elapsed_time
remaining_time_str = time.strftime('%H:%M:%S', time.gmtime(remaining_time))
self.time_label.config(text=f"预计剩余时间: {remaining_time_str}")
self.root.update_idletasks()
self.process.wait()
self.progress['value'] = 100
def cancel_encoding(self):
if self.is_running:
self.is_running = False
if self.process:
self.process.terminate()
self.status_label.config(text="状态: 已取消")
self.progress['value'] = 0
self.time_label.config(text="预计剩余时间: N/A")
self.cancel_button.config(state='disabled')
self.start_button.config(state='normal')
def get_duration(self, filename):
try:
cmd = ['ffprobe', '-v', 'error', '-show_entries',
'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', filename]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
duration = float(result.stdout.strip())
return duration
except Exception as e:
print(f"Error getting duration: {e}")
return 0
def check_environment(self):
# 检查 ffmpeg 是否可用
ffmpeg_available = False
try:
subprocess.run(['ffmpeg', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
ffmpeg_available = True
except Exception:
ffmpeg_available = False
# 检查是否存在 NVIDIA 显卡
nvidia_gpu_available = False
if platform.system() == 'Linux':
try:
result = subprocess.run(['nvidia-smi'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
if "NVIDIA-SMI" in result.stdout:
nvidia_gpu_available = True
except Exception:
nvidia_gpu_available = False
elif platform.system() == 'Darwin':
# macOS 下默认不支持 NVIDIA GPU,假定无 NVIDIA GPU 支持
nvidia_gpu_available = False
# 创建新的窗口显示结果
env_window = tk.Toplevel(self.root)
env_window.title("环境检查结果")
# 使用 Unicode 字符显示对号和叉号
check_mark = '✔' # ✔
cross_mark = '✘' # ✘
# ffmpeg 检查结果
ffmpeg_label = tk.Label(env_window, text="ffmpeg 可用性:")
ffmpeg_label.grid(row=0, column=0, padx=5, pady=5, sticky='e')
ffmpeg_result = tk.Label(env_window, text=check_mark if ffmpeg_available else cross_mark, fg='green' if ffmpeg_available else 'red')
ffmpeg_result.grid(row=0, column=1, padx=5, pady=5, sticky='w')
# NVIDIA 显卡检查结果
nvidia_label = tk.Label(env_window, text="NVIDIA 显卡:")
nvidia_label.grid(row=1, column=0, padx=5, pady=5, sticky='e')
nvidia_result = tk.Label(env_window, text=check_mark if nvidia_gpu_available else cross_mark, fg='green' if nvidia_gpu_available else 'red')
nvidia_result.grid(row=1, column=1, padx=5, pady=5, sticky='w')
# 提示信息
info_text = ""
if not ffmpeg_available:
info_text += "未找到 ffmpeg,请安装 ffmpeg 并将其添加到系统环境变量。\n"
if not nvidia_gpu_available:
info_text += "未检测到 NVIDIA 显卡。请确保已安装 NVIDIA 显卡和正确的驱动程序。\n"
if info_text == "":
info_text = "您的环境已正确配置。"
info_label = tk.Label(env_window, text=info_text)
info_label.grid(row=2, column=0, columnspan=2, padx=5, pady=10)
# 关闭按钮
close_button = tk.Button(env_window, text="关闭", command=env_window.destroy)
close_button.grid(row=3, column=0, columnspan=2, pady=5)
def install_ffmpeg(self):
# 根据操作系统选择对应的 ffmpeg 下载链接
system = platform.system()
if system == 'Windows':
ffmpeg_url = 'https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip'
zip_name = 'ffmpeg.zip'
install_path = os.path.join(os.getcwd(), 'ffmpeg')
bin_path = os.path.join(install_path, 'bin')
else:
messagebox.showinfo("暂不支持", "自动安装 ffmpeg 的功能目前仅支持 Windows 系统。")
return
try:
# 下载 ffmpeg
self.status_label.config(text="状态: 正在下载 ffmpeg...")
urllib.request.urlretrieve(ffmpeg_url, zip_name)
# 解压缩
self.status_label.config(text="状态: 正在安装 ffmpeg...")
with zipfile.ZipFile(zip_name, 'r') as zip_ref:
zip_ref.extractall(install_path)
# 删除压缩文件
os.remove(zip_name)
# 更新系统环境变量
os.environ['PATH'] += os.pathsep + bin_path
messagebox.showinfo("安装完成", "ffmpeg 已成功安装。请重新启动程序以确保更改生效。")
self.status_label.config(text="状态: ffmpeg 安装完成")
except Exception as e:
messagebox.showerror("安装失败", f"ffmpeg 安装失败:{e}")
self.status_label.config(text="状态: ffmpeg 安装失败")
if __name__ == '__main__':
root = tk.Tk()
app = FFmpegGUI(root)
root.mainloop()