File size: 3,876 Bytes
24d2e2b
 
 
 
725a76b
 
 
 
 
 
 
24d2e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
725a76b
 
24d2e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
725a76b
 
24d2e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import subprocess
import shutil
import os
import sys
from pathlib import Path

BASE = Path(__file__).resolve().parent
CKPT_VACE = BASE / "Wan2.1-VACE-1.3B"
CKPT_FLF = BASE / "Wan2.1-FLF2V-14B-720P"

def run_cmd(cmd):
    """Run a command and handle errors"""
    try:
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        return result.stdout
    except subprocess.CalledProcessError as e:
        print(f"Command failed: {' '.join(cmd)}")
        print(f"Error: {e.stderr}")
        raise

def check_wan_installation():
    """Check if Wan2.1 and VACE are properly installed"""
    try:
        # Check if the repositories exist locally
        wan_path = BASE / "Wan2.1"
        vace_path = BASE / "VACE"
        
        if not wan_path.exists():
            print("Cloning Wan2.1 repository...")
            run_cmd(["git", "clone", "https://github.com/Wan-Video/Wan2.1.git", str(wan_path)])
        
        if not vace_path.exists():
            print("Cloning VACE repository...")
            run_cmd(["git", "clone", "https://github.com/ali-vilab/VACE.git", str(vace_path)])
        
        # Add to Python path
        sys.path.insert(0, str(wan_path))
        sys.path.insert(0, str(vace_path))
        
        return True
    except Exception as e:
        print(f"Failed to setup Wan2.1/VACE: {e}")
        return False

def generate_video(ref_img, first_frame, last_frame, output_path):
    """Generate video from reference image and first/last frames"""
    if not check_wan_installation():
        raise RuntimeError("Wan2.1/VACE setup failed")
    
    # Create necessary directories
    os.makedirs("processed", exist_ok=True)
    os.makedirs("results", exist_ok=True)
    
    # Preprocess frames
    vace_script = BASE / "VACE" / "vace_preprocess.py"
    if vace_script.exists():
        run_cmd([
            sys.executable, str(vace_script),
            "--task", "extension",
            "--mode", "firstlastframe",
            "--image", f"{first_frame},{last_frame}",
            "--expand_num", "240"
        ])
    else:
        # Fallback: create a simple template video
        print("VACE preprocessing not available, using fallback method")
        # This would need actual implementation
    
    # Generate video
    generate_script = BASE / "Wan2.1" / "generate.py"
    if generate_script.exists():
        run_cmd([
            sys.executable, str(generate_script),
            "--task", "flf2v-14B",
            "--size", "1280*720",
            "--ckpt_dir", str(CKPT_FLF),
            "--src_ref_images", ref_img,
            "--src_video", "processed/src_video.mp4",
            "--fps", "24",
            "--offload_model", "True",
            "--t5_cpu"
        ])
        
        # Move result
        if os.path.exists("results/final.mp4"):
            shutil.move("results/final.mp4", output_path)
    else:
        raise FileNotFoundError("Wan2.1 generate.py not found")

def generate_image(ref_img, prompt, output_path):
    """Generate image from reference image and prompt"""
    if not check_wan_installation():
        raise RuntimeError("Wan2.1/VACE setup failed")
    
    # Create necessary directories
    os.makedirs("results", exist_ok=True)
    
    # Generate image
    generate_script = BASE / "Wan2.1" / "generate.py"
    if generate_script.exists():
        run_cmd([
            sys.executable, str(generate_script),
            "--task", "vace-1.3B",
            "--size", "832*480",
            "--ckpt_dir", str(CKPT_VACE),
            "--src_ref_images", ref_img,
            "--frame_num", "1",
            "--prompt", prompt or " "
        ])
        
        # Move result
        if os.path.exists("results/final_frame000.png"):
            shutil.move("results/final_frame000.png", output_path)
    else:
        raise FileNotFoundError("Wan2.1 generate.py not found")