Quantumbraid commited on
Commit
bdd6bfe
·
verified ·
1 Parent(s): ed84da8

Persist chained render segments in bucket

Browse files

Store each chained MP4 and handoff frame under /data before joining, and retain artifacts if ffmpeg fails.

Files changed (1) hide show
  1. app.py +41 -13
app.py CHANGED
@@ -581,12 +581,22 @@ def last_frame_of_video(video_path):
581
  capture.release()
582
 
583
 
584
- def stitch_videos(segment_paths):
585
  """Join equal-format render segments into one MP4 using ffmpeg's concat demuxer."""
586
  if len(segment_paths) == 1:
587
  return segment_paths[0]
588
 
589
- with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, encoding="utf-8") as concat_file:
 
 
 
 
 
 
 
 
 
 
590
  concat_path = concat_file.name
591
  for path in segment_paths:
592
  # ffconcat uses single-quoted paths; temporary filenames are local,
@@ -594,8 +604,11 @@ def stitch_videos(segment_paths):
594
  concat_file.write("file '{}'".format(os.path.abspath(path).replace("'", r"'\\''")))
595
  concat_file.write(chr(10))
596
 
597
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as output_file:
598
- output_path = output_file.name
 
 
 
599
 
600
  copy_command = [
601
  "ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", concat_path,
@@ -637,8 +650,9 @@ def generate_video(
637
  sequential_renders=1,
638
  lora_groups=None,
639
  video_component=True,
640
- progress=None,
641
- ):
 
642
  """
643
  Generate a video from an input image using the Wan 2.2 14B I2V model with Lightning LoRA.
644
  This function takes an input image and generates a video animation based on the provided
@@ -699,6 +713,10 @@ def generate_video(
699
  if last_image:
700
  processed_last_image = resize_and_crop_to_match(last_image, resized_image)
701
 
 
 
 
 
702
  segment_paths = []
703
  current_input = resized_image
704
  current_last_image = processed_last_image
@@ -726,23 +744,33 @@ def generate_video(
726
  lora_groups,
727
  progress,
728
  )
 
 
 
 
 
 
729
  segment_paths.append(video_path)
730
  print(f"GPU complete: {task_n} ({segment_index + 1}/{sequential_renders})")
731
  if segment_index + 1 < sequential_renders:
732
  current_input = resize_image(last_frame_of_video(video_path))
 
 
733
  # The optional user-selected end image applies to the first clip
734
  # only; chained clips are steered by their predecessor frame.
735
  current_last_image = None
736
 
737
- video_path = stitch_videos(segment_paths)
738
  if progress:
739
  progress(1, desc="Joining chained segments")
740
  finally:
741
- # The stitched output is a separate file. Intermediate clips need not
742
- # linger in the Space's temporary storage after a successful join.
743
- for segment_path in segment_paths:
744
- if segment_path != locals().get("video_path") and os.path.exists(segment_path):
745
- os.unlink(segment_path)
 
 
746
 
747
  return (video_path if video_component else None), video_path, current_seed
748
 
@@ -1284,5 +1312,5 @@ if __name__ == "__main__":
1284
  head=f"<style>{GLASS_CSS}</style>{GLASS_JS}",
1285
  theme=theme,
1286
  show_error=True,
1287
- allowed_paths=["outputs"]
1288
  )
 
581
  capture.release()
582
 
583
 
584
+ def stitch_videos(segment_paths, artifact_dir=None):
585
  """Join equal-format render segments into one MP4 using ffmpeg's concat demuxer."""
586
  if len(segment_paths) == 1:
587
  return segment_paths[0]
588
 
589
+ if artifact_dir:
590
+ os.makedirs(artifact_dir, exist_ok=True)
591
+
592
+ with tempfile.NamedTemporaryFile(
593
+ mode="w",
594
+ suffix=".ffconcat",
595
+ prefix="segments_",
596
+ dir=artifact_dir,
597
+ delete=False,
598
+ encoding="utf-8",
599
+ ) as concat_file:
600
  concat_path = concat_file.name
601
  for path in segment_paths:
602
  # ffconcat uses single-quoted paths; temporary filenames are local,
 
604
  concat_file.write("file '{}'".format(os.path.abspath(path).replace("'", r"'\\''")))
605
  concat_file.write(chr(10))
606
 
607
+ if artifact_dir:
608
+ output_path = os.path.join(artifact_dir, "joined.mp4")
609
+ else:
610
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as output_file:
611
+ output_path = output_file.name
612
 
613
  copy_command = [
614
  "ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", concat_path,
 
650
  sequential_renders=1,
651
  lora_groups=None,
652
  video_component=True,
653
+ progress=None,
654
+ artifact_dir=None,
655
+ ):
656
  """
657
  Generate a video from an input image using the Wan 2.2 14B I2V model with Lightning LoRA.
658
  This function takes an input image and generates a video animation based on the provided
 
713
  if last_image:
714
  processed_last_image = resize_and_crop_to_match(last_image, resized_image)
715
 
716
+ if artifact_dir:
717
+ artifact_dir = os.path.abspath(artifact_dir)
718
+ os.makedirs(artifact_dir, exist_ok=True)
719
+
720
  segment_paths = []
721
  current_input = resized_image
722
  current_last_image = processed_last_image
 
744
  lora_groups,
745
  progress,
746
  )
747
+ if artifact_dir:
748
+ durable_segment = os.path.join(artifact_dir, f"segment_{segment_index + 1:02d}.mp4")
749
+ shutil.copy2(video_path, durable_segment)
750
+ if os.path.abspath(video_path) != durable_segment and os.path.exists(video_path):
751
+ os.unlink(video_path)
752
+ video_path = durable_segment
753
  segment_paths.append(video_path)
754
  print(f"GPU complete: {task_n} ({segment_index + 1}/{sequential_renders})")
755
  if segment_index + 1 < sequential_renders:
756
  current_input = resize_image(last_frame_of_video(video_path))
757
+ if artifact_dir:
758
+ current_input.save(os.path.join(artifact_dir, f"handoff_frame_{segment_index + 1:02d}.png"))
759
  # The optional user-selected end image applies to the first clip
760
  # only; chained clips are steered by their predecessor frame.
761
  current_last_image = None
762
 
763
+ video_path = stitch_videos(segment_paths, artifact_dir=artifact_dir)
764
  if progress:
765
  progress(1, desc="Joining chained segments")
766
  finally:
767
+ # Queued jobs keep every durable segment and handoff frame in /data so
768
+ # a failed join can be retried or retrieved. Direct UI runs still use
769
+ # temporary storage and retain the old cleanup behavior.
770
+ if not artifact_dir:
771
+ for segment_path in segment_paths:
772
+ if segment_path != locals().get("video_path") and os.path.exists(segment_path):
773
+ os.unlink(segment_path)
774
 
775
  return (video_path if video_component else None), video_path, current_seed
776
 
 
1312
  head=f"<style>{GLASS_CSS}</style>{GLASS_JS}",
1313
  theme=theme,
1314
  show_error=True,
1315
+ allowed_paths=["outputs", "/data/outputs"]
1316
  )