2525)
2626
2727
28-
2928class Controller :
3029 """
3130 Taking an input directory of files, convert them to a multitude of formats.
@@ -242,31 +241,53 @@ def run(
242241 language : str ,
243242 workers : int ,
244243 ) -> None :
245- # Main function, convert media files to defined formats
246- # or merge or concatenate, according to the arguments
247- input_paths = []
248- # Apply worker override via environment for converters
249- try :
250- if workers is not None :
251- # Workers flag was set, do some validation, hand it to env
244+ # Convert media files to defined formats or
245+ # merge or concatenate, according to the arguments
246+ #
247+ # Set environment for worker count
248+ if workers is not None :
249+ try :
252250 os .environ ["A2A_MAX_WORKERS" ] = str (max (1 , int (workers )))
253- except Exception :
254- # Ignore invalid values; converters will fall back to CPU-based default
255- pass
251+ except Exception :
252+ pass
256253
257- # Apply input paths
254+ # Prepare input paths
258255 input_path_args = (
259256 input_path_args
260257 if input_path_args is not None
261258 else [os .path .dirname (os .getcwd ())]
262259 )
260+ input_paths = []
261+ for _ , arg in enumerate (input_path_args ):
262+ if arg .startswith ("-" ) and arg [1 :].isdigit ():
263+ input_paths .append (arg [2 :])
264+ else :
265+ try :
266+ input_paths [- 1 ] = (input_paths [- 1 ] + f" { arg } " ).strip ()
267+ except IndexError :
268+ input_paths .append (arg )
263269
270+ # Set flags and parameters
264271 self .across = across
265272 self .recursive = recursive
273+ self .merging = merge
274+ self .concatenating = concat
275+ self .page_ranges = split
276+ self .target_format = format .lower () if format is not None else None
277+ self .framerate = framerate
278+ self .delete = delete
279+ self .quality = (
280+ (
281+ quality .lower ()
282+ if quality and quality .lower () in [self .high , self .medium , self .low ]
283+ else None
284+ )
285+ if quality is not None
286+ else None
287+ )
266288
267- # User-set Language
289+ # Language
268290 if language is not None :
269- # we expect a language code like "en_US" or "pl_PL"
270291 if re .match (r"^[a-z]{2}_[A-Z]{2}$" , language ) and language in list (
271292 lang .LANGUAGE_CODES .keys ()
272293 ):
@@ -276,54 +297,21 @@ def run(
276297 f"[!] { lang .get_translation ('lang_not_supported' , self .locale )} "
277298 )
278299
279- for _ , arg in enumerate (input_path_args ):
280- # Custom handling of multiple input paths
281- # (e.g. "-1 path1 -2 path2 -n pathn")
282- if arg .startswith ("-" ) and arg [1 :].isdigit ():
283- input_paths .append (arg [2 :])
284- else :
285- try :
286- input_paths [- 1 ] = (input_paths [- 1 ] + f" { arg } " ).strip ()
287- except IndexError :
288- input_paths .append (arg )
289-
300+ # Output path
290301 if len (input_paths ) == 1 :
291302 self .output = output if output is not None else input_paths [0 ]
292303 else :
293- # len(input_paths) > 1
294- if not across :
295- self .output = output if output is not None else None
296- else :
297- self .output = (
298- output if output is not None else os .path .dirname (os .getcwd ())
299- )
304+ self .output = (
305+ output
306+ if output is not None
307+ else (os .path .dirname (os .getcwd ()) if across else None )
308+ )
300309
301- # Check if the output dir exists - Create it otherwise
302310 if self .output is not None and not os .path .exists (self .output ):
303311 os .makedirs (self .output )
304312
305- # No format means no conversion (but maybe merge || concat)
306- self .target_format = format .lower () if format is not None else None
307- self .framerate = framerate # Possibly no framerate means same as input
308- self .delete = delete # Delete mp4 files after conversion
309- # Check if quality is set, if not, set it to None
310- self .quality = (
311- (quality .lower () if quality .lower () in [self .high , self .medium , self .low ] else None )
312- if quality is not None
313- else None
314- )
315-
316- # Merge movie files with equally named audio files
317- self .merging = merge
318- # Concatenate files of same type (img/movie/audio) back to back
319- self .concatenating = concat
320- # Split files into smaller parts
321- self .page_ranges = split
322-
323313 file_paths = {}
324314 was_none , found_files = False , False
325-
326- # Check if input paths are given
327315 for input_path in input_paths :
328316 input_path = os .path .abspath (input_path )
329317 try :
@@ -336,28 +324,19 @@ def run(
336324 FileNotFoundError ,
337325 f"[!] { lang .get_translation ('error' , self .locale )} : { lang .get_translation ('no_dir_exist' , self .locale ).replace ('[dir]' , f'{ input_path } ' )} " ,
338326 )
339- # Make self.input hold the actual file if a file, otherwise the directory
340- if os .path .isfile (str (input_path )):
341- self .input = input_path # Use the file path directly
342- else :
343- self .input = input_path
327+ self .input = Path (input_path )
328+ self .output = Path (self .output )
344329
345- # If no output path set or derived by the script by now, throw an error
346330 if not self .output :
347331 end_with_msg (
348332 self .event_logger ,
349333 ValueError ,
350334 f"[!] { lang .get_translation ('error' , self .locale )} : { lang .get_translation ('no_out_multi_in' , self .locale )} " ,
351335 )
352-
353- # If output is just a file for whatever reason, turn it into directory
354336 if os .path .isfile (self .output ):
355337 self .output = os .path .dirname (self .output )
356338
357- # Unify path formatting, still works like any other string
358- self .input , self .output = Path (self .input ), Path (self .output )
359-
360- # Cut setup for media conversion short, begin dropzone mode
339+ # Dropzone
361340 if dropzone :
362341 if self .input == self .output or self .input in self .output .parents :
363342 end_with_msg (
@@ -371,14 +350,11 @@ def run(
371350 self .watchdropzone (self .input )
372351 return
373352
374- # What if that directory contains subdirectories?
353+ # Recursion
375354 if self .recursive :
376- # Check if there is any subdirectory in the input path
377355 if any (entry .is_dir () for entry in os .scandir (self .input )):
378- file_paths = {} # Reset, we go through everything again to be sure
379- # If the user set the recursive option, also scan every non-empty subdirectory
356+ file_paths = {}
380357 for root , _ , files in os .walk (self .input ):
381- # root should be a directory with >=1 file to be considered
382358 try :
383359 file_paths = self .file_handler .get_file_paths (
384360 root , file_paths , self ._supported_formats
@@ -403,7 +379,6 @@ def run(
403379 )
404380 continue
405381
406- # If no output given, output is set to the input path
407382 if not across :
408383 if self .output is None or was_none :
409384 self .output = os .path .dirname (input_path )
@@ -412,14 +387,12 @@ def run(
412387 found_files = len (file_paths ) > 0 if not found_files else found_files
413388 file_paths = {}
414389
415- # If multiple input paths are given, yet no output, output is set to the first input path
416390 if across :
417391 if self .output is None :
418392 self .output = os .path .dirname (input_paths [0 ])
419393 self .process_file_paths (file_paths )
420394
421- # Check if file_paths is empty
422- if across and len (file_paths ) == 0 or not found_files :
395+ if (across and len (file_paths ) == 0 ) or not found_files :
423396 self .event_logger .warning (
424397 f"{ lang .get_translation ('no_media_general' , self .locale )} "
425398 )
0 commit comments