-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
506 lines (428 loc) · 15.7 KB
/
main.py
File metadata and controls
506 lines (428 loc) · 15.7 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
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
import os
import sys
import subprocess
from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog, QInputDialog, QMessageBox
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl, QThread, pyqtSignal, QTimer
from PyQt6.QtWebChannel import QWebChannel
from PyQt6.QtCore import QObject, pyqtSlot
def get_ffmpeg_path(exe_name):
base = os.path.abspath("./ffmpeg/bin")
if sys.platform == "win32":
return os.path.join(base, exe_name + ".exe")
return os.path.join(base, exe_name)
class CompressorThread(QThread):
progress_updated = pyqtSignal(int)
compression_done = pyqtSignal(str)
def __init__(self, input_path, output_path, target_size_mb, duration_sec):
super().__init__()
self.input_path = input_path
self.output_path = output_path
self.target_size_mb = target_size_mb
self.duration_sec = duration_sec
def run(self):
ffmpeg_path = get_ffmpeg_path("ffmpeg")
audio_bitrate = 128 * 1024
target_total_bits = self.target_size_mb * 1024 * 1024 * 8
video_bitrate = int((target_total_bits / self.duration_sec) - audio_bitrate)
if video_bitrate < 100_000:
video_bitrate = 100_000
command = [
ffmpeg_path,
"-y",
"-i", self.input_path,
"-c:v", "libx264",
"-b:v", str(video_bitrate),
"-preset", "slow",
"-profile:v", "high",
"-c:a", "aac",
"-b:a", "128k",
"-movflags", "+faststart",
"-vf", "scale='min(1280,iw)':-2",
self.output_path
]
process = subprocess.Popen(command, stderr=subprocess.PIPE, text=True)
for line in process.stderr:
if "time=" in line:
timestamp = line.split("time=")[-1].split(" ")[0]
try:
parts = timestamp.split(":")
if len(parts) == 3:
h, m, s = [float(x) for x in parts]
elif len(parts) == 2:
h = 0
m, s = [float(x) for x in parts]
elif len(parts) == 1:
h = 0
m = 0
s = float(parts[0])
else:
continue
seconds = h * 3600 + m * 60 + s
percent = int((seconds / self.duration_sec) * 100)
self.progress_updated.emit(min(percent, 100))
except ValueError:
continue
process.wait()
if os.path.exists(self.output_path):
self.compression_done.emit(self.output_path)
class Bridge(QObject):
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
self.percent = 0
@pyqtSlot()
def selectFile(self):
self.main_window.select_file_and_start()
@pyqtSlot(str)
def openFolder(self, path):
self.main_window.open_folder(path)
@pyqtSlot(int)
def setPercent(self, val):
self.percent = val
@pyqtSlot(result=int)
def getPercent(self):
return self.percent
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.view = QWebEngineView()
self.setCentralWidget(self.view)
self.setWindowTitle("VideoCompressor")
self.bridge = Bridge(self)
self.channel = QWebChannel()
self.channel.registerObject("bridge", self.bridge)
self.view.page().setWebChannel(self.channel)
self.view.setHtml(self.html_content())
self.resize(900, 600)
QTimer.singleShot(500, self.show_welcome_screen)
def show_welcome_screen(self):
self.view.page().runJavaScript("showWelcomeScreen();")
def select_file_and_start(self):
input_path, _ = QFileDialog.getOpenFileName(self, "Wybierz plik wideo", "", "Pliki wideo (*.mp4 *.avi *.mkv *.mov *.wmv)")
if not input_path:
self.view.page().runJavaScript("showWelcomeScreen();")
return
try:
duration = self.get_duration(input_path)
except Exception as e:
QMessageBox.critical(self, "Błąd FFprobe", f"Nie udało się odczytać długości filmu:\n{e}")
self.view.page().runJavaScript("showWelcomeScreen();")
return
target_size_mb, ok = QInputDialog.getInt(self, "Rozmiar docelowy", "Do ilu MB chcesz zmniejszyć?", 20, 1, 10_000_000)
if not ok:
self.view.page().runJavaScript("showWelcomeScreen();")
return
base, ext = os.path.splitext(input_path)
output_path = base + f"_compressed{ext}"
self.view.page().runJavaScript("showCompressionScreen();")
self.thread = CompressorThread(input_path, output_path, target_size_mb, duration)
self.thread.progress_updated.connect(self.update_progress)
self.thread.compression_done.connect(self.compression_finished)
self.thread.start()
def get_duration(self, input_path):
ffprobe_path = get_ffmpeg_path("ffprobe")
result = subprocess.run(
[ffprobe_path, "-v", "error", "-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1", input_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
if result.returncode != 0 or not result.stdout.strip():
raise RuntimeError(result.stderr)
return float(result.stdout.strip())
def update_progress(self, percent):
self.view.page().runJavaScript(f"updateProgress({percent});")
def compression_finished(self, output_path):
js_path = output_path.replace('\\', '\\\\')
self.view.page().runJavaScript(f"showCompletionScreen('{js_path}');")
def open_folder(self, output_path):
folder_path = os.path.dirname(output_path)
if sys.platform == 'win32':
os.startfile(folder_path)
elif sys.platform == 'darwin':
subprocess.run(["open", folder_path])
else:
subprocess.run(["xdg-open", folder_path])
def html_content(self):
return """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>VideoCompressor</title>
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #000000, #121212, #1e1e1e);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
overflow: hidden;
}
.container {
width: 80%;
max-width: 800px;
text-align: center;
background: rgba(30, 30, 30, 0.7);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 40px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
border: 1px solid rgba(50, 50, 50, 0.5);
}
h1 {
font-size: 36px;
margin-bottom: 20px;
color: #ffffff;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
p {
font-size: 18px;
margin-bottom: 30px;
line-height: 1.6;
color: #cccccc;
}
.btn {
background: #333333;
color: white;
border: none;
padding: 15px 30px;
font-size: 18px;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
outline: none;
margin: 10px;
font-weight: bold;
}
.btn:hover {
background: #444444;
transform: translateY(-3px);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
}
.btn:active {
transform: translateY(1px);
}
.btn-secondary {
background: #222222;
border: 1px solid #444444;
}
.btn-secondary:hover {
background: #333333;
}
.progress-container {
width: 100%;
background: rgba(40, 40, 40, 0.5);
border-radius: 30px;
overflow: hidden;
height: 30px;
margin: 30px 0;
box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.3);
position: relative;
}
.progress-bar {
height: 100%;
width: 0%;
background: linear-gradient(90deg, #333333, #555555);
text-align: center;
line-height: 30px;
color: white;
font-weight: bold;
transition: width 0.3s ease;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
.screen {
display: none;
}
.active {
display: block;
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.icon {
font-size: 80px;
margin-bottom: 20px;
color: #ffffff;
}
.file-info {
background: rgba(20, 20, 20, 0.5);
padding: 15px;
border-radius: 10px;
margin: 20px 0;
text-align: left;
border: 1px solid #333333;
}
.file-info p {
margin: 5px 0;
font-size: 16px;
color: #aaaaaa;
}
.completion-message {
font-size: 24px;
color: #7f7f7f;
margin: 20px 0;
font-weight: bold;
}
.logo {
font-size: 24px;
font-weight: bold;
position: absolute;
top: 20px;
left: 20px;
color: white;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
.particles {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
.particle {
position: absolute;
background: rgba(100, 100, 100, 0.3);
border-radius: 50%;
pointer-events: none;
}
@keyframes float {
0% {
transform: translateY(0) translateX(0);
}
50% {
transform: translateY(-20px) translateX(10px);
}
100% {
transform: translateY(0) translateX(0);
}
}
</style>
</head>
<body>
<div class="particles" id="particles"></div>
<div class="logo">VideoCompressor</div>
<div class="container screen" id="welcomeScreen">
<div class="icon">📹</div>
<h1>VideoCompressor</h1>
<p>Witaj w aplikacji do kompresji wideo! Wybierz plik wideo, który chcesz skompresować, a my zajmiemy się resztą.</p>
<button class="btn" id="selectFileBtn">Wybierz plik wideo</button>
</div>
<div class="container screen" id="compressionScreen">
<h1>Kompresowanie wideo...</h1>
<p>Trwa kompresja twojego pliku. Proszę czekać.</p>
<div class="progress-container">
<div class="progress-bar" id="progressBar">0%</div>
</div>
</div>
<div class="container screen" id="completionScreen">
<div class="icon">✅</div>
<h1>Kompresja zakończona!</h1>
<p class="completion-message">Twój plik został pomyślnie skompresowany.</p>
<div class="file-info" id="fileInfo">
<p id="filePath"></p>
</div>
<button class="btn" id="openFolderBtn">Otwórz folder</button>
<button class="btn btn-secondary" id="newCompressionBtn">Nowa kompresja</button>
</div>
<script>
var bridge = null;
var currentFilePath = "";
document.addEventListener("DOMContentLoaded", function() {
initializeApp();
});
function initializeApp() {
new QWebChannel(qt.webChannelTransport, function(channel) {
bridge = channel.objects.bridge;
setupEventListeners();
createParticles();
showWelcomeScreen();
});
}
function setupEventListeners() {
document.getElementById("selectFileBtn").addEventListener("click", function() {
if (bridge) {
bridge.selectFile();
}
});
document.getElementById("openFolderBtn").addEventListener("click", function() {
if (bridge) {
bridge.openFolder(currentFilePath);
}
});
document.getElementById("newCompressionBtn").addEventListener("click", function() {
showWelcomeScreen();
});
}
function showWelcomeScreen() {
hideAllScreens();
document.getElementById("welcomeScreen").classList.add("active");
}
function showCompressionScreen() {
hideAllScreens();
document.getElementById("compressionScreen").classList.add("active");
resetProgress();
}
function showCompletionScreen(filePath) {
hideAllScreens();
currentFilePath = filePath;
document.getElementById("filePath").textContent = "Lokalizacja: " + filePath;
document.getElementById("completionScreen").classList.add("active");
}
function hideAllScreens() {
const screens = document.querySelectorAll(".screen");
screens.forEach(screen => {
screen.classList.remove("active");
});
}
function updateProgress(val) {
const bar = document.getElementById("progressBar");
bar.style.width = val + "%";
bar.innerText = val + "%";
}
function resetProgress() {
updateProgress(0);
}
function createParticles() {
const particlesContainer = document.getElementById("particles");
const particleCount = 20;
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement("div");
particle.classList.add("particle");
const size = Math.random() * 4 + 2;
particle.style.width = size + "px";
particle.style.height = size + "px";
particle.style.left = Math.random() * 100 + "%";
particle.style.top = Math.random() * 100 + "%";
particle.style.opacity = Math.random() * 0.5 + 0.1;
const duration = Math.random() * 10 + 10;
particle.style.animation = `float ${duration}s linear infinite`;
particle.style.animationDelay = `${Math.random() * 5}s`;
particlesContainer.appendChild(particle);
}
}
</script>
</body>
</html>
"""
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()