File size: 2,839 Bytes
795e2af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8274d3f
 
 
3b23a4a
795e2af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07e21d6
 
 
795e2af
07e21d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
795e2af
 
07e21d6
795e2af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import FileResponse
import subprocess
import shutil
import os

app = FastAPI(
    title="Wav2Lip API",
    version="1.0"
)

os.makedirs("sample_data", exist_ok=True)
os.makedirs("results", exist_ok=True)


def merge_audio_video(video_path, audio_path, output_path):

    if os.path.exists(output_path):
        os.remove(output_path)

    subprocess.run(
        [
            "ffmpeg",
            "-y",
            "-i", video_path,
            "-i", audio_path,
            "-c:v", "copy",
            "-c:a", "aac",
            "-map", "0:v:0",
            "-map", "1:a:0",
            output_path
        ],
        check=True
    )

    return output_path


@app.get("/")
def root():
    return {
        "message": "Wav2Lip API is running",
        "docs": "/docs"
    }


@app.post("/generate-video")
async def generate_video(
    image: UploadFile = File(...),
    audio: UploadFile = File(...)
):

    image_path = "sample_data/input_image.png"
    audio_path = "sample_data/input_audio.mp3"

    with open(image_path, "wb") as f:
        shutil.copyfileobj(image.file, f)

    with open(audio_path, "wb") as f:
        shutil.copyfileobj(audio.file, f)

    process = subprocess.run(
        [
            "python",
            "inference.py",
            "--checkpoint_path",
            "checkpoints/wav2lip_gan.pth",
            "--face",
            image_path,
            "--audio",
            audio_path
        ],
        capture_output=True,
        text=True
    )

    # دمج stdout و stderr
    output = (process.stdout or "") + "\n" + (process.stderr or "")

    if process.returncode != 0:

        # خطأ عدم اكتشاف وجه
        if "Face not detected!" in output or "No face detected" in output:
            raise HTTPException(
                status_code=400,
                detail="No face detected in the uploaded image. Please upload a clear front-facing image."
            )

        # خطأ الملف الصوتي
        if "Mel contains nan" in output:
            raise HTTPException(
                status_code=400,
                detail="The uploaded audio is invalid or unsupported."
            )

        # أي خطأ آخر
        raise HTTPException(
            status_code=500,
            detail=output
        )

    wav2lip_video = "results/result_voice.mp4"

    if not os.path.exists(wav2lip_video):
        raise HTTPException(
            status_code=500,
            detail="Wav2Lip output video not found."
        )

    final_video = "results/final_output.mp4"

    merge_audio_video(
        wav2lip_video,
        audio_path,
        final_video
    )

    return FileResponse(
        final_video,
        media_type="video/mp4",
        filename="final_output.mp4"
    )