-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
205 lines (164 loc) · 7.07 KB
/
Utils.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
import platform
import re
import shutil
import subprocess
import sys
import os
import tempfile
import zipfile
from PyQt5.QtGui import QPalette, QFont, QColor
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
def setup():
"""
Simple method for some initial setup or imports based on specific operating systems, or other variables
"""
if getattr(sys, 'frozen', False):
import resources_rc
if hasattr(QApplication, 'setAttribute'):
"""
Enable high dpi scaling
"""
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
def customize_tooltips():
"""
Customize the appearance of tooltips to make them look more dim/transparent.
"""
palette = QToolTip.palette()
palette.setColor(QPalette.ToolTipBase, QColor(50, 50, 50, 180))
palette.setColor(QPalette.ToolTipText, QColor(255, 255, 255, 180))
QToolTip.setPalette(palette)
QToolTip.setFont(QFont('SansSerif', 10))
def get_ffmpeg_path(include_packaged_version=False):
"""
Determines the path of the ffmpeg executable, whether the application runs bundled or in a Python environment.
Checks for a newer version of ffmpeg installed on the computer and uses it if available.
Args:
include_packaged_version (bool): Whether to return a boolean indicating if the packaged version was used.
Returns:
tuple: A tuple containing the path to the ffmpeg executable and a boolean indicating if the packaged version was used.
"""
def get_version(ffmpeg_path):
"""
Get the version of the ffmpeg executable.
Args:
ffmpeg_path (str): The path to the ffmpeg executable.
Returns:
tuple: A tuple containing the version numbers, or None if the version couldn't be determined.
"""
try:
if platform.system() == 'Windows':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
result = subprocess.run([ffmpeg_path, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, startupinfo=startupinfo)
else:
result = subprocess.run([ffmpeg_path, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
version_line = result.stdout.split('\n')[0]
version = version_line.split()[2]
version_parts = version.split('-')[0].split('.')
version_numbers = tuple(int(part) for part in version_parts if part.isdigit())
return version_numbers
except FileNotFoundError:
return None
except Exception as e:
return None
installed_ffmpeg_path = 'ffmpeg'
if platform.system() == 'Windows':
if getattr(sys, 'frozen', False):
packaged_ffmpeg_path = os.path.join(sys._MEIPASS, 'ffmpeg.exe')
else:
zip_path = os.path.join(os.path.dirname(__file__), 'resources', 'ffmpeg.zip')
extract_dir = tempfile.mkdtemp()
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extract('ffmpeg.exe', extract_dir)
packaged_ffmpeg_path = os.path.join(extract_dir, 'ffmpeg.exe')
else:
packaged_ffmpeg_path = None
packaged_version = get_version(packaged_ffmpeg_path) if packaged_ffmpeg_path else None
installed_version = get_version(installed_ffmpeg_path)
if installed_version and packaged_version:
if installed_version > packaged_version:
if platform.system() == 'Windows' and not getattr(sys, 'frozen', False):
shutil.rmtree(extract_dir)
return (installed_ffmpeg_path, False) if include_packaged_version else installed_ffmpeg_path
else:
return (packaged_ffmpeg_path, True) if include_packaged_version else packaged_ffmpeg_path
elif installed_version:
return (installed_ffmpeg_path, False) if include_packaged_version else installed_ffmpeg_path
elif packaged_ffmpeg_path:
return (packaged_ffmpeg_path, True) if include_packaged_version else packaged_ffmpeg_path
else:
return (None, None) if include_packaged_version else None
def create_file_selection_box(text, buttons):
"""
Create a file selection message box with multiple buttons.
Args:
text (str): The message to display.
buttons (list): A list of buttons to include in the message box.
Returns:
QMessageBox: The created message box.
"""
msg = QMessageBox()
msg.setIcon(QMessageBox.Question)
msg.setWindowTitle("Flere filtyper funnet")
msg.setText(text)
for button in buttons:
msg.addButton(button, QMessageBox.ActionRole)
msg.addButton(QMessageBox.Cancel)
return msg
def select_file_from_list(files, file_type_description):
"""
Open a file selection dialog to select a file from a list.
Args:
files (list): A list of file paths to select from.
file_type_description (str): A description of the file type.
Returns:
str: The selected file path, or None if no file was selected.
"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.ExistingFile)
dialog.setNameFilter("{} Files (*.{})".format(file_type_description, file_type_description))
dialog.setViewMode(QFileDialog.Detail)
dialog.setDirectory(os.path.dirname(files[0]))
if dialog.exec_():
selected_file = dialog.selectedFiles()[0]
return selected_file
return None
def extract_number(filename):
"""
Extract the first number found in a filename.
Args:
filename (str): The filename to extract the number from.
Returns:
int: The extracted number, or float('inf') if no number was found.
"""
base = os.path.basename(filename)
match = re.search(r'(\d+)', base)
return int(match.group(1)) if match else float('inf')
def cuda_available():
"""
Check if CUDA is available on the system.
This function checks for the presence of CUDA support in FFmpeg and
the availability of an NVIDIA GPU using 'nvidia-smi'.
Returns:
bool: True if CUDA is available, False otherwise.
The function performs the following checks:
1. Runs `ffmpeg -hwaccels` to check if CUDA is listed among available hardware accelerations.
2. If CUDA is found in the FFmpeg output, runs `nvidia-smi` to check for the presence of an NVIDIA GPU.
3. Returns True if both checks pass, otherwise returns False.
Exceptions:
- Handles FileNotFoundError if 'nvidia-smi' is not found.
- Catches and prints any other exceptions that occur during the checks.
"""
try:
result = subprocess.run(['ffmpeg', '-hwaccels'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if 'cuda' in result.stdout:
result = subprocess.run(['nvidia-smi'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
return False
return False
except FileNotFoundError:
return False
except Exception as e:
return False