23
23
clips_array ,
24
24
)
25
25
26
+
26
27
class Controller :
27
28
"""
28
29
Taking an input directory of files, convert them to a multitude of formats.
@@ -32,7 +33,9 @@ class Controller:
32
33
33
34
def __init__ (self , job_id = None , shared_progress_dict = None ):
34
35
# 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
+ )
36
39
# Get locale
37
40
self .locale = lang .get_system_language ()
38
41
# Setting up event logger and format
@@ -564,21 +567,29 @@ def handle_file_event(event_type: str, file_path: str) -> None:
564
567
565
568
def split (self , file_paths : dict , page_ranges ) -> None :
566
569
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
+ ):
568
574
import threading
575
+
569
576
with threading .Lock ():
570
577
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
+ )
575
586
if doc_path_set [2 ] == "pdf" :
576
587
self .doc_converter .split_pdf (
577
588
output = self .output ,
578
589
doc_path_set = doc_path_set ,
579
- format = ' pdf' ,
590
+ format = " pdf" ,
580
591
delete = self .delete ,
581
- page_ranges = page_ranges
592
+ page_ranges = page_ranges ,
582
593
)
583
594
584
595
def concat (self , file_paths : dict , format : str ) -> None :
@@ -604,7 +615,7 @@ def concat(self, file_paths: dict, format: str) -> None:
604
615
logger = self .prog_logger ,
605
616
)
606
617
concat_audio .close ()
607
-
618
+
608
619
# Concatenate movie files
609
620
if file_paths [Category .MOVIE ] and (
610
621
format is None or format in self ._supported_formats [Category .MOVIE ]
@@ -629,7 +640,7 @@ def concat(self, file_paths: dict, format: str) -> None:
629
640
logger = self .prog_logger ,
630
641
)
631
642
concat_vid .close ()
632
-
643
+
633
644
# Concatenate image files (make a gif out of them)
634
645
if file_paths [Category .IMAGE ] and (
635
646
format is None or format in self ._supported_formats [Category .IMAGE ]
@@ -651,7 +662,7 @@ def concat(self, file_paths: dict, format: str) -> None:
651
662
gif_out_path , fps = self .framerate , logger = self .prog_logger
652
663
)
653
664
concatenated_image .close () # Added for consistency
654
-
665
+
655
666
# Concatenate document files (keep respective document format)
656
667
if file_paths [Category .DOCUMENT ] and (
657
668
format is None or format in self ._supported_formats [Category .DOCUMENT ]
@@ -665,7 +676,7 @@ def concat(self, file_paths: dict, format: str) -> None:
665
676
key = lambda x : x [1 ] if x else "" , # Handle None values
666
677
)
667
678
pdfs = [pdf for pdf in pdfs if pdf is not None ] # Filter out None values
668
-
679
+
669
680
srt_out_path = os .path .join (self .output , "concatenated_subtitles.srt" )
670
681
srts = sorted (
671
682
[
@@ -675,25 +686,36 @@ def concat(self, file_paths: dict, format: str) -> None:
675
686
key = lambda x : x [1 ] if x else "" , # Handle None values
676
687
)
677
688
srts = [srt for srt in srts if srt is not None ] # Filter out None values
678
-
689
+
679
690
if len (pdfs ) > 0 :
680
691
# Produce a single pdf file
681
692
# 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 :
683
694
total_pdfs = len (pdfs )
684
695
for i , doc_path_set in enumerate (pdfs ):
685
696
# 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
+ ):
687
701
import threading
702
+
688
703
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
+
697
719
doc = fitz .open ()
698
720
for doc_path_set in pdfs :
699
721
pdf_path = self .file_handler .join_back (doc_path_set )
@@ -702,24 +724,35 @@ def concat(self, file_paths: dict, format: str) -> None:
702
724
pdf_document .close ()
703
725
doc .save (pdf_out_path )
704
726
doc .close ()
705
-
727
+
706
728
if len (srts ) > 0 :
707
729
# 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 :
709
731
total_srts = len (srts )
710
732
for i , doc_path_set in enumerate (srts ):
711
733
# 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
+ ):
713
738
import threading
739
+
714
740
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
+
723
756
for doc_path_set in srts :
724
757
# Produce a single srt file
725
758
srt_path = self .file_handler .join_back (doc_path_set )
@@ -729,32 +762,43 @@ def concat(self, file_paths: dict, format: str) -> None:
729
762
with open (srt_out_path , "a" ) as srt_file :
730
763
srt_file .write (srt_content )
731
764
srt_file .write ("\n " )
732
-
765
+
733
766
# Post-processing with progress tracking
734
767
total_categories = sum (len (files ) for files in file_paths .values ())
735
768
processed_files = 0
736
-
769
+
737
770
for category in file_paths .keys ():
738
771
# Iterate over each input category and post-process respective files
739
772
for i , file_path in enumerate (file_paths [category ]):
740
773
# 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
+ ):
743
779
import threading
780
+
744
781
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
+
753
797
self .file_handler .post_process (
754
798
file_path , self .output , self .delete , show_status = (i == 0 )
755
799
)
756
800
processed_files += 1
757
-
801
+
758
802
self .event_logger .info (
759
803
f"[+] { lang .get_translation ('concat_success' , self .locale )} "
760
804
)
@@ -765,24 +809,35 @@ def merge(self, file_paths: dict, across: bool = False) -> None:
765
809
# If only a video file is provided, look for a matching audio file in the same directory
766
810
found_audio = False
767
811
audio_exts = list (self ._supported_formats [Category .AUDIO ].keys ())
768
-
812
+
769
813
total_movies = len (file_paths [Category .MOVIE ])
770
814
processed_movies = 0
771
815
772
816
for movie_path_set in file_paths [Category .MOVIE ]:
773
817
# 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
+ ):
776
823
import threading
824
+
777
825
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
+
786
841
# Try to find a corresponding audio file in the input set
787
842
# (e.g. "-1 path1 -2 path2 -n pathn")
788
843
if across :
@@ -839,15 +894,17 @@ def merge(self, file_paths: dict, across: bool = False) -> None:
839
894
)
840
895
except Exception as e :
841
896
# 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
+ )
844
901
raise
845
902
finally :
846
903
if audio is not None :
847
904
audio .close ()
848
905
if video is not None :
849
906
video .close ()
850
-
907
+
851
908
self .file_handler .post_process (
852
909
movie_path_set , merged_out_path , self .delete
853
910
)
@@ -856,23 +913,31 @@ def merge(self, file_paths: dict, across: bool = False) -> None:
856
913
self .file_handler .post_process (
857
914
audio_fit , merged_out_path , self .delete , show_status = False
858
915
)
859
-
916
+
860
917
processed_movies += 1
861
918
862
919
if not found_audio :
863
920
self .event_logger .warning (
864
921
f"[!] { lang .get_translation ('no_audio_movie_match' , self .locale )} "
865
922
)
866
-
923
+
867
924
# 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
+ ):
870
930
import threading
931
+
871
932
with threading .Lock ():
872
933
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