Ii commited on
Commit
53d8a0a
·
verified ·
1 Parent(s): ad53592

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
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 # Importing tempfile module
 
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
- # Open the refaced video file and write to a temporary file
52
- with open(refaced_video_path, "rb") as f:
53
- # Use tempfile to store the refaced video in memory
54
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
55
- temp_file.write(f.read())
56
- temp_file.close() # Close the temporary file
 
 
 
 
 
 
 
 
57
 
58
- # Reopen the temp file to use in the Gradio UI
59
- with open(temp_file.name, "rb") as temp_f:
60
- video_buffer = io.BytesIO(temp_f.read())
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