-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFFMpeg.py
1281 lines (1122 loc) · 38.5 KB
/
FFMpeg.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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os
import re
import sys
import json
import time
import logging
import subprocess as SP
import datetime as TM
from collections import defaultdict
from dataclasses import dataclass
from typing import List, Dict, Tuple, Any, Optional
# External references from your code
from My_Utils import * # e.g. hm_sz, hm_time, divd_strn, stmpd_rad_str, perf_monitor
from Yaml import * # if needed
# Set up logging (simple example)
logging.basicConfig(
level=logging.WARNING,
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
datefmt="%H:%M:%S"
)
logger = logging.getLogger(__name__)
# Paths to FFmpeg/FFprobe executables (update if needed)
ffmpg_bin = r"C:\Program Files\ffmpeg\bin"
ffmpeg = os.path.join(ffmpg_bin, "ffmpeg.exe")
ffprob = os.path.join(ffmpg_bin, "ffprobe.exe")
if not os.path.exists(ffmpeg) or not os.path.exists(ffprob):
raise OSError(f"FFmpeg or FFprobe not found in {ffmpg_bin}.")
logger.info(
"Ffmpeg version: %s",
SP.run([ffmpeg, "-version"], stdout=SP.PIPE)
.stdout.decode("utf-8")[15:20]
)
# Some global or shared constants (rather than global variables)
Skip_key = "AlreadyEncoded"
TmpF_Ex = "_out.mp4"
###############################################################################
# DATA CLASSES & CONTEXT
###############################################################################
@dataclass
class VideoContext:
"""Holds metadata about the video file being processed."""
vid_width: int = 0
vid_height: int = 0
vid_length: int = 0 # total duration in seconds
vid_bitrate: int = 0
total_frames: int = 0
###############################################################################
# FFPROBE
###############################################################################
@perf_monitor
def ffprobe_run(
input_file: str,
execu: str = ffprob,
de_bug: bool = False
) -> dict:
"""
Runs ffprobe to extract media information from a file and returns JSON data.
Raises exceptions on errors or if no valid JSON is returned.
"""
if not input_file:
raise FileNotFoundError("No input_file provided.")
cmd = [
execu, "-i", input_file,
"-hide_banner",
"-analyzeduration", "100000000",
"-probesize", "50000000",
"-v", "fatal", # quiet, panic, fatal, error, warning, info, verbose, debug, trace
"-show_programs",
"-show_format",
"-show_streams",
"-show_error",
"-show_data",
"-show_private_data",
"-of", "json"
]
try:
out = SP.run(cmd, stdout=SP.PIPE, stderr=SP.PIPE, check=True)
jsn_out = json.loads(out.stdout.decode("utf-8"))
if not jsn_out or "error" in jsn_out:
err_msg = jsn_out.get("error") if jsn_out else f"{input_file} no JSON output"
logger.error("ffprobe error: %s", err_msg)
raise ValueError(f"ffprobe error: {err_msg}")
return jsn_out
except (FileNotFoundError, SP.CalledProcessError, SP.TimeoutExpired, json.JSONDecodeError) as e:
raise Exception(f"ffprobe_run error: {e} in file: {input_file}") from e
###############################################################################
# RUN FFMPEG
###############################################################################
@perf_monitor
def run_ffm(args: List[str], de_bug: bool = False) -> bool:
"""
Run an ffmpeg command. If `de_bug` is True, print full output; otherwise, show a spinner.
Returns True if command succeeds, False otherwise.
"""
logger.debug("run_ffm: %s", " ".join(args))
if de_bug:
print("run_ffm: %s", " ".join(args))
if not args:
logger.error("run_ffm: No arguments provided.")
return False
try:
if de_bug:
# Debug mode: Capture stdout/stderr and print them
print ("Running ffmpeg in debug mode.\n%s", " ".join(args))
process = SP.run(args, stdout=SP.PIPE, stderr=SP.STDOUT)
print ("Stdout:\n%s", process.stdout.decode("utf-8", errors="replace"))
if process.returncode != 0:
print ("FFmpeg failed with return code %d", process.returncode)
return False
time.sleep(1)
return True
else:
spin_char = "|/-o+\\"
spinner_counter = 0
with SP.Popen ( args,
stdout=SP.PIPE,
stderr=SP.STDOUT,
text=True, # same as universal_newlines=True in modern Python
encoding="utf-8", # or "cp437", "cp65001",
errors="replace"
) as process:
for line in process.stdout:
# show_progrs is your custom function that parses ffmpeg progress
if show_progrs(line, spin_char[spinner_counter % len(spin_char)], de_bug=False):
spinner_counter += 1
# print ( SP.PIPE )
_, _ = process.communicate()
if process.returncode != 0:
print ("FFmpeg failed with return code %d", process.returncode)
return False
return True
except Exception as e:
logger.exception("run_ffm exception: %s", e)
return False
@perf_monitor
def ffmpeg_run(
input_file: str,
ff_com: List[str],
skip_it: bool,
execu: str = "ffmpeg",
de_bug: bool = False,
max_retries: int = 2,
retry_delay: int = 2
) -> Optional[str]:
"""
Builds and runs an FFmpeg command with optional retries. Returns the output file
name on success or None on skip/failure.
"""
if not input_file or skip_it:
# print("Skipping ffmpeg_run: skip_it=%s, input_file=%s", skip_it, input_file)
return None
# logger.info("ffmpeg_run start: %s", input_file)
file_name, _ = os.path.splitext(os.path.basename(input_file))
out_file = os.path.normpath("_" + stmpd_rad_str(7, file_name[0:25]))
out_file = re.sub(r"[^\w\s_-]+", "", out_file).strip().replace(" ", "_") + TmpF_Ex
# Attempt to get ffmpeg version
try:
ffmpeg_vers = SP.check_output([execu, "-version"]).decode("utf-8").splitlines()[0].split()[2]
except SP.CalledProcessError as e:
logger.error("Error getting ffmpeg version: %s", e)
return None
# Head + user command + tail
ff_head = [
execu, "-thread_queue_size", "24",
"-i", input_file,
"-hide_banner"
]
ff_tail = [
"-metadata", f"title={file_name}",
"-metadata", f"comment={Skip_key}",
"-metadata", f"encoder=ffmpeg {ffmpeg_vers}",
"-metadata", "copyright=2024",
"-metadata", "author=Encoded by GeoHab",
"-movflags", "+faststart", # Place moov atom at the beginning for fast start
"-fflags", "+fastseek", # Enable fast seeking
"-fflags", "+genpts", # Generate presentation timestamps
"-keyint_min", "30", # Minimum interval between keyframes
"-g", "60", # Set the GOP (Group of Pictures) size
"-y", out_file
]
for attempt in range(1, max_retries + 1):
try:
if attempt > 1:
time.sleep(retry_delay)
logger.warning("Retry attempt %d...", attempt)
# In debug mode for the second attempt
ff_head = [execu, "-report", "-loglevel", "verbose", "-i", input_file, "-hide_banner"]
full_cmd = ff_head + ff_com + ff_tail
if run_ffm(full_cmd, de_bug=de_bug):
logger.info("Successfully created file: %s", out_file)
return out_file
except Exception as e:
logger.error("ffmpeg_run attempt %d failed: %s", attempt, e)
if attempt == max_retries:
msg = f"Failed after {attempt} attempts."
logger.error(msg)
raise Exception(msg)
return None
###############################################################################
# PARSE VIDEO & FRIENDS
###############################################################################
@perf_monitor
def get_encoder_options(
codec_name: str,
is_10bit: bool,
bit_rate: int,
use_hw_accel: bool = False
) -> List[str]:
"""
Returns a list of FFmpeg arguments to encode with x265 or QSV for streaming-friendly output.
"""
# Keep your existing 'target_quality' = 'as_is'
target_quality = "as_is"
quality_presets = {
"low": {
"bitrate": (bit_rate // (1024 * 3)),
"quality": 26
},
"medium": {
"bitrate": (bit_rate // (1024 * 1.5)),
"quality": 24
},
"as_is": {
"bitrate": (bit_rate // 1024),
"quality": 21
},
"high": {
"bitrate": (bit_rate // (1024 * 0.75)),
"quality": 20
},
"higher": {
"bitrate": (bit_rate // (1024 * 0.5)),
"quality": 18
}
}
preset = quality_presets[target_quality]
base_target_bitrate = int(preset["bitrate"])
global_quality = preset["quality"]
# If 10-bit source, bump up the bitrate slightly
if is_10bit:
base_target_bitrate = int(base_target_bitrate * 1.25)
# Convert to 'k'
target_bitrate = f"{base_target_bitrate}k"
max_bitrate_int = int(base_target_bitrate * 1.5)
max_bitrate = f"{max_bitrate_int}k"
bufsize_int = max_bitrate_int * 2
bufsize = f"{bufsize_int}k"
# Hardware QSV path
if use_hw_accel:
hw_pix_fmt = "p010le" if is_10bit else "nv12"
return [
"hevc_qsv",
"-load_plugin", "hevc_hw",
"-init_hw_device", "qsv=qsv:MFX_IMPL_hw_any",
"-filter_hw_device", "qsv",
"-pix_fmt", hw_pix_fmt,
"-b:v", target_bitrate,
"-maxrate", max_bitrate,
"-bufsize", bufsize,
"-look_ahead", "1",
"-look_ahead_depth", "90",
"-global_quality", str(round(global_quality)),
"-rc:v", "vbr_la",
"-preset", "slow"
]
# Software x265 path
sw_pix_fmt = "yuv420p10le" if is_10bit else "yuv420p"
x265_params_str = (
"open-gop=0:"
"keyint=60:"
"min-keyint=30:"
"scenecut=40:"
"bframes=3:"
"b-adapt=2:"
"psy-rd=1:"
"aq-mode=3:"
"aq-strength=0.8:"
"deblock=1,1:"
"me=umh:"
"subme=7"
)
return [
"libx265",
"-x265-params", x265_params_str,
"-pix_fmt", sw_pix_fmt,
"-crf", str(round(global_quality)),
"-b:v", target_bitrate,
"-maxrate", max_bitrate,
"-bufsize", bufsize,
"-preset", "slow"
]
@perf_monitor
def parse_video(
streams_in: List[Dict[str, Any]],
context: VideoContext,
de_bug: bool = False,
skip_it: bool = False
) -> Tuple[List[str], bool]:
global glb_vidolen
glb_vidolen = context.vid_length
logger.debug("parse_video start, skip_it=%s", skip_it)
ff_video = []
skip_all = True
for idx, stream in enumerate(streams_in):
if "codec_name" not in stream:
raise Exception(f"No codec_name in video stream {stream}")
codec_name = stream.get("codec_name", "XXX")
pix_fmt = stream.get("pix_fmt", "")
handler_name = stream.get("tags", {}).get("handler_name", "Unknown")
frm_rate = divd_strn(stream.get("r_frame_rate", "25"))
this_bitrate = int(stream.get("bit_rate", context.vid_bitrate * 0.8))
if codec_name.lower() == 'mjpeg':
print (f" |<V:{idx:2}>| > Skip {codec_name:6} |" )
skip_all = False
continue
# Compute total frames
context.total_frames = round(frm_rate * context.vid_length)
# Decide maximum allowed bitrate
max_vid_btrt = 3620000
bit_depth_str = "8-bit"
if pix_fmt.endswith("10le"):
bit_depth_str = "10-bit"
max_vid_btrt = int(max_vid_btrt * 1.25)
# Final target bitrate for this stream
btrt = min(this_bitrate * 1.1, max_vid_btrt)
# Decide if we need to scale
output_resolutions = [
(7600, 4300, "8K"),
(3800, 2100, "4K"),
(2100, 1920, "2K"),
(1280, 720, "HD")
]
output_label = "SD"
for w, h, label in output_resolutions:
if context.vid_width >= w or context.vid_height >= h:
output_label = label
break
needs_scaling = (output_label in ["2K", "4K", "8K"])
# The combined condition:
# - Must be HEVC
# - Must have bitrate <= max
# - Must not need scaling
already_streaming_friendly = (
codec_name.lower() == "hevc"
and this_bitrate <= max_vid_btrt
and not needs_scaling
)
# Build ffmpeg args
ff_vid = ["-map", f"0:v:{idx}", f"-c:v:{idx}"]
extra_msg = ""
if already_streaming_friendly:
# Just copy
ff_vid.append("copy")
extra_msg = f"=> Copy (x265 < {hm_sz(max_vid_btrt)}, stream OK)"
else:
# Re-encode with streaming-friendly logic
encoder_opts = get_encoder_options(
codec_name,
pix_fmt.endswith("10le"),
int(btrt),
True # use_hw_accel = True
)
ff_vid.extend(encoder_opts)
skip_all = False
extra_msg = f" Re-encode: {hm_sz(btrt)}"
if needs_scaling:
use_hw_accel = True
max_width = 1920 # Adjust as needed
max_height = 1080 # Adjust as needed
if context.vid_width <= 0 or context.vid_height <= 0:
raise ValueError("Invalid video dimensions: Width and height must be positive.")
aspect_ratio = context.vid_width / context.vid_height
if context.vid_width > max_width or context.vid_height > max_height:
if max_width / aspect_ratio <= max_height:
scaled_width = max_width
scaled_height = round(max_width / aspect_ratio / 2) * 2
else:
scaled_height = max_height
scaled_width = round(max_height * aspect_ratio / 2) * 2
else:
scaled_width = context.vid_width
scaled_height = context.vid_height
if scaled_width <= 0 or scaled_height <= 0:
raise ValueError("Calculated dimensions are invalid for scaling.")
scale_pad_chain = (
f"scale={scaled_width}:{scaled_height}:force_original_aspect_ratio=decrease,"
# f"pad={max_width}:{max_height}:(ow-iw)/2:(oh-ih)/2"
)
# filter_chain = "hqdn3d=1.5:1.5:6:6, unsharp=5:5:0.8:3:3:0.4"
# filter_chain = "hqdn3d=1.0:1.0:3:3, unsharp=5:5:0.6:3:3:0.3"
filter_chain = "nlmeans=s=2:pc=1, unsharp=5:5:0.7:3:3:0.35"
is_10bit = True
hw_pix_fmt = "p010le" if is_10bit else "nv12"
if use_hw_accel:
# Initialize QSV only if not already initialized
if "-init_hw_device" not in ff_vid:
ff_vid.extend([
"-init_hw_device", "qsv=qsv:MFX_IMPL_hw_any", # Initialize QSV hardware once
"-filter_hw_device", "qsv"
])
ff_vid.extend([
"-load_plugin", "hevc_hw",
"-vf", scale_pad_chain, # Scaling and padding
"-pix_fmt", hw_pix_fmt,
"-c:v", "hevc_qsv",
"-b:v", str(min(max_vid_btrt, int(btrt))), # Limit bitrate
"-global_quality", "20", # Set global quality for QSV
"-look_ahead", "1",
"-look_ahead_depth", "90",
"-rc:v", "vbr_la",
"-preset", "slow"
])
extra_msg += f"| HW Accelerated Scaling: {context.vid_width}x{context.vid_height} -> {scaled_width}x{scaled_height}"
else:
ff_vid.extend([
"-vf", f"{scale_pad_chain},{filter_chain}",
"-pix_fmt", "yuv420p10le",
"-crf", "20",
"-b:v", str(min(max_vid_btrt, int(btrt))), # Limit bitrate
"-c:v", "libx265",
"-preset", "slow"
])
extra_msg += f"| SW Scaling: {context.vid_width}x{context.vid_height} -> {scaled_width}x{scaled_height}"
skip_all = False
# Update handler name if needed
desired_handler = "VideoHandler x265"
if handler_name != desired_handler:
ff_vid.extend([
f"-metadata:s:v:{idx}",
f"handler_name={desired_handler}"
])
skip_all = False
extra_msg += f"| Handler => {desired_handler}"
# Print log
msg = (
f"|<V:{idx:2}>|{codec_name:^8}|{context.vid_width}x{context.vid_height}|"
f"{bit_depth_str}|Bitrate: {hm_sz(this_bitrate,'bits')}|Frames: {hm_sz(context.total_frames,'frames')}|{extra_msg}|"
)
print(f"\033[91m {msg}\033[0m")
ff_video += ff_vid
if de_bug:
logger.debug("Stream %s => %s", idx, ff_vid)
if skip_all :
logger.info("Skipping video because skip_all=%s and skip_it=%s", skip_all, skip_it)
print(" .Skip: Video")
return ff_video, skip_all
###############################################################################
# PARSE AUDIO
###############################################################################
@perf_monitor
def parse_audio(
streams: List[Dict[str, Any]],
de_bug: bool = False
) -> Tuple[List[str], bool]:
"""
Parse and process audio streams, prioritizing the best English stream
based on channels and bitrate.
"""
ffmpeg_audio_options: List[str] = []
all_skippable = True
best_stream = None
prev_default_idx = None
extracted_data = []
# Analyze streams
for idx, audio_stream in enumerate(streams):
codec_name = audio_stream.get("codec_name", "unknown")
channels = int(audio_stream.get("channels", -1))
bitrate = int(audio_stream.get("bit_rate", 0))
language = audio_stream.get("tags", {}).get("language", "und")
disposition = audio_stream.get("disposition", {})
dispo_default = int(disposition.get("default", 0))
handler_name = audio_stream.get("tags", {}).get("handler_name", "Unknown")
sample_rate = audio_stream.get("sample_rate", "N/A")
extracted_data.append({
"codec_name": codec_name,
"channels": channels,
"bitrate": bitrate,
"language": language,
"dispo_default": dispo_default,
"handler_name": handler_name,
"sample_rate": sample_rate
})
if dispo_default:
prev_default_idx = idx
# Pick best English by channels then bitrate
if language == "eng":
if (best_stream is None
or channels > best_stream["channels"]
or (channels == best_stream["channels"] and bitrate > best_stream["bitrate"])):
best_stream = extracted_data[-1]
# Assign disposition & generate ffmpeg commands
for idx, data in enumerate(extracted_data):
copy_codec = (
data["codec_name"] in ("aac", "vorbis", "mp3", "opus")
and data["channels"] <= 8
)
stream_opts = ["-map", f"0:a:{idx}"]
if copy_codec:
stream_opts.extend([f"-c:a:{idx}", "copy"])
else:
# re-encode to e.g. libvorbis
stream_opts.extend([f"-c:a:{idx}", "libvorbis", "-q:a", "8"])
# Update metadata if needed
if data["handler_name"] != "SoundHandler":
stream_opts.extend([
f"-metadata:s:a:{idx}",
"handler_name=SoundHandler"
])
# Disposition
if data == best_stream:
stream_opts.extend([f"-disposition:a:{idx}", "default"])
if prev_default_idx is None or best_stream != extracted_data[prev_default_idx]:
all_skippable = False
else:
stream_opts.extend([f"-disposition:a:{idx}", "none"])
ffmpeg_audio_options.extend(stream_opts)
msg = (
f"|<A:{idx:2}>|{data['codec_name']:^8}|"
f"Br:{data['bitrate']:>7}|{data['language']}|"
f"Frq:{data['sample_rate']:>6}|Ch:{data['channels']}|"
f"Dis:{data['dispo_default']}|Handler:{data['handler_name']}"
)
# Additional notes
if data["dispo_default"]:
msg += "|Was default"
if data == best_stream:
msg += "|Is new default"
print(f"\033[92m {msg}\033[0m")
# Decide skip
skip_audio = (
len(streams) == 1
or (best_stream and prev_default_idx is not None and best_stream == extracted_data[prev_default_idx])
)
if skip_audio:
all_skippable = True
logger.info("Skipping Audio")
print(" .Skip: Audio")
if de_bug:
logger.debug("Audio opts: %s", ffmpeg_audio_options)
return ffmpeg_audio_options, all_skippable
###############################################################################
# PARSE SUBTITLES
###############################################################################
# Example: Some global or previously defined variable
#Keep_langua = ["eng", "spa", "fre"] # Adjust to your needs
def _score_english_sub(codec_name: str, disposition: Dict[str, int], handler_name: str) -> int:
"""
Assign a 'score' to an English subtitle. Higher means more preferred.
You can customize these rules as you see fit.
"""
score = 0
# Prefer mov_text over subrip
if codec_name == "mov_text":
score += 3
elif codec_name == "subrip":
score += 2
# Bonus points if it's forced or default in the source
if disposition.get("forced", 0) == 1:
score += 2
if disposition.get("default", 0) == 1:
score += 1
# Bonus if the handler_name is mov_text
if handler_name.lower() == "mov_text":
score += 1
return score
def parse_subtl(
streams_in: List[Dict[str, Any]],
de_bug: bool = False
) -> Tuple[List[str], bool]:
"""
Parse and extract data from subtitle streams, removing some, keeping others.
Enhanced so that the "best" English subtitle is forced to be default.
"""
ff_subttl = []
all_skippable = True
# First, find the "best" English subtitle (highest score).
best_eng_idx = -1
best_eng_score = float("-inf")
for idx, this_sub in enumerate(streams_in):
# Extract fields we'll need for scoring
codec_name = this_sub.get("codec_name", "unknown?")
disposition = this_sub.get("disposition", {"forced": 0, "default": 0})
tags = this_sub.get("tags", {})
handler_name = tags.get("handler_name", "Unknown")
language = tags.get("language", "und")
# Only compute a score if it's subrip/mov_text AND English
if codec_name in ("subrip", "mov_text") and language == "eng":
s = _score_english_sub(codec_name, disposition, handler_name)
if s > best_eng_score:
best_eng_score = s
best_eng_idx = idx
#
# Now do the main pass: keep/remove subtitles and set dispositions.
#
for idx, this_sub in enumerate(streams_in):
ff_sub = []
extra = ""
metadata_changed = False
codec_name = this_sub.get("codec_name", "unknown?")
codec_type = this_sub.get("codec_type", "unknown?")
disposition = this_sub.get("disposition", {"forced": 0, "default": 0})
tags = this_sub.get("tags", {})
handler_name = tags.get("handler_name", "Unknown")
language = tags.get("language", "und")
if codec_name in ("hdmv_pgs_subtitle", "dvd_subtitle", "ass", "unknown?"):
# Remove these
ff_sub = ["-map", f"-0:s:{idx}"]
extra += f" Delete: {codec_name} {language} |"
all_skippable = False
elif codec_name in ("subrip", "mov_text"):
# Keep subrip/mov_text
ff_sub = ["-map", f"0:s:{idx}"]
# Is this English?
if language == "eng":
extra += f"Keep: {codec_name} {language}|"
# If this stream is the best English one, set default
if idx == best_eng_idx and best_eng_idx != -1:
extra += "Set to Default|"
ff_sub.extend([
f"-c:s:{idx}", "mov_text",
f"-metadata:s:s:{idx}", f"language={language}",
f"-disposition:s:s:{idx}", "default"
])
else:
# It's English but not the best
extra += "Not Default|"
ff_sub.extend([
f"-c:s:{idx}", "mov_text",
f"-metadata:s:s:{idx}", f"language={language}",
f"-disposition:s:s:{idx}", "0"
])
# If it's a non-English language we want to keep
elif language in Keep_langua:
extra += f"Keep: {codec_name} {language}"
ff_sub.extend([
f"-c:s:{idx}", "mov_text",
f"-metadata:s:s:{idx}", f"language={language}",
f"-disposition:s:s:{idx}", "0"
])
else:
# Remove any other languages not in Keep_langua
ff_sub = ["-map", f"-0:s:{idx}"]
extra += f" Delete: {codec_name} {language} X"
# Handler name fix
if handler_name != "mov_text":
extra += f" handler_name: {handler_name} -> mov_text"
ff_sub.extend([
f"-metadata:s:s:{idx}",
"handler_name=mov_text"
])
metadata_changed = True
# Otherwise, remove unrecognized formats (if any)
else:
ff_sub = ["-map", f"-0:s:{idx}"]
extra += f" Delete: {codec_name} {language} X"
all_skippable = False
# Build the debug message in your original style
msg = (
f"|<S:{idx:2}>|{codec_name[:8]:^8}|{codec_type[:8]}|"
f"{language:3}|Disp: default={disposition.get('default',0)}, forced={disposition.get('forced',0)}|"
f"{extra}"
)
print(f"\033[94m {msg}\033[0m")
ff_subttl += ff_sub
if metadata_changed:
all_skippable = False
# Force mov_text for all kept subtitles (global setting)
ff_subttl.extend(["-c:s", "mov_text"])
# If we ended up removing everything, all_skippable might be True
if all_skippable:
logger.info("Skipping Subtitles")
print(" .Skip: Subtitle")
return ff_subttl, all_skippable
###############################################################################
# PARSE EXTRA DATA
###############################################################################
@perf_monitor
def parse_extrd(
streams_in: List[Dict[str, Any]],
de_bug: bool = False
) -> Tuple[List[str], bool]:
"""
Parse data streams. Usually we discard them, or at least we map them carefully.
"""
ff_data = []
skip_all = True
for idx, data_stream in enumerate(streams_in):
codec_name = data_stream.get("codec_name", "")
codec_long_name = data_stream.get("codec_long_name", "")
codec_type = data_stream.get("codec_type", "")
tags = data_stream.get("tags", {})
handler_name = tags.get("handler_name", "Unknown")
msj = f" |<D:{idx:2}>|{codec_name:^8}| {codec_long_name:<9}| {codec_type:^11} | {handler_name}"
if handler_name == 'SubtitleHandler':
msj += "|Keep Subtitle|"
print(msj)
print (" .Skip: Data")
return [], True
# Potential logic: keep or remove
ff_dd = ["-map", f"-0:d:{idx}"]
ff_data += ff_dd
# If you do something special with data, put it here
return ff_data, skip_all
###############################################################################
# PARSE FORMAT / MAIN PARSER
###############################################################################
@perf_monitor
def parse_frmat(
input_file: str,
metadata: Dict[str, Any],
de_bug: bool
) -> Tuple[List[str], bool]:
"""
Parses the top-level 'format' and 'streams' from ffprobe data,
then delegates to parse_video, parse_audio, parse_subtl, etc.
Returns combined FFmpeg args and skip flag.
"""
_format = metadata.get("format", {})
_streams = metadata.get("streams", [])
if not _format:
raise ValueError(f"'format' not found in metadata:\n{json.dumps(metadata, indent=2)}")
if not _streams:
raise ValueError(f"'streams' not found in metadata:\n{json.dumps(metadata, indent=2)}")
filename = _format.get("filename", "No_file_name")
size = int(float(_format.get("size", 0)))
duration = int(float(_format.get("duration", 0.0)))
bitrate = int(_format.get("bit_rate", 0))
nb_streams = int(_format.get("nb_streams", 0))
nb_programs = int(_format.get("nb_programs", 0))
tags = _format.get("tags", {})
f_comment = tags.get("comment", "No_comment")
title = tags.get("title", "No_title")
# We build a context object for video
context = VideoContext(
vid_width=0, # Will set later from parse_video
vid_height=0,
vid_length=duration,
vid_bitrate=bitrate,
total_frames=0
)
# For logging
summary_msg = (
f"Title={title} | "
f"Size={hm_sz(size)} | "
f"Bitrate={hm_sz(bitrate)} | "
f"Length={hm_time(duration)} | "
f"Streams={nb_streams} | "
f"Programs={nb_programs}"
)
logger.info("parse_frmat: %s", summary_msg)
# Build stream groups
from collections import defaultdict
streams_by_type = defaultdict(list)
for s in _streams:
codec_type = s.get("codec_type", "?")
streams_by_type[codec_type].append(s)
video_streams = streams_by_type["video"]
audio_streams = streams_by_type["audio"]
subtl_streams = streams_by_type["subtitle"]
datax_streams = streams_by_type["data"]
# Also build a stream_counts dict for your console print
stream_counts = {
"V": len(video_streams),
"A": len(audio_streams),
"S": len(subtl_streams),
"D": len(datax_streams),
"Prog": nb_programs,
"Strms": nb_streams
}
# Summaries for console
summary_msg = (
f" |=Title|{title}|\n"
f" |>FRMT<|Size: {hm_sz(size)}|Bitrate: {hm_sz(bitrate)}|Length: {hm_time(duration)}|"
)
summary_msg += ''.join([f"{key}:{count}|" for key, count in stream_counts.items() if count != 0])
print(f"\033[96m{summary_msg}\033[0m")
# Skip logic
fnam, ext = os.path.splitext(os.path.basename(input_file))
good_fname = (fnam == title and ext == ".mp4")
skip_it = (good_fname and f_comment == Skip_key)
if skip_it:
print(" .Skip: Format")
# Parse video
ff_com = []
v_skip = True
if video_streams:
# We set context.vid_width / context.vid_height from the first video stream
first_vid = video_streams[0]
context.vid_width = first_vid.get("width", 2)
context.vid_height = first_vid.get("height", 1)
v_cmd, v_skip = parse_video(video_streams, context, de_bug, skip_it)
ff_com.extend(v_cmd)
else:
logger.warning("No Video in: %s", input_file)
time.sleep(2)
return [], True
# Parse audio
a_skip = True
if audio_streams:
a_cmd, a_skip = parse_audio(audio_streams, de_bug)
ff_com.extend(a_cmd)
else:
logger.warning("No Audio in: %s", input_file)
time.sleep(2)
return [], True
# Parse subtitles
s_skip = True
if subtl_streams:
s_cmd, s_skip = parse_subtl(subtl_streams, de_bug)
ff_com.extend(s_cmd)
else:
# Try external subtitles?
s_cmd, s_skip = add_subtl_from_file(input_file, de_bug)
ff_com.extend(s_cmd)
# Parse data
d_skip = True
if datax_streams:
d_cmd, d_skip = parse_extrd(datax_streams, de_bug)
ff_com.extend(d_cmd)
# we might do skip logic, etc.
# Combine skip logic
final_skip = skip_it and v_skip and a_skip and s_skip and d_skip
return ff_com, final_skip
###############################################################################
# ADD SUBTITLE FROM FILE
###############################################################################
@perf_monitor
def add_subtl_from_file(input_file: str, de_bug: bool) -> Tuple[List[str], bool]:
"""
Searches for a subtitle file in the same directory as the input file.
Returns a (list_of_subtitle_args, skip_flag).
"""
directory = os.path.dirname(input_file)
largest_file = None
extensions = [".srt", ".ass", ".sub"]
for file in os.listdir(directory):
if any(file.endswith(ext) for ext in extensions):
filepath = os.path.join(directory, file)
if not largest_file or os.path.getsize(filepath) > os.path.getsize(largest_file):
largest_file = filepath
if largest_file:
if de_bug:
logger.debug("Found external subtitle: %s", largest_file)
# If you want to auto-add:
# ff_sub = ["-i", largest_file, "-map", "1:0", "-c:s", "mov_text"]
# return ff_sub, False
# For now, just skip:
return [], True
else:
if de_bug:
logger.debug("No external subtitle found.")
return [], True
###############################################################################
# FILE INFO PARSER
###############################################################################
@perf_monitor
def parse_finfo(input_file: str, metadata: Dict[str, Any], de_bug: bool = False) -> Tuple[Any, bool]:
"""
High-level entry point that uses parse_frmat to get the combined ffmpeg command & skip flag.
"""
if not input_file or not metadata:
logger.error("parse_finfo: missing file or metadata")
return [], True
ff_run_cmd, skip_it = parse_frmat(input_file, metadata, de_bug)
return ff_run_cmd, skip_it