Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,8 @@ import gradio as gr
|
|
3 |
from refacer import Refacer
|
4 |
import os
|
5 |
import requests
|
6 |
-
import tempfile
|
|
|
7 |
|
8 |
# Hugging Face URL to download the model
|
9 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
@@ -48,18 +49,25 @@ def run(video_path, *vars):
|
|
48 |
refaced_video_path = refacer.reface(video_path, faces)
|
49 |
print(f"Refaced video can be found at {refaced_video_path}")
|
50 |
|
51 |
-
#
|
52 |
-
with
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
#
|
59 |
-
with open(
|
60 |
-
video_buffer = io.BytesIO(
|
61 |
|
62 |
-
# Return the video buffer for Gradio to display in the UI
|
63 |
return video_buffer # Gradio will handle the video display
|
64 |
|
65 |
# Prepare Gradio components
|
|
|
3 |
from refacer import Refacer
|
4 |
import os
|
5 |
import requests
|
6 |
+
import tempfile
|
7 |
+
import subprocess
|
8 |
|
9 |
# Hugging Face URL to download the model
|
10 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
|
|
49 |
refaced_video_path = refacer.reface(video_path, faces)
|
50 |
print(f"Refaced video can be found at {refaced_video_path}")
|
51 |
|
52 |
+
# Use tempfile to create a temporary video file
|
53 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
|
54 |
+
temp_file_path = temp_file.name
|
55 |
+
|
56 |
+
# Run ffmpeg to process the video and write the result to the temporary file
|
57 |
+
ffmpeg_command = [
|
58 |
+
"ffmpeg", "-i", refaced_video_path, "-c:v", "libx264",
|
59 |
+
"-crf", "23", "-preset", "fast", temp_file_path
|
60 |
+
]
|
61 |
+
|
62 |
+
try:
|
63 |
+
subprocess.run(ffmpeg_command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
64 |
+
except subprocess.CalledProcessError as e:
|
65 |
+
print(f"ffmpeg error: {e.stderr.decode()}") # Print stderr output for debugging
|
66 |
|
67 |
+
# Read the temporary file and convert it to a BytesIO buffer
|
68 |
+
with open(temp_file_path, "rb") as f:
|
69 |
+
video_buffer = io.BytesIO(f.read())
|
70 |
|
|
|
71 |
return video_buffer # Gradio will handle the video display
|
72 |
|
73 |
# Prepare Gradio components
|