Spaces:
Running
Running
Commit Β·
fa37ee5
1
Parent(s): ee20eba
Restore YouTube URL input with yt-dlp
Browse files- app.py +38 -9
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -55,12 +55,32 @@ def to_h264(src: str, dst: str):
|
|
| 55 |
)
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# ββ Core processing ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
-
def process(face_image, video_file, trim_seconds, progress=gr.Progress(track_tqdm=True)):
|
| 60 |
if face_image is None:
|
| 61 |
return None, "Please upload a source face image."
|
| 62 |
-
if video_file is None:
|
| 63 |
-
return None, "Please upload a video file."
|
| 64 |
|
| 65 |
try:
|
| 66 |
progress(0.0, desc="Detecting source face...")
|
|
@@ -82,12 +102,16 @@ def process(face_image, video_file, trim_seconds, progress=gr.Progress(track_tqd
|
|
| 82 |
source_face.embedding /= np.linalg.norm(source_face.embedding)
|
| 83 |
|
| 84 |
# Prepare video
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
converted = f"{WORK_DIR}/temp/input_h264.mp4"
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# Verify codec
|
| 93 |
cap_check = cv2.VideoCapture(converted)
|
|
@@ -176,6 +200,11 @@ Swap any face into a video using **InsightFace + inswapper_128**.
|
|
| 176 |
type="filepath",
|
| 177 |
height=220,
|
| 178 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
video_input = gr.Video(label="Upload Video File")
|
| 180 |
trim_input = gr.Slider(
|
| 181 |
label="Trim to first N seconds (0 = full video)",
|
|
@@ -197,7 +226,7 @@ Swap any face into a video using **InsightFace + inswapper_128**.
|
|
| 197 |
|
| 198 |
run_btn.click(
|
| 199 |
fn=process,
|
| 200 |
-
inputs=[face_input, video_input, trim_input],
|
| 201 |
outputs=[video_out, status_box],
|
| 202 |
)
|
| 203 |
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
|
| 58 |
+
def download_youtube(url: str, out_path: str):
|
| 59 |
+
import yt_dlp
|
| 60 |
+
raw = out_path.replace(".mp4", "_raw.mp4")
|
| 61 |
+
ydl_opts = {
|
| 62 |
+
"outtmpl": raw,
|
| 63 |
+
"format": "best[height<=480]/best",
|
| 64 |
+
"http_headers": {
|
| 65 |
+
"User-Agent": (
|
| 66 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 67 |
+
"AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36"
|
| 68 |
+
)
|
| 69 |
+
},
|
| 70 |
+
}
|
| 71 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 72 |
+
ydl.download([url])
|
| 73 |
+
to_h264(raw, out_path)
|
| 74 |
+
if os.path.exists(raw):
|
| 75 |
+
os.remove(raw)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
# ββ Core processing ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
+
def process(face_image, video_file, youtube_url, trim_seconds, progress=gr.Progress(track_tqdm=True)):
|
| 80 |
if face_image is None:
|
| 81 |
return None, "Please upload a source face image."
|
| 82 |
+
if video_file is None and not (youtube_url or "").strip():
|
| 83 |
+
return None, "Please upload a video file or paste a YouTube URL."
|
| 84 |
|
| 85 |
try:
|
| 86 |
progress(0.0, desc="Detecting source face...")
|
|
|
|
| 102 |
source_face.embedding /= np.linalg.norm(source_face.embedding)
|
| 103 |
|
| 104 |
# Prepare video
|
| 105 |
+
raw_video = f"{WORK_DIR}/temp/input.mp4"
|
| 106 |
+
converted = f"{WORK_DIR}/temp/input_h264.mp4"
|
|
|
|
| 107 |
|
| 108 |
+
if (youtube_url or "").strip():
|
| 109 |
+
progress(0.05, desc="Downloading YouTube video...")
|
| 110 |
+
download_youtube(youtube_url.strip(), converted)
|
| 111 |
+
else:
|
| 112 |
+
progress(0.05, desc="Preparing video...")
|
| 113 |
+
shutil.copy(video_file, raw_video)
|
| 114 |
+
to_h264(raw_video, converted)
|
| 115 |
|
| 116 |
# Verify codec
|
| 117 |
cap_check = cv2.VideoCapture(converted)
|
|
|
|
| 200 |
type="filepath",
|
| 201 |
height=220,
|
| 202 |
)
|
| 203 |
+
youtube_input = gr.Textbox(
|
| 204 |
+
label="YouTube URL (optional)",
|
| 205 |
+
placeholder="https://www.youtube.com/watch?v=...",
|
| 206 |
+
)
|
| 207 |
+
gr.Markdown("**β or β**")
|
| 208 |
video_input = gr.Video(label="Upload Video File")
|
| 209 |
trim_input = gr.Slider(
|
| 210 |
label="Trim to first N seconds (0 = full video)",
|
|
|
|
| 226 |
|
| 227 |
run_btn.click(
|
| 228 |
fn=process,
|
| 229 |
+
inputs=[face_input, video_input, youtube_input, trim_input],
|
| 230 |
outputs=[video_out, status_box],
|
| 231 |
)
|
| 232 |
|
requirements.txt
CHANGED
|
@@ -2,4 +2,5 @@ insightface==0.7.3
|
|
| 2 |
onnxruntime
|
| 3 |
opencv-python-headless
|
| 4 |
huggingface_hub
|
|
|
|
| 5 |
gradio
|
|
|
|
| 2 |
onnxruntime
|
| 3 |
opencv-python-headless
|
| 4 |
huggingface_hub
|
| 5 |
+
yt-dlp
|
| 6 |
gradio
|