File size: 6,975 Bytes
abfb2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fd599c
 
abfb2cd
0fd599c
 
6c2be15
0fd599c
57f1fab
 
 
 
 
 
 
 
abfb2cd
57f1fab
 
 
abfb2cd
 
 
 
 
 
 
 
 
 
 
 
 
0fd599c
abfb2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fd599c
abfb2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7eaeff5
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
lipsync.py β€” Lip sync any video to any audio via LatentSync (fffiloni/LatentSync)

Strategy:
  1. Split input audio into ≀8s chunks (LatentSync hard limit)
  2. Slice matching video segment per chunk (loops video if audio is longer)
  3. Call fffiloni/LatentSync remotely for each chunk
  4. Normalize each chunk to consistent codec/fps
  5. Concatenate into one seamless final video
"""

import os
import subprocess
import tempfile
from pathlib import Path


# ─────────────────────────────────────────────────────────────────────────────
# Utilities
# ─────────────────────────────────────────────────────────────────────────────

def get_duration(path: str) -> float:
    result = subprocess.run([
        "ffprobe", "-v", "error",
        "-show_entries", "format=duration",
        "-of", "default=noprint_wrappers=1:nokey=1",
        path
    ], capture_output=True, text=True, check=True)
    return float(result.stdout.strip())


def slice_video(video_path: str, start: float, duration: float, output_path: str):
    subprocess.run([
        "ffmpeg", "-y",
        "-ss", str(start), "-i", video_path,
        "-t", str(duration),
        "-c:v", "libx264", "-c:a", "aac",
        "-r", "25",
        "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
        output_path
    ], check=True, capture_output=True)


def slice_audio(audio_path: str, start: float, duration: float, output_path: str):
    subprocess.run([
        "ffmpeg", "-y",
        "-ss", str(start), "-i", audio_path,
        "-t", str(duration),
        "-ar", "16000", "-ac", "1",
        output_path
    ], check=True, capture_output=True)


def normalize_chunk(input_path: str, output_path: str):
    """Re-encode to consistent codec/fps/resolution for clean concat"""
    subprocess.run([
        "ffmpeg", "-y", "-i", input_path,
        "-c:v", "libx264", "-c:a", "aac",
        "-r", "25",
        "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
        "-preset", "fast",
        output_path
    ], check=True, capture_output=True)


def concat_videos(video_paths: list[str], output_path: str, tmp: str):
    list_file = os.path.join(tmp, "concat_list.txt")
    with open(list_file, "w") as f:
        for p in video_paths:
            f.write(f"file '{os.path.abspath(p)}'\n")
    subprocess.run([
        "ffmpeg", "-y",
        "-f", "concat", "-safe", "0",
        "-i", list_file,
        "-c:v", "libx264", "-c:a", "aac",
        "-movflags", "+faststart",
        output_path
    ], check=True, capture_output=True)


# ─────────────────────────────────────────────────────────────────────────────
# Remote LatentSync call
# ─────────────────────────────────────────────────────────────────────────────

def run_latentsync_remote(video_chunk: str, audio_chunk: str, hf_token: str = None) -> str:
    import os
    from gradio_client import Client, handle_file

    # token passed in from user input
    client = Client("fffiloni/LatentSync", token=hf_token)

    # Print all available endpoints so we can find the right one
    info = client.view_api(return_format="dict")
    print("ENDPOINTS:", list(info["named_endpoints"].keys()))

    # Use the first available endpoint
    endpoint = list(info["named_endpoints"].keys())[0]
    print(f"Using endpoint: {endpoint}")

    result = client.predict(
        handle_file(video_chunk),
        handle_file(audio_chunk),
        api_name=endpoint,
    )
    print(f"        β†’ LatentSync returned: {result}")
    return result

# ─────────────────────────────────────────────────────────────────────────────
# Main pipeline
# ─────────────────────────────────────────────────────────────────────────────

def run_lipsync_pipeline(
    original_video: str,
    dubbed_audio: str,
    output_path: str,
    chunk_duration: float = 8.0,
    hf_token: str = None,
) -> str:
    print(f"\nStarting lip sync pipeline...")

    video_dur = get_duration(original_video)
    audio_dur = get_duration(dubbed_audio)

    print(f"  β†’ Video: {video_dur:.1f}s | Audio: {audio_dur:.1f}s")

    with tempfile.TemporaryDirectory() as tmp:
        # Build chunks from audio length
        chunks = []
        t = 0.0
        while t < audio_dur:
            dur = min(chunk_duration, audio_dur - t)
            if dur < 0.5:
                break
            chunks.append((t, dur))
            t += dur

        print(f"  β†’ {len(chunks)} chunks to process")

        normalized_chunks = []
        for i, (start, dur) in enumerate(chunks):
            print(f"  [{i+1}/{len(chunks)}] t={start:.1f}s dur={dur:.1f}s")

            # Slice audio chunk
            audio_chunk = os.path.join(tmp, f"audio_{i}.wav")
            slice_audio(dubbed_audio, start, dur, audio_chunk)

            # Slice video β€” loop if dubbed audio longer than original video
            video_start = start % video_dur
            safe_dur = min(dur + 0.5, video_dur - video_start)
            video_chunk = os.path.join(tmp, f"video_{i}.mp4")
            slice_video(original_video, video_start, safe_dur, video_chunk)

            try:
                synced_raw = run_latentsync_remote(video_chunk, audio_chunk, hf_token=hf_token)
                synced_norm = os.path.join(tmp, f"synced_norm_{i}.mp4")
                normalize_chunk(synced_raw, synced_norm)
                normalized_chunks.append(synced_norm)
                print(f"    βœ… Chunk {i+1} done")
            except Exception as e:
                print(f"    ⚠ Chunk {i+1} LatentSync failed: {e} β€” using raw video")
                fallback_norm = os.path.join(tmp, f"fallback_norm_{i}.mp4")
                normalize_chunk(video_chunk, fallback_norm)
                normalized_chunks.append(fallback_norm)

        if not normalized_chunks:
            raise RuntimeError("All chunks failed.")

        print(f"  β†’ Concatenating {len(normalized_chunks)} chunks...")
        concat_videos(normalized_chunks, output_path, tmp)

    size_mb = os.path.getsize(output_path) / (1024 * 1024)
    print(f"  β†’ Done: {output_path} ({size_mb:.1f} MB)")
    return output_path