-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
143 lines (126 loc) · 5.12 KB
/
program.py
File metadata and controls
143 lines (126 loc) · 5.12 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
import os
import subprocess
from PySide6 import QtWidgets, QtCore
import sys
import time
# Create the main application
input_dir = "Input"
output_dir = "Output"
app = QtWidgets.QApplication(sys.argv)
# Define the function that will be executed when the button is pressed
def on_start_button_clicked():
# Create the directories if they don't exist
os.makedirs(input_dir, exist_ok=True)
os.makedirs(output_dir, exist_ok=True)
# Get selected encoder
selected_encoder = encoder_combo.currentText()
# Loop through all files in the input directory
for filename in os.listdir(input_dir):
# Check if the file is a .webm file
if filename.endswith(".webm"):
# Define the full path to the input and output files
input_path = os.path.join(input_dir, filename)
output_filename = filename.replace(".webm", ".mp4")
output_path = os.path.join(output_dir, output_filename)
# Construct the ffmpeg command based on selected encoder
if selected_encoder == "NVIDIA GPU (h264_nvenc)":
ffmpeg_command = [
"ffmpeg",
"-i", input_path,
"-c:v", "h264_nvenc",
"-preset", "fast",
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
output_path
]
elif selected_encoder == "AMD GPU (h264_amf)":
ffmpeg_command = [
"ffmpeg",
"-i", input_path,
"-c:v", "h264_amf",
"-quality", "speed",
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
output_path
]
elif selected_encoder == "Intel GPU (h264_qsv)":
ffmpeg_command = [
"ffmpeg",
"-i", input_path,
"-c:v", "h264_qsv",
"-preset", "fast",
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
output_path
]
else: # CPU encoding (default)
ffmpeg_command = [
"ffmpeg",
"-i", input_path,
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
output_path
]
# Run the ffmpeg command
try:
subprocess.run(ffmpeg_command, check=True)
# Wait a moment to ensure file is released
time.sleep(0.5)
# Try to remove the file with retries
for attempt in range(3):
try:
os.remove(input_path)
break
except PermissionError:
time.sleep(1) # Wait longer and try again
if attempt == 2: # Last attempt failed
print(f"Warning: Could not delete {filename} - file may be in use")
except subprocess.CalledProcessError:
print(f"Error converting {filename}, trying CPU encoding...")
# Fallback to CPU encoding if GPU fails
ffmpeg_command = [
"ffmpeg",
"-i", input_path,
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
output_path
]
subprocess.run(ffmpeg_command, check=True)
# Wait and try to remove with retries
time.sleep(0.5)
for attempt in range(3):
try:
os.remove(input_path)
break
except PermissionError:
time.sleep(1)
if attempt == 2:
print(f"Warning: Could not delete {filename} - file may be in use")
print("Conversión completada.")
# Create the main window
window = QtWidgets.QWidget()
window.setWindowTitle('WEBM to MP4 Converter')
window.setFixedSize(400, 350)
# Create the layout
layout = QtWidgets.QVBoxLayout()
# Add encoder selection
encoder_label = QtWidgets.QLabel('Select Encoder:')
encoder_combo = QtWidgets.QComboBox()
encoder_combo.addItems([
"CPU (libx264) - Default",
"NVIDIA GPU (h264_nvenc)",
"AMD GPU (h264_amf)",
"Intel GPU (h264_qsv)"
])
# Create the start button
start_button = QtWidgets.QPushButton('Start')
# Connect the button's clicked signal to the function
start_button.clicked.connect(on_start_button_clicked)
# Add widgets to layout
layout.addStretch(1)
layout.addWidget(encoder_label, alignment=QtCore.Qt.AlignCenter)
layout.addWidget(encoder_combo, alignment=QtCore.Qt.AlignCenter)
layout.addStretch(1)
layout.addWidget(start_button, alignment=QtCore.Qt.AlignCenter)
layout.addStretch(1)
# Set the layout on the window
window.setLayout(layout)
# Show the window
window.show()
# Run the application's event loop
sys.exit(app.exec())