2323 clips_array ,
2424)
2525
26+
2627class Controller :
2728 """
2829 Taking an input directory of files, convert them to a multitude of formats.
@@ -32,7 +33,9 @@ class Controller:
3233
3334 def __init__ (self , job_id = None , shared_progress_dict = None ):
3435 # Setting up progress logger with optional web progress tracking
35- self .prog_logger = ProgLogger (job_id = job_id , shared_progress_dict = shared_progress_dict )
36+ self .prog_logger = ProgLogger (
37+ job_id = job_id , shared_progress_dict = shared_progress_dict
38+ )
3639 # Get locale
3740 self .locale = lang .get_system_language ()
3841 # Setting up event logger and format
@@ -564,21 +567,29 @@ def handle_file_event(event_type: str, file_path: str) -> None:
564567
565568 def split (self , file_paths : dict , page_ranges ) -> None :
566569 for doc_path_set in file_paths [Category .DOCUMENT ]:
567- if hasattr (self .prog_logger , 'shared_progress_dict' ) and self .prog_logger .shared_progress_dict :
570+ if (
571+ hasattr (self .prog_logger , "shared_progress_dict" )
572+ and self .prog_logger .shared_progress_dict
573+ ):
568574 import threading
575+
569576 with threading .Lock ():
570577 if self .prog_logger .job_id in self .prog_logger .shared_progress_dict :
571- self .prog_logger .shared_progress_dict [self .prog_logger .job_id ].update ({
572- 'status' : f'splitting { doc_path_set [1 ]} ' ,
573- 'last_updated' : time .time ()
574- })
578+ self .prog_logger .shared_progress_dict [
579+ self .prog_logger .job_id
580+ ].update (
581+ {
582+ "status" : f"splitting { doc_path_set [1 ]} " ,
583+ "last_updated" : time .time (),
584+ }
585+ )
575586 if doc_path_set [2 ] == "pdf" :
576587 self .doc_converter .split_pdf (
577588 output = self .output ,
578589 doc_path_set = doc_path_set ,
579- format = ' pdf' ,
590+ format = " pdf" ,
580591 delete = self .delete ,
581- page_ranges = page_ranges
592+ page_ranges = page_ranges ,
582593 )
583594
584595 def concat (self , file_paths : dict , format : str ) -> None :
@@ -604,7 +615,7 @@ def concat(self, file_paths: dict, format: str) -> None:
604615 logger = self .prog_logger ,
605616 )
606617 concat_audio .close ()
607-
618+
608619 # Concatenate movie files
609620 if file_paths [Category .MOVIE ] and (
610621 format is None or format in self ._supported_formats [Category .MOVIE ]
@@ -629,7 +640,7 @@ def concat(self, file_paths: dict, format: str) -> None:
629640 logger = self .prog_logger ,
630641 )
631642 concat_vid .close ()
632-
643+
633644 # Concatenate image files (make a gif out of them)
634645 if file_paths [Category .IMAGE ] and (
635646 format is None or format in self ._supported_formats [Category .IMAGE ]
@@ -651,7 +662,7 @@ def concat(self, file_paths: dict, format: str) -> None:
651662 gif_out_path , fps = self .framerate , logger = self .prog_logger
652663 )
653664 concatenated_image .close () # Added for consistency
654-
665+
655666 # Concatenate document files (keep respective document format)
656667 if file_paths [Category .DOCUMENT ] and (
657668 format is None or format in self ._supported_formats [Category .DOCUMENT ]
@@ -665,7 +676,7 @@ def concat(self, file_paths: dict, format: str) -> None:
665676 key = lambda x : x [1 ] if x else "" , # Handle None values
666677 )
667678 pdfs = [pdf for pdf in pdfs if pdf is not None ] # Filter out None values
668-
679+
669680 srt_out_path = os .path .join (self .output , "concatenated_subtitles.srt" )
670681 srts = sorted (
671682 [
@@ -675,25 +686,36 @@ def concat(self, file_paths: dict, format: str) -> None:
675686 key = lambda x : x [1 ] if x else "" , # Handle None values
676687 )
677688 srts = [srt for srt in srts if srt is not None ] # Filter out None values
678-
689+
679690 if len (pdfs ) > 0 :
680691 # Produce a single pdf file
681692 # Set up manual progress tracking for PDF concatenation
682- if hasattr (self .prog_logger , ' job_id' ) and self .prog_logger .job_id :
693+ if hasattr (self .prog_logger , " job_id" ) and self .prog_logger .job_id :
683694 total_pdfs = len (pdfs )
684695 for i , doc_path_set in enumerate (pdfs ):
685696 # Update progress manually since PDF operations don't have built-in progress
686- if hasattr (self .prog_logger , 'shared_progress_dict' ) and self .prog_logger .shared_progress_dict :
697+ if (
698+ hasattr (self .prog_logger , "shared_progress_dict" )
699+ and self .prog_logger .shared_progress_dict
700+ ):
687701 import threading
702+
688703 with threading .Lock ():
689- if self .prog_logger .job_id in self .prog_logger .shared_progress_dict :
690- self .prog_logger .shared_progress_dict [self .prog_logger .job_id ].update ({
691- 'progress' : i ,
692- 'total' : total_pdfs ,
693- 'status' : f'processing PDF { i + 1 } /{ total_pdfs } ' ,
694- 'last_updated' : time .time ()
695- })
696-
704+ if (
705+ self .prog_logger .job_id
706+ in self .prog_logger .shared_progress_dict
707+ ):
708+ self .prog_logger .shared_progress_dict [
709+ self .prog_logger .job_id
710+ ].update (
711+ {
712+ "progress" : i ,
713+ "total" : total_pdfs ,
714+ "status" : f"processing PDF { i + 1 } /{ total_pdfs } " ,
715+ "last_updated" : time .time (),
716+ }
717+ )
718+
697719 doc = fitz .open ()
698720 for doc_path_set in pdfs :
699721 pdf_path = self .file_handler .join_back (doc_path_set )
@@ -702,24 +724,35 @@ def concat(self, file_paths: dict, format: str) -> None:
702724 pdf_document .close ()
703725 doc .save (pdf_out_path )
704726 doc .close ()
705-
727+
706728 if len (srts ) > 0 :
707729 # Set up manual progress tracking for SRT concatenation
708- if hasattr (self .prog_logger , ' job_id' ) and self .prog_logger .job_id :
730+ if hasattr (self .prog_logger , " job_id" ) and self .prog_logger .job_id :
709731 total_srts = len (srts )
710732 for i , doc_path_set in enumerate (srts ):
711733 # Update progress manually
712- if hasattr (self .prog_logger , 'shared_progress_dict' ) and self .prog_logger .shared_progress_dict :
734+ if (
735+ hasattr (self .prog_logger , "shared_progress_dict" )
736+ and self .prog_logger .shared_progress_dict
737+ ):
713738 import threading
739+
714740 with threading .Lock ():
715- if self .prog_logger .job_id in self .prog_logger .shared_progress_dict :
716- self .prog_logger .shared_progress_dict [self .prog_logger .job_id ].update ({
717- 'progress' : i ,
718- 'total' : total_srts ,
719- 'status' : f'processing SRT { i + 1 } /{ total_srts } ' ,
720- 'last_updated' : time .time ()
721- })
722-
741+ if (
742+ self .prog_logger .job_id
743+ in self .prog_logger .shared_progress_dict
744+ ):
745+ self .prog_logger .shared_progress_dict [
746+ self .prog_logger .job_id
747+ ].update (
748+ {
749+ "progress" : i ,
750+ "total" : total_srts ,
751+ "status" : f"processing SRT { i + 1 } /{ total_srts } " ,
752+ "last_updated" : time .time (),
753+ }
754+ )
755+
723756 for doc_path_set in srts :
724757 # Produce a single srt file
725758 srt_path = self .file_handler .join_back (doc_path_set )
@@ -729,32 +762,43 @@ def concat(self, file_paths: dict, format: str) -> None:
729762 with open (srt_out_path , "a" ) as srt_file :
730763 srt_file .write (srt_content )
731764 srt_file .write ("\n " )
732-
765+
733766 # Post-processing with progress tracking
734767 total_categories = sum (len (files ) for files in file_paths .values ())
735768 processed_files = 0
736-
769+
737770 for category in file_paths .keys ():
738771 # Iterate over each input category and post-process respective files
739772 for i , file_path in enumerate (file_paths [category ]):
740773 # Manual progress update for post-processing
741- if hasattr (self .prog_logger , 'job_id' ) and self .prog_logger .job_id :
742- if hasattr (self .prog_logger , 'shared_progress_dict' ) and self .prog_logger .shared_progress_dict :
774+ if hasattr (self .prog_logger , "job_id" ) and self .prog_logger .job_id :
775+ if (
776+ hasattr (self .prog_logger , "shared_progress_dict" )
777+ and self .prog_logger .shared_progress_dict
778+ ):
743779 import threading
780+
744781 with threading .Lock ():
745- if self .prog_logger .job_id in self .prog_logger .shared_progress_dict :
746- self .prog_logger .shared_progress_dict [self .prog_logger .job_id ].update ({
747- 'progress' : processed_files ,
748- 'total' : total_categories ,
749- 'status' : f'post-processing files ({ processed_files + 1 } /{ total_categories } )' ,
750- 'last_updated' : time .time ()
751- })
752-
782+ if (
783+ self .prog_logger .job_id
784+ in self .prog_logger .shared_progress_dict
785+ ):
786+ self .prog_logger .shared_progress_dict [
787+ self .prog_logger .job_id
788+ ].update (
789+ {
790+ "progress" : processed_files ,
791+ "total" : total_categories ,
792+ "status" : f"post-processing files ({ processed_files + 1 } /{ total_categories } )" ,
793+ "last_updated" : time .time (),
794+ }
795+ )
796+
753797 self .file_handler .post_process (
754798 file_path , self .output , self .delete , show_status = (i == 0 )
755799 )
756800 processed_files += 1
757-
801+
758802 self .event_logger .info (
759803 f"[+] { lang .get_translation ('concat_success' , self .locale )} "
760804 )
@@ -765,24 +809,35 @@ def merge(self, file_paths: dict, across: bool = False) -> None:
765809 # If only a video file is provided, look for a matching audio file in the same directory
766810 found_audio = False
767811 audio_exts = list (self ._supported_formats [Category .AUDIO ].keys ())
768-
812+
769813 total_movies = len (file_paths [Category .MOVIE ])
770814 processed_movies = 0
771815
772816 for movie_path_set in file_paths [Category .MOVIE ]:
773817 # Manual progress update for merge operations
774- if hasattr (self .prog_logger , 'job_id' ) and self .prog_logger .job_id :
775- if hasattr (self .prog_logger , 'shared_progress_dict' ) and self .prog_logger .shared_progress_dict :
818+ if hasattr (self .prog_logger , "job_id" ) and self .prog_logger .job_id :
819+ if (
820+ hasattr (self .prog_logger , "shared_progress_dict" )
821+ and self .prog_logger .shared_progress_dict
822+ ):
776823 import threading
824+
777825 with threading .Lock ():
778- if self .prog_logger .job_id in self .prog_logger .shared_progress_dict :
779- self .prog_logger .shared_progress_dict [self .prog_logger .job_id ].update ({
780- 'progress' : processed_movies ,
781- 'total' : total_movies ,
782- 'status' : f'merging video { processed_movies + 1 } /{ total_movies } ' ,
783- 'last_updated' : time .time ()
784- })
785-
826+ if (
827+ self .prog_logger .job_id
828+ in self .prog_logger .shared_progress_dict
829+ ):
830+ self .prog_logger .shared_progress_dict [
831+ self .prog_logger .job_id
832+ ].update (
833+ {
834+ "progress" : processed_movies ,
835+ "total" : total_movies ,
836+ "status" : f"merging video { processed_movies + 1 } /{ total_movies } " ,
837+ "last_updated" : time .time (),
838+ }
839+ )
840+
786841 # Try to find a corresponding audio file in the input set
787842 # (e.g. "-1 path1 -2 path2 -n pathn")
788843 if across :
@@ -839,15 +894,17 @@ def merge(self, file_paths: dict, across: bool = False) -> None:
839894 )
840895 except Exception as e :
841896 # Handle errors gracefully and update progress logger
842- if hasattr (self .prog_logger , 'set_error' ):
843- self .prog_logger .set_error (f"Error merging { movie_path_set [1 ]} : { str (e )} " )
897+ if hasattr (self .prog_logger , "set_error" ):
898+ self .prog_logger .set_error (
899+ f"Error merging { movie_path_set [1 ]} : { str (e )} "
900+ )
844901 raise
845902 finally :
846903 if audio is not None :
847904 audio .close ()
848905 if video is not None :
849906 video .close ()
850-
907+
851908 self .file_handler .post_process (
852909 movie_path_set , merged_out_path , self .delete
853910 )
@@ -856,23 +913,31 @@ def merge(self, file_paths: dict, across: bool = False) -> None:
856913 self .file_handler .post_process (
857914 audio_fit , merged_out_path , self .delete , show_status = False
858915 )
859-
916+
860917 processed_movies += 1
861918
862919 if not found_audio :
863920 self .event_logger .warning (
864921 f"[!] { lang .get_translation ('no_audio_movie_match' , self .locale )} "
865922 )
866-
923+
867924 # Final progress update
868- if hasattr (self .prog_logger , 'job_id' ) and self .prog_logger .job_id :
869- if hasattr (self .prog_logger , 'shared_progress_dict' ) and self .prog_logger .shared_progress_dict :
925+ if hasattr (self .prog_logger , "job_id" ) and self .prog_logger .job_id :
926+ if (
927+ hasattr (self .prog_logger , "shared_progress_dict" )
928+ and self .prog_logger .shared_progress_dict
929+ ):
870930 import threading
931+
871932 with threading .Lock ():
872933 if self .prog_logger .job_id in self .prog_logger .shared_progress_dict :
873- self .prog_logger .shared_progress_dict [self .prog_logger .job_id ].update ({
874- 'progress' : total_movies ,
875- 'total' : total_movies ,
876- 'status' : 'merge completed' ,
877- 'last_updated' : time .time ()
878- })
934+ self .prog_logger .shared_progress_dict [
935+ self .prog_logger .job_id
936+ ].update (
937+ {
938+ "progress" : total_movies ,
939+ "total" : total_movies ,
940+ "status" : "merge completed" ,
941+ "last_updated" : time .time (),
942+ }
943+ )
0 commit comments