-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_profiles.py
More file actions
365 lines (322 loc) · 10.5 KB
/
Copy pathencode_profiles.py
File metadata and controls
365 lines (322 loc) · 10.5 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
"""
Encode profile definitions and interactive profile selection.
"""
from __future__ import annotations
import subprocess
import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from convert_media import ConversionOptions
PROFILE_NAMES = ("archive", "fast", "quality")
@dataclass(frozen=True)
class EncodeProfile:
name: str
label: str
description: str
crf: int
nvenc_cq: int
use_hardware: bool | None # None = auto per file
archive: bool
hw_preset: str | None
vt_preset: str
software_preset: str
output_size_ratio: float
hw_fps_factor: float
sw_fps_factor: float
settings_summary: str
PROFILES: dict[str, EncodeProfile] = {
"archive": EncodeProfile(
name="archive",
label="Archive",
description="Balanced size and speed — recommended for libraries. Picks GPU or CPU per file.",
crf=24,
nvenc_cq=26,
use_hardware=None,
archive=False,
hw_preset="p5",
vt_preset="quality",
software_preset="medium",
output_size_ratio=0.55,
hw_fps_factor=0.85,
sw_fps_factor=1.0,
settings_summary="x265/HW auto · CRF/CQ ~24",
),
"fast": EncodeProfile(
name="fast",
label="Fast",
description="Faster conversion, larger files — good when speed matters more than size.",
crf=28,
nvenc_cq=28,
use_hardware=True,
archive=False,
hw_preset="p3",
vt_preset="speed",
software_preset="fast",
output_size_ratio=0.60,
hw_fps_factor=1.4,
sw_fps_factor=1.2,
settings_summary="NVENC p3 · CQ 28",
),
"quality": EncodeProfile(
name="quality",
label="Quality",
description="Smaller files, slower encode — CPU only; best for small batches.",
crf=20,
nvenc_cq=20,
use_hardware=False,
archive=False,
hw_preset=None,
vt_preset="quality",
software_preset="slow",
output_size_ratio=0.70,
hw_fps_factor=1.0,
sw_fps_factor=0.55,
settings_summary="x265 slow · CRF 20 · no GPU",
),
}
DEFAULT_PROFILE = "archive"
def get_profile(name: str) -> EncodeProfile:
key = name.lower()
if key not in PROFILES:
valid = ", ".join(PROFILE_NAMES)
raise ValueError(f"Unknown profile {name!r}. Choose one of: {valid}")
return PROFILES[key]
def profile_to_options_kwargs(name: str) -> dict[str, Any]:
"""Map a profile name to ConversionOptions keyword arguments."""
profile = get_profile(name)
hardware = profile.use_hardware if profile.use_hardware is not None else False
return {
"crf": profile.crf,
"hardware": hardware,
"auto_hardware": profile.use_hardware is None,
"archive": profile.archive,
"hw_preset": profile.hw_preset,
"software_preset": profile.software_preset,
"nvenc_cq": profile.nvenc_cq,
"vt_preset": profile.vt_preset,
"encode_profile": profile.name,
}
def has_legacy_encode_flags(args: Any) -> bool:
"""True when explicit legacy encode flags bypass the profile picker."""
return bool(
args.hardware
or args.archive
or args.hw_preset is not None
or args.crf != 24,
)
def check_nvenc_available() -> bool:
try:
nvidia_check = subprocess.run(
["nvidia-smi"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
if nvidia_check.returncode != 0:
return False
nvenc_check = subprocess.run(
["ffmpeg", "-encoders"],
capture_output=True,
text=True,
check=False,
)
return "hevc_nvenc" in nvenc_check.stdout
except (subprocess.CalledProcessError, FileNotFoundError, OSError):
return False
def check_videotoolbox_available() -> bool:
try:
result = subprocess.run(
["ffmpeg", "-encoders"],
capture_output=True,
text=True,
check=False,
)
return "hevc_videotoolbox" in result.stdout
except (subprocess.CalledProcessError, FileNotFoundError, OSError):
return False
def hardware_encoder_available() -> bool:
import platform
if platform.system() == "Darwin":
return check_videotoolbox_available()
if platform.system() == "Linux":
return check_nvenc_available()
return False
@dataclass
class VideoEncodeSettings:
crf: int
nvenc_cq: int
software_preset: str
hw_preset: str | None
vt_preset: str
use_hardware: bool
auto_hardware: bool
archive: bool
def settings_from_options(options: ConversionOptions) -> VideoEncodeSettings:
if options.encode_profile:
profile = get_profile(options.encode_profile)
use_hardware = (
profile.use_hardware if profile.use_hardware is not None else False
)
return VideoEncodeSettings(
crf=profile.crf,
nvenc_cq=profile.nvenc_cq,
software_preset=profile.software_preset,
hw_preset=profile.hw_preset,
vt_preset=profile.vt_preset,
use_hardware=use_hardware,
auto_hardware=profile.use_hardware is None,
archive=profile.archive,
)
nvenc_cq = options.nvenc_cq if options.nvenc_cq is not None else options.crf
return VideoEncodeSettings(
crf=options.crf,
nvenc_cq=nvenc_cq,
software_preset=options.software_preset,
hw_preset=options.hw_preset,
vt_preset=options.vt_preset,
use_hardware=options.hardware,
auto_hardware=options.auto_hardware,
archive=options.archive,
)
def resolve_use_hardware(
settings: VideoEncodeSettings,
video_stream: dict[str, Any] | None,
) -> bool:
"""Decide hardware vs software for a single file."""
if settings.auto_hardware and video_stream:
from media_analysis import determine_encode_method
method = determine_encode_method(video_stream)
want_hardware = method["recommended"] == "hardware"
if want_hardware and hardware_encoder_available():
return True
return False
if settings.use_hardware and hardware_encoder_available():
return True
return False
def build_video_encode_args(
settings: VideoEncodeSettings,
use_hardware: bool,
) -> tuple[list[str], str]:
"""Build ffmpeg video encode arguments and a log message."""
import platform
if use_hardware:
system = platform.system()
if system == "Darwin":
vt_quality = "60"
if settings.vt_preset == "quality" or settings.hw_preset == "quality":
vt_quality = "80"
elif settings.vt_preset == "speed" or settings.hw_preset == "speed":
vt_quality = "40"
elif settings.vt_preset == "balanced" or settings.hw_preset == "balanced":
vt_quality = "60"
args = [
"-c:v",
"hevc_videotoolbox",
"-q:v",
vt_quality,
"-tag:v",
"hvc1",
"-allow_sw",
"1",
]
return args, (
f"Using Apple VideoToolbox hardware acceleration with quality {vt_quality}"
)
if system == "Linux" and check_nvenc_available():
nvenc_preset = settings.hw_preset or "p4"
if nvenc_preset not in [f"p{i}" for i in range(1, 8)]:
nvenc_preset = "p4"
if settings.archive:
nvenc_preset = settings.hw_preset or "p5"
cq = settings.nvenc_cq
args = [
"-c:v",
"hevc_nvenc",
"-preset",
nvenc_preset,
"-cq",
str(cq),
"-tag:v",
"hvc1",
]
return args, (
f"Using NVIDIA hardware acceleration (NVENC) with preset {nvenc_preset}, CQ {cq}"
)
if settings.archive:
preset = "slower"
crf = settings.crf + 4
args = ["-c:v", "libx265", "-preset", preset, "-crf", str(crf)]
return args, f"Using archive mode: preset={preset}, crf={crf}"
args = [
"-c:v",
"libx265",
"-preset",
settings.software_preset,
"-crf",
str(settings.crf),
]
return args, (
f"Using software encoding: preset={settings.software_preset}, crf={settings.crf}"
)
def format_profile_cards(
file_count: int,
input_gb: float,
estimates: dict[str, Any],
) -> str:
lines = [
"",
"Choose an encoding profile:",
"",
]
for index, name in enumerate(PROFILE_NAMES, start=1):
profile = PROFILES[name]
estimate = estimates[name]
recommended = " (recommended)" if name == DEFAULT_PROFILE else ""
lines.append(
f" [{index}] {profile.label}{recommended} — {profile.description}",
)
lines.append(
f" ~{estimate.output_gb:.1f} GB output · ~{estimate.time_display} · "
f"{profile.settings_summary}",
)
lines.append("")
lines.append("Press Enter for [1], or type 1/2/3: ")
return "\n".join(lines)
def prompt_encode_profile(
file_count: int,
input_gb: float,
estimates: dict[str, Any],
stdin: Any | None = None,
) -> str:
"""Interactive profile selection. Returns profile name."""
if stdin is None:
if not sys.stdin.isatty():
print(
"Error: Interactive profile selection requires a TTY.\n"
"Use --profile archive|fast|quality, or pass legacy flags "
"(--hardware, --crf, --archive, --hw-preset).",
)
raise SystemExit(1)
input_stream = sys.stdin
else:
input_stream = stdin
archive_estimate = estimates[DEFAULT_PROFILE]
print(
f"\nFound {file_count} files · {input_gb:.1f} GB source · "
f"~{archive_estimate.time_display} total (Archive estimate)",
)
print(
format_profile_cards(file_count, input_gb, estimates),
end="",
flush=True,
)
choice = input_stream.readline().strip()
if choice == "" or choice == "1":
return DEFAULT_PROFILE
if choice == "2":
return "fast"
if choice == "3":
return "quality"
print(f"Invalid choice {choice!r}. Using {DEFAULT_PROFILE} profile.")
return DEFAULT_PROFILE