Lemonator commited on
Commit
4c12131
Β·
verified Β·
1 Parent(s): 7a5570e

Update app_lora.py

Browse files
Files changed (1) hide show
  1. app_lora.py +28 -22
app_lora.py CHANGED
@@ -11,6 +11,10 @@ import numpy as np
11
  from PIL import Image
12
  import random
13
 
 
 
 
 
14
  MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
15
 
16
  LORA_REPO_ID = "vrgamedevgirl84/Wan14BT2VFusioniX"
@@ -116,7 +120,11 @@ def generate_video(input_image, prompt, height, width,
116
  target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
117
  target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
118
 
119
- num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
 
 
 
 
120
 
121
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
122
 
@@ -133,30 +141,28 @@ def generate_video(input_image, prompt, height, width,
133
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
134
  video_path = tmpfile.name
135
 
136
- # Use imageio as the preferred backend with optimized settings
137
- export_to_video(
138
- output_frames_list,
139
- video_path,
140
- fps=FIXED_FPS,
141
- codec="libx264",
142
- output_params=[
143
- "-pix_fmt", "yuv420p", # Most compatible pixel format
144
- "-movflags", "+faststart", # Enable streaming
145
- "-profile:v", "main", # Broad compatibility profile
146
- "-tune", "film" # Optimize for high quality video
147
- ]
148
- )
149
 
150
- # Verify video can be read (basic sanity check)
151
  try:
152
- import imageio
153
- with imageio.get_reader(video_path) as reader:
154
- if len(reader) < 1: # Check if video has frames
155
- raise ValueError("Empty video generated")
 
 
 
 
 
 
 
 
 
 
 
156
  except Exception as e:
157
- print(f"Video validation failed, regenerating: {str(e)}")
158
- # Fallback to simple export if validation fails
159
- export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
160
 
161
  return video_path, current_seed
162
 
 
11
  from PIL import Image
12
  import random
13
 
14
+ import warnings
15
+ warnings.filterwarnings("ignore", message=".*Attempting to use legacy OpenCV backend.*")
16
+ warnings.filterwarnings("ignore", message=".*num_frames - 1.*")
17
+
18
  MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
19
 
20
  LORA_REPO_ID = "vrgamedevgirl84/Wan14BT2VFusioniX"
 
120
  target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
121
  target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
122
 
123
+ # Ensure num_frames-1 is divisible by 4
124
+ raw_frames = int(round(duration_seconds * FIXED_FPS))
125
+ num_frames = ((raw_frames - 1) // 4) * 4 + 1
126
+ num_frames = np.clip(num_frames, MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
127
+ print(f"Using {num_frames} frames (requested {raw_frames})")
128
 
129
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
130
 
 
141
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
142
  video_path = tmpfile.name
143
 
144
+ # Simple export that works with current diffusers version
145
+ export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
 
 
 
 
 
 
 
 
 
 
 
146
 
147
+ # Optional: Use FFmpeg directly for better encoding if available
148
  try:
149
+ import subprocess
150
+ optimized_path = video_path + "_opt.mp4"
151
+ subprocess.run([
152
+ 'ffmpeg',
153
+ '-y', # Overwrite without asking
154
+ '-i', video_path, # Input file
155
+ '-c:v', 'libx264', # Codec
156
+ '-pix_fmt', 'yuv420p', # Pixel format
157
+ '-profile:v', 'main', # Compatibility profile
158
+ '-movflags', '+faststart', # Streaming optimized
159
+ '-crf', '23', # Quality level
160
+ '-preset', 'fast', # Encoding speed
161
+ optimized_path
162
+ ], check=True)
163
+ video_path = optimized_path
164
  except Exception as e:
165
+ print(f"FFmpeg optimization failed, using default export: {str(e)}")
 
 
166
 
167
  return video_path, current_seed
168