@@ -314,15 +314,19 @@ def _format_ffmpeg_concat_path(file_path: str) -> str:
314314
315315
316316def concat_video_clips_with_ffmpeg (
317- clip_files : List [str ], output_file : str , threads : int , output_dir : str
317+ clip_files : List [str ],
318+ output_file : str ,
319+ threads : int ,
320+ output_dir : str ,
321+ max_duration : float | None = None ,
318322):
319323 concat_list_file = os .path .join (output_dir , "ffmpeg-concat-list.txt" )
320324 with open (concat_list_file , "w" , encoding = "utf-8" ) as fp :
321325 for clip_file in clip_files :
322326 fp .write (f"file '{ _format_ffmpeg_concat_path (clip_file )} '\n " )
323327
324328 def build_command (codec : str ) -> list [str ]:
325- return [
329+ command = [
326330 utils .get_ffmpeg_binary (),
327331 "-y" ,
328332 "-f" ,
@@ -337,8 +341,11 @@ def build_command(codec: str) -> list[str]:
337341 str (threads or 2 ),
338342 "-pix_fmt" ,
339343 "yuv420p" ,
340- output_file ,
341344 ]
345+ if max_duration is not None and max_duration > 0 :
346+ command .extend (["-t" , f"{ max_duration :.3f} " ])
347+ command .append (output_file )
348+ return command
342349
343350 def run_concat (codec : str ):
344351 command = build_command (codec )
@@ -719,21 +726,14 @@ def combine_videos(
719726 logger .warning ("no clips available for merging" )
720727 return combined_video_path
721728
722- # if there is only one clip, use it directly
723- if len (processed_clips ) == 1 :
724- logger .info ("using single clip directly" )
725- shutil .copy (processed_clips [0 ].file_path , combined_video_path )
726- delete_files ([processed_clips [0 ].file_path ])
727- logger .info ("video combining completed" )
728- return combined_video_path
729-
730729 clip_files = [clip .file_path for clip in processed_clips ]
731730 logger .info (f"concatenating { len (clip_files )} clips with ffmpeg" )
732731 concat_video_clips_with_ffmpeg (
733732 clip_files = clip_files ,
734733 output_file = combined_video_path ,
735734 threads = threads ,
736735 output_dir = output_dir ,
736+ max_duration = audio_duration ,
737737 )
738738
739739 # clean temp files
0 commit comments