33import os
44import subprocess
55import logging
6+ import threading
7+ import time
68
79
810# External libraries
911from rich .console import Console
12+ from tqdm import tqdm
1013
1114
1215# Internal utilities
1316from StreamingCommunity .Util .os import get_mp4decrypt_path
17+ from StreamingCommunity .Util .color import Colors
1418
1519# Variable
1620console = Console ()
1721
1822
1923# NOTE!: SAREBBE MEGLIO FARLO PER OGNI FILE DURANTE IL DOWNLOAD ... MA PER ORA LO LASCIO COSI
20- def decrypt_with_mp4decrypt (encrypted_path , kid , key , output_path = None , cleanup = True ):
24+ def decrypt_with_mp4decrypt (type , encrypted_path , kid , key , output_path = None , cleanup = True ):
2125 """
2226 Decrypt an mp4/m4s file using mp4decrypt.
2327
2428 Args:
29+ type (str): Type of file ('video' or 'audio').
2530 encrypted_path (str): Path to encrypted file.
2631 kid (str): Hexadecimal KID.
2732 key (str): Hexadecimal key.
@@ -48,15 +53,63 @@ def decrypt_with_mp4decrypt(encrypted_path, kid, key, output_path=None, cleanup=
4853 if not output_path :
4954 output_path = os .path .splitext (encrypted_path )[0 ] + "_decrypted.mp4"
5055
56+ # Get file size for progress tracking
57+ file_size = os .path .getsize (encrypted_path )
58+
5159 key_format = f"{ kid .lower ()} :{ key .lower ()} "
5260 cmd = [get_mp4decrypt_path (), "--key" , key_format , encrypted_path , output_path ]
5361 logging .info (f"Running command: { ' ' .join (cmd )} " )
5462
63+ # Create progress bar with custom format
64+ bar_format = (
65+ f"{ Colors .YELLOW } DECRYPT{ Colors .CYAN } { type } { Colors .WHITE } : "
66+ f"{ Colors .MAGENTA } {{bar:40}} "
67+ f"{ Colors .LIGHT_GREEN } {{n_fmt}}{ Colors .WHITE } /{ Colors .CYAN } {{total_fmt}} "
68+ f"{ Colors .DARK_GRAY } [{ Colors .YELLOW } {{elapsed}}{ Colors .WHITE } < { Colors .CYAN } {{remaining}}{ Colors .DARK_GRAY } ] "
69+ f"{ Colors .WHITE } {{postfix}}"
70+ )
71+
72+ progress_bar = tqdm (
73+ total = 100 ,
74+ bar_format = bar_format ,
75+ unit = "" ,
76+ ncols = 150
77+ )
78+
79+ def monitor_output_file ():
80+ """Monitor output file growth and update progress bar."""
81+ last_size = 0
82+ while True :
83+ if os .path .exists (output_path ):
84+ current_size = os .path .getsize (output_path )
85+ if current_size > 0 :
86+ progress_percent = min (int ((current_size / file_size ) * 100 ), 100 )
87+ progress_bar .n = progress_percent
88+ progress_bar .refresh ()
89+
90+ if current_size == last_size and current_size > 0 :
91+ # File stopped growing, likely finished
92+ break
93+
94+ last_size = current_size
95+
96+ time .sleep (0.1 )
97+
98+ # Start monitoring thread
99+ monitor_thread = threading .Thread (target = monitor_output_file , daemon = True )
100+ monitor_thread .start ()
101+
55102 try :
56103 result = subprocess .run (cmd , capture_output = True , text = True , timeout = 300 )
57104 except Exception as e :
105+ progress_bar .close ()
58106 console .print (f"[bold red] mp4decrypt execution failed: { e } [/bold red]" )
59107 return None
108+
109+ # Ensure progress bar reaches 100%
110+ progress_bar .n = 100
111+ progress_bar .refresh ()
112+ progress_bar .close ()
60113
61114 if result .returncode == 0 and os .path .exists (output_path ):
62115
0 commit comments