File size: 13,038 Bytes
da25681
 
 
 
 
 
 
 
05c5bfd
da25681
 
 
 
 
 
 
 
82c406c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da25681
 
f9ec5ab
 
 
 
82c406c
 
 
da25681
 
 
 
 
 
e0936b5
f9ec5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82c406c
f9ec5ab
da25681
 
f9ec5ab
 
 
 
 
 
 
 
 
 
da25681
a02db13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05c5bfd
f9ec5ab
 
 
 
82c406c
 
 
05c5bfd
 
f9ec5ab
05c5bfd
 
 
f9ec5ab
 
05c5bfd
 
 
 
 
 
82c406c
05c5bfd
 
 
f9ec5ab
 
 
 
 
05c5bfd
f9ec5ab
05c5bfd
 
 
 
f9ec5ab
 
05c5bfd
 
 
f9ec5ab
 
 
 
 
 
 
 
 
 
 
05c5bfd
5ef29e5
f9ec5ab
 
 
 
5ef29e5
f9ec5ab
5ef29e5
eec2aff
f9ec5ab
eec2aff
4aa1838
ee57d7f
f9ec5ab
f959be9
05c5bfd
d52bea3
da25681
05c5bfd
 
 
 
 
 
 
 
 
 
 
d52bea3
 
25494c5
 
 
 
 
 
 
 
f9ec5ab
 
d52bea3
 
f9ec5ab
d52bea3
05c5bfd
d52bea3
 
 
 
f9ec5ab
d52bea3
 
f9ec5ab
d52bea3
 
f9ec5ab
 
d52bea3
 
 
 
355b39c
d52bea3
f9ec5ab
d52bea3
 
f9ec5ab
eec2aff
 
 
 
 
5ef29e5
 
 
 
 
d52bea3
5ef29e5
eec2aff
 
5ef29e5
eec2aff
d52bea3
 
 
f9ec5ab
d52bea3
da25681
 
439e8db
6f3ece3
 
439e8db
 
f9ec5ab
7eea0f7
 
05c5bfd
5ef29e5
7eea0f7
 
 
 
da25681
 
ee0352c
5ef29e5
da25681
 
 
 
 
 
f9ec5ab
da25681
96e2b62
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import os
import requests
import json
import time
import subprocess
import gradio as gr
import uuid
from dotenv import load_dotenv
from urllib.parse import urlparse

# Load environment variables
load_dotenv()

# API Keys
B_KEY = os.getenv("B_KEY")

# URLs
API_URL = "https://api.sync.so/v2/generate"

def get_media_resolution(url):
    print(f"\n[DEBUG] Getting resolution for: {url}")
    
    # Download the file to a temporary location
    response = requests.get(url)
    if response.status_code != 200:
        print(f"[ERROR] Failed to download media. Status code: {response.status_code}")
        return None
        
    temp_path = f"temp_media_{uuid.uuid4()}"
    with open(temp_path, 'wb') as f:
        f.write(response.content)
    
    # Get resolution using FFprobe
    cmd = [
        'ffprobe', 
        '-v', 'error',
        '-select_streams', 'v:0',
        '-show_entries', 'stream=width,height',
        '-of', 'json',
        temp_path
    ]
    
    try:
        result = subprocess.run(cmd, capture_output=True, text=True)
        os.remove(temp_path)  # Clean up temp file
        
        if result.returncode == 0:
            data = json.loads(result.stdout)
            if 'streams' in data and data['streams']:
                width = data['streams'][0].get('width')
                height = data['streams'][0].get('height')
                if width and height:
                    print(f"[DEBUG] Detected resolution: {width}x{height}")
                    return [width, height]
    except Exception as e:
        print(f"[ERROR] Failed to get resolution: {str(e)}")
        if os.path.exists(temp_path):
            os.remove(temp_path)
    
    print("[DEBUG] Failed to detect resolution, using default")
    return [1280, 720]  # Default resolution

def lipsync_api_call(video_url, audio_url):
    print(f"\n[DEBUG] Starting lipsync_api_call")
    print(f"[DEBUG] Video URL: {video_url}")
    print(f"[DEBUG] Audio URL: {audio_url}")
    
    # Get the resolution of the input video/image
    resolution = get_media_resolution(video_url)
    
    headers = {
        "Content-Type": "application/json",
        "x-api-key": B_KEY
    }
    
    data = {
        "model": "lipsync-1.8.0-beta",
        "input": [
            {
                "type": "video",
                "url": video_url
            },
            {
                "type": "audio",
                "url": audio_url
            }
        ],
        "options": {
            "pads": [0, 5, 0, 0],
            "speedup": 1,
            "output_format": "mp4",
            "sync_mode": "bounce",
            "fps": 24,
            "output_resolution": resolution
        }
    }
    
    print(f"[DEBUG] Request payload: {json.dumps(data, indent=2)}")
    
    try:
        response = requests.post(API_URL, headers=headers, data=json.dumps(data))
        print(f"[DEBUG] API Response status code: {response.status_code}")
        print(f"[DEBUG] API Response: {response.text}")
        return response.json()
    except Exception as e:
        print(f"[ERROR] API call failed: {str(e)}")
        raise

def check_job_status(job_id):
    print(f"\n[DEBUG] Checking job status for ID: {job_id}")
    headers = {"x-api-key": B_KEY}
    max_attempts = 3000
    attempt = 0
    
    while attempt < max_attempts:
        try:
            response = requests.get(f"{API_URL}/{job_id}", headers=headers)
            print(f"[DEBUG] Status check attempt {attempt + 1}")
            print(f"[DEBUG] Status response: {response.text}")
            
            data = response.json()
            status = data.get("status")
            print(f"[DEBUG] Current status: {status}")
            
            if status == "COMPLETED":
                print(f"[DEBUG] Job completed. Output URL: {data.get('outputUrl')}")
                return data.get("outputUrl")
            elif status == "FAILED" or status == "CANCELED":
                print(f"[ERROR] Job failed or was canceled. Error: {data.get('error')}")
                return None
            
            attempt += 1
            time.sleep(10)
            
        except Exception as e:
            print(f"[ERROR] Status check failed: {str(e)}")
            return None
    
    print("[ERROR] Max attempts reached")
    return None

def get_media_duration(file_path):
    print(f"\n[DEBUG] Getting duration for: {file_path}")
    cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
    result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    duration = float(result.stdout.strip())
    print(f"[DEBUG] Media duration: {duration} seconds")
    return duration

def combine_audio_video(video_path, audio_path, output_path):
    print(f"\n[DEBUG] Combining audio and video")
    print(f"[DEBUG] Video path: {video_path}")
    print(f"[DEBUG] Audio path: {audio_path}")
    print(f"[DEBUG] Output path: {output_path}")
    
    video_duration = get_media_duration(video_path)
    audio_duration = get_media_duration(audio_path)

    if video_duration > audio_duration:
        print("[DEBUG] Video longer than audio - trimming video")
        cmd = [
            'ffmpeg', '-i', video_path, '-i', audio_path,
            '-t', str(audio_duration),
            '-map', '0:v', '-map', '1:a',
            '-c:v', 'copy', '-c:a', 'aac',
            '-y', output_path
        ]
    else:
        print("[DEBUG] Audio longer than video - looping video")
        loop_count = int(audio_duration // video_duration) + 1
        cmd = [
            'ffmpeg', '-stream_loop', str(loop_count), '-i', video_path, '-i', audio_path,
            '-t', str(audio_duration),
            '-map', '0:v', '-map', '1:a',
            '-c:v', 'copy', '-c:a', 'aac',
            '-shortest', '-y', output_path
        ]

    print(f"[DEBUG] FFmpeg command: {' '.join(cmd)}")
    result = subprocess.run(cmd, capture_output=True, text=True)
    print(f"[DEBUG] FFmpeg stdout: {result.stdout}")
    print(f"[DEBUG] FFmpeg stderr: {result.stderr}")

def is_image_url(url):
    parsed = urlparse(url)
    path = parsed.path.lower()
    result = path.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp', '.heic', '.svg', '.ico'))
    print(f"\n[DEBUG] Checking if URL is image: {url}")
    print(f"[DEBUG] Result: {result}")
    return result

def create_video_from_image(image_url, output_path, duration=10):
    print(f"\n[DEBUG] Creating video from image")
    print(f"[DEBUG] Image URL: {image_url}")
    print(f"[DEBUG] Output path: {output_path}")
    
    # Get the resolution before creating the video
    resolution = get_media_resolution(image_url)
    
    response = requests.get(image_url)
    if response.status_code != 200:
        print(f"[ERROR] Failed to download image. Status code: {response.status_code}")
        raise Exception("Failed to download the image")
    
    temp_image_path = f"temp_image_{uuid.uuid4()}.jpg"
    print(f"[DEBUG] Temporary image path: {temp_image_path}")
    
    with open(temp_image_path, 'wb') as f:
        f.write(response.content)
    
    cmd = [
        'ffmpeg', '-loop', '1', '-i', temp_image_path,
        '-c:v', 'libx264', '-t', str(duration), '-pix_fmt', 'yuv420p',
        '-vf', f'scale={resolution[0]}:{resolution[1]}',
        '-y', output_path
    ]
    
    print(f"[DEBUG] FFmpeg command: {' '.join(cmd)}")
    result = subprocess.run(cmd, capture_output=True, text=True)
    print(f"[DEBUG] FFmpeg stdout: {result.stdout}")
    print(f"[DEBUG] FFmpeg stderr: {result.stderr}")
    
    os.remove(temp_image_path)
    print(f"[DEBUG] Temporary image removed")
    
    return output_path

def upload_file(file_path):
    print(f"\n[DEBUG] Uploading file: {file_path}")
    
    with open(file_path, 'rb') as file:
        files = {'fileToUpload': (os.path.basename(file_path), file)}
        data = {'reqtype': 'fileupload'}
        try:
            response = requests.post(UPLOAD_URL, files=files, data=data)
            print(f"[DEBUG] Upload response status code: {response.status_code}")
            print(f"[DEBUG] Upload response: {response.text}")
            
            if response.status_code == 200:
                return response.text.strip()
            return None
        except Exception as e:
            print(f"[ERROR] File upload failed: {str(e)}")
            return None

def process_video(video_url, audio_url, progress=gr.Progress()):
    print(f"\n[DEBUG] Starting video processing")
    print(f"[DEBUG] Video URL: {video_url}")
    print(f"[DEBUG] Audio URL: {audio_url}")
    
    if not audio_url:
        print("[ERROR] No audio URL provided")
        return None, "No audio URL provided"
    if not video_url:
        print("[ERROR] No video URL provided")
        return None, "No video URL provided"
        
    session_id = str(uuid.uuid4())
    print(f"[DEBUG] Session ID: {session_id}")
    
    progress(0.2, desc="Processing media...")
    
    try:
        if is_image_url(video_url):
            progress(0.3, desc="Converting image to video...")
            video_path = f"temp_video_{session_id}.mp4"
            create_video_from_image(video_url, video_path)
            progress(0.4, desc="Uploading converted video...")
            video_url = upload_file(video_path)
            if not video_url:
                raise Exception("Failed to upload converted video")
            os.remove(video_path)
        
        progress(0.5, desc="Initiating lipsync...")
        job_data = lipsync_api_call(video_url, audio_url)
        
        # Check if we have a valid job ID
        if "id" not in job_data:
            print("[ERROR] No job ID in response")
            raise Exception("No job ID received from API")
            
        # Only treat as error if error field has actual error message
        if job_data.get("error") not in [None, ""]:
            error_msg = job_data["error"]
            print(f"[ERROR] API error: {error_msg}")
            raise Exception(error_msg)
        
        job_id = job_data["id"]
        print(f"[DEBUG] Job ID: {job_id}")
        
        progress(0.6, desc="Processing lipsync...")
        result_url = check_job_status(job_id)
        
        if result_url:
            progress(0.9, desc="Downloading result...")
            print(f"[DEBUG] Downloading from: {result_url}")
            response = requests.get(result_url)
            output_path = f"output_{session_id}.mp4"
            
            with open(output_path, "wb") as f:
                f.write(response.content)
            
            print(f"[DEBUG] Result saved to: {output_path}")
            progress(1.0, desc="Complete!")
            return output_path, "Lipsync completed successfully!"
        else:
            raise Exception("Lipsync processing failed or timed out")
            
    except Exception as e:
        print(f"[ERROR] Main process failed: {str(e)}")
        progress(0.8, desc="Falling back to simple combination...")
        try:
            print("[DEBUG] Attempting fallback method")
            video_response = requests.get(video_url)
            temp_video_path = f"temp_video_{session_id}.mp4"
            with open(temp_video_path, "wb") as f:
                f.write(video_response.content)

            audio_response = requests.get(audio_url)
            temp_audio_path = f"temp_audio_{session_id}.mp3"
            with open(temp_audio_path, "wb") as f:
                f.write(audio_response.content)

            output_path = f"output_{session_id}.mp4"
            combine_audio_video(temp_video_path, temp_audio_path, output_path)
            
            os.remove(temp_video_path)
            os.remove(temp_audio_path)
            
            progress(1.0, desc="Complete!")
            return output_path, f"Used fallback method. Original error: {str(e)}"
        except Exception as fallback_error:
            print(f"[ERROR] Fallback method failed: {str(fallback_error)}")
            return None, f"All methods failed. Error: {str(fallback_error)}"

def create_interface():
    css = """
    #component-0 > :not(.prose) {display: none !important;}
    footer {display: none !important;}
    """
    with gr.Blocks(css=css) as app:
        gr.Markdown("# Lipsync Video Generator")
        with gr.Row():
            with gr.Column():
                video_url_input = gr.Textbox(label="Video or Image URL")
                audio_url_input = gr.Textbox(label="Audio URL")
                generate_btn = gr.Button("Generate Video")
            with gr.Column():
                video_output = gr.Video(label="Generated Video")
                status_output = gr.Textbox(label="Status", interactive=False)
        
        generate_btn.click(
            fn=process_video,
            inputs=[video_url_input, audio_url_input],
            outputs=[video_output, status_output]
        )
    
    return app

if __name__ == "__main__":
    print("[DEBUG] Starting application")
    app = create_interface()
    app.launch()