-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
289 lines (232 loc) · 11.1 KB
/
index.py
File metadata and controls
289 lines (232 loc) · 11.1 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
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
import os
import subprocess
import threading
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk
ffmpeg_path = os.getenv('FFMPEG_PATH', 'ffmpeg')
class VideoConverterApp:
def __init__(self, root):
self.root = root
self.root.title("视频转换工具 - MP4转AMV")
self.root.geometry("550x420")
self.root.resizable(False, False)
self.convert_lock = threading.Lock()
self.is_converting = False
self.current_process = None
self.selected_files = []
self._create_widgets()
def _create_widgets(self):
header_frame = tk.Frame(self.root)
header_frame.pack(pady=10)
self.title_label = tk.Label(header_frame, text="视频转换工具 - MP4转AMV", font=("Microsoft YaHei UI", 16, "bold"))
self.title_label.pack()
self.status_label = tk.Label(self.root, text="就绪", fg="blue", font=("Microsoft YaHei UI", 9))
self.status_label.pack(pady=5)
self.select_button = tk.Button(
self.root,
text="📁 选择视频文件",
command=self.select_files,
font=("Microsoft YaHei UI", 10),
width=20,
height=1
)
self.select_button.pack(pady=10)
file_frame = tk.Frame(self.root)
file_frame.pack(pady=5)
tk.Label(file_frame, text="已选文件:", font=("Microsoft YaHei UI", 9)).pack(side=tk.LEFT)
self.file_count_label = tk.Label(file_frame, text="0", fg="blue", font=("Microsoft YaHei UI", 9, "bold"))
self.file_count_label.pack(side=tk.LEFT)
self.convert_button = tk.Button(
self.root,
text="▶ 开始转换",
state=tk.DISABLED,
command=self.start_conversion_thread,
font=("Microsoft YaHei UI", 10, "bold"),
width=20,
height=1,
bg="#4CAF50",
fg="white"
)
self.convert_button.pack(pady=10)
progress_frame = tk.Frame(self.root)
progress_frame.pack(pady=10, fill=tk.X, padx=30)
tk.Label(progress_frame, text="总进度:", font=("Microsoft YaHei UI", 9)).pack(anchor=tk.W)
self.total_progress = ttk.Progressbar(progress_frame, mode='determinate', length=480)
self.total_progress.pack(pady=5)
self.total_percent_label = tk.Label(progress_frame, text="0%", font=("Microsoft YaHei UI", 9))
self.total_percent_label.pack()
tk.Label(progress_frame, text="当前文件:", font=("Microsoft YaHei UI", 9)).pack(anchor=tk.W)
self.file_progress = ttk.Progressbar(progress_frame, mode='indeterminate', length=480)
self.file_progress.pack(pady=5)
self.file_progress.stop()
self.current_file_label = tk.Label(
progress_frame,
text="无",
fg="gray",
font=("Microsoft YaHei UI", 9),
wraplength=480
)
self.current_file_label.pack(pady=5)
log_frame = tk.Frame(self.root)
log_frame.pack(pady=10, fill=tk.BOTH, expand=True, padx=20)
tk.Label(log_frame, text="转换日志:", font=("Microsoft YaHei UI", 9)).pack(anchor=tk.W)
self.progress_text = tk.Text(log_frame, height=10, width=60, font=("Consolas", 8))
scrollbar = tk.Scrollbar(log_frame, orient=tk.VERTICAL, command=self.progress_text.yview)
self.progress_text.configure(yscrollcommand=scrollbar.set)
self.progress_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
def select_files(self):
if self.is_converting:
messagebox.showwarning("警告", "转换进行中,请等待完成!")
return
self.selected_files = filedialog.askopenfilenames(
title="选择视频文件",
filetypes=[("MP4 files", "*.mp4"), ("All files", "*.*")]
)
if self.selected_files:
valid_files = []
for file in self.selected_files:
if os.path.exists(file):
valid_files.append(file)
else:
self._log_message(f"⚠ 跳过不存在的文件: {file}")
self.selected_files = valid_files
if self.selected_files:
self.convert_button.config(state=tk.NORMAL)
count = len(self.selected_files)
self.file_count_label.config(text=str(count))
self._log_message(f"已选择 {count} 个有效文件")
self._update_status(f"已加载 {count} 个文件")
else:
self.file_count_label.config(text="0")
messagebox.showinfo("信息", "没有选择有效的视频文件")
def start_conversion_thread(self):
if not self.selected_files:
messagebox.showerror("错误", "请先选择视频文件!")
return
with self.convert_lock:
if self.is_converting:
messagebox.showwarning("警告", "转换任务正在进行中!")
return
self.is_converting = True
self.convert_button.config(state=tk.DISABLED, bg="#888888")
self.select_button.config(state=tk.DISABLED)
self.total_progress['value'] = 0
self.total_percent_label.config(text="0%")
conversion_thread = threading.Thread(target=self.convert_videos, daemon=True)
conversion_thread.start()
def convert_videos(self):
success_count = 0
fail_count = 0
total_files = len(self.selected_files)
for index, input_file in enumerate(self.selected_files):
if not os.path.exists(input_file):
self._log_message(f"❌ 文件不存在: {input_file}")
fail_count += 1
self._update_total_progress(index + 1, total_files)
continue
output_file = os.path.splitext(input_file)[0] + ".amv"
file_name = os.path.basename(input_file)
self._update_current_file(file_name)
self._log_message(f"📼 正在转换 [{index + 1}/{total_files}]: {file_name}")
self._start_file_progress()
try:
command = [
ffmpeg_path,
"-i", input_file,
"-c:v", "amv",
"-c:a", "adpcm_ima_amv",
"-ar", "22050",
"-ac", "1",
"-r", "15",
"-block_size", "1470",
"-vf", "scale=160:120,format=yuvj420p",
"-strict", "experimental",
"-y", output_file
]
self.current_process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = self.current_process.communicate()
if self.current_process.returncode == 0:
output_name = os.path.basename(output_file)
self._log_message(f"✅ 转换成功: {file_name} → {output_name}")
success_count += 1
else:
error_msg = self._extract_error(stderr)
self._log_message(f"❌ 转换失败: {file_name} - {error_msg}")
fail_count += 1
except Exception as e:
self._log_message(f"💥 执行错误: {file_name} - {str(e)}")
fail_count += 1
finally:
self._stop_file_progress()
self.current_process = None
self._update_total_progress(index + 1, total_files)
self._finalize_conversion(success_count, fail_count, total_files)
def _extract_error(self, stderr):
if not stderr:
return "未知错误"
lines = stderr.strip().split('\n')
error_lines = [line for line in lines if 'error' in line.lower() or 'invalid' in line.lower()]
if error_lines:
return error_lines[0].strip()
return lines[-1].strip() if lines else "未知错误"
def _update_status(self, message):
self.root.after(0, lambda: self.status_label.config(text=message))
def _update_current_file(self, filename):
self.root.after(0, lambda: self.current_file_label.config(text=filename, fg="blue"))
def _start_file_progress(self):
self.root.after(0, lambda: self.file_progress.start(50))
def _stop_file_progress(self):
self.root.after(0, lambda: self.file_progress.stop())
def _update_total_progress(self, current, total):
percentage = (current / total) * 100
self.root.after(0, lambda: self.total_progress.config(value=percentage))
self.root.after(0, lambda: self.total_percent_label.config(text=f"{percentage:.1f}%"))
def _log_message(self, message):
def _update():
self.progress_text.insert(tk.END, message + "\n")
self.progress_text.see(tk.END)
self.root.after(0, _update)
def _finalize_conversion(self, success, failed, total):
def _finish():
self.is_converting = False
self.convert_button.config(state=tk.NORMAL, bg="#4CAF50")
self.select_button.config(state=tk.NORMAL)
if success > 0:
self._update_status(f"✅ 完成 - 成功: {success}, 失败: {failed}")
else:
self._update_status("❌ 全部失败")
summary = f"\n{'='*50}\n"
summary += f"转换完成!总计: {total} 个文件\n"
summary += f"✅ 成功: {success} 个\n"
summary += f"❌ 失败: {failed} 个\n"
summary += f"{'='*50}\n"
self.progress_text.insert(tk.END, summary)
self.progress_text.see(tk.END)
self.current_file_label.config(text="无", fg="gray")
message_type = "完成" if failed == 0 else "部分完成"
messagebox.showinfo(
message_type,
f"视频转换任务{message_type}!\n\n✅ 成功: {success} 个\n❌ 失败: {failed} 个"
)
self.root.after(0, _finish)
def on_close(self):
if self.is_converting and self.current_process:
if messagebox.askyesno("确认", "转换正在进行中,确定要关闭吗?"):
if self.current_process:
self.current_process.terminate()
self.root.destroy()
else:
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = VideoConverterApp(root)
root.protocol("WM_DELETE_WINDOW", app.on_close)
root.mainloop()