Spaces:
Runtime error
Runtime error
import os | |
import subprocess | |
import time | |
from datetime import datetime | |
import cv2 | |
from flask import Flask, request, jsonify, send_file | |
from flask_ngrok2 import run_with_ngrok | |
from subprocess import call | |
from tqdm import tqdm | |
import numpy as np | |
from ffmpy import FFmpeg | |
# !pip install flask flask-ngrok2 pyngrok | |
app = Flask(__name__) | |
auth_token = '2XQTtjJqVg4B4ryQVgauTPIGeiK_3JUWVJcMhXwpPxz2sc9KB' | |
root_dir = '/content/wav2lip-gfpgan' | |
jobs_dir = os.path.join('/content', 'jobs') | |
def index(): | |
return jsonify({'hello': 'world!'}) | |
# New route to serve files from /content/input | |
def download_file(job_id, filename): | |
try: | |
file_path = os.path.join(jobs_dir, job_id, filename) | |
print(file_path) | |
return send_file(file_path, as_attachment=True) | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |
def wav2lip(): | |
try: | |
# Get uploaded files | |
video_file = request.files['video'] | |
audio_file = request.files['audio'] | |
job_id = str(int(time.time())) | |
job_path = os.path.join(jobs_dir, job_id) | |
os.makedirs(job_path, exist_ok=True) | |
# Save the files to a temporary directory | |
video_path = os.path.join(job_path, video_file.filename) | |
audio_path = os.path.join(job_path, audio_file.filename) | |
video_file.save(video_path) | |
audio_file.save(audio_path) | |
wav2lip_mp4 = os.path.join(job_path, 'wav2lip.mp4') | |
call_wav2lip(video_path, audio_path, wav2lip_mp4) | |
output_filename = 'output.mp4' | |
output_mp4 = os.path.join(job_path, output_filename) | |
call_gfpgan(wav2lip_mp4, audio_path, output_mp4) | |
return jsonify({'url': f'/job/{job_id}/{output_filename}'}) | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |
def call_wav2lip(video_path, audio_path, output_path): | |
checkpoint_path = os.path.join(root_dir, 'wav2lip/checkpoints/wav2lip.pth') | |
assert os.path.isfile(video_path), f'Video path {video_path} not exist.' | |
assert os.path.isfile(audio_path), f'Audio path {audio_path} not exist.' | |
assert os.path.isfile(checkpoint_path), f'Checkpoint file {checkpoint_path} not exist.' | |
# python inference.py \ | |
# --checkpoint_path checkpoints/wav2lip.pth \ | |
# --face {inputVideoPath} \ | |
# --audio {inputAudioPath} \ | |
# --outfile {lipSyncedOutputPath} | |
start = datetime.now() | |
cmd = [ | |
"python", | |
"wav2lip/inference.py", | |
"--checkpoint_path", checkpoint_path, # | |
# "--segmentation_path", "checkpoints/face_segmentation.pth", | |
"--face", video_path, | |
"--audio", audio_path, | |
"--outfile", output_path, | |
] | |
print(f'Call subprocess: {cmd}') | |
proc = subprocess.Popen(cmd, shell=False) | |
proc.communicate() | |
duration = datetime.now() - start | |
print(f'wav2lip finished in {duration}') | |
return output_path | |
def _get_frames(video_path): | |
folder_path = os.path.dirname(video_path) | |
origin_frames_folder = os.path.join(folder_path, 'frames') | |
os.makedirs(origin_frames_folder, exist_ok=True) | |
# get frames pics | |
vidcap = cv2.VideoCapture(video_path) | |
numberOfFrames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
fps = vidcap.get(cv2.CAP_PROP_FPS) | |
print("FPS: ", fps, "Frames: ", numberOfFrames) | |
for frameNumber in tqdm(range(numberOfFrames)): | |
_, image = vidcap.read() | |
cv2.imwrite(os.path.join(origin_frames_folder, str(frameNumber).zfill(4) + '.jpg'), image) | |
return origin_frames_folder | |
def call_gfpgan(wav2lip_mp4, audio_path, output_mp4): | |
assert os.path.isfile(wav2lip_mp4), f'Video path {wav2lip_mp4} not exist.' | |
origin_frames_folder = _get_frames(wav2lip_mp4) | |
folder_path = os.path.dirname(wav2lip_mp4) | |
# python inference_gfpgan.py | |
# -i "$unProcessedFramesFolderPath" | |
# -o "$outputPath" | |
# -v 1.3 | |
# -s 2 | |
# --only_center_face | |
# --bg_upsampler None | |
start = datetime.now() | |
cmd = [ | |
"python", | |
"gfpgan/inference_gfpgan.py", | |
"-i", origin_frames_folder, | |
"-o", folder_path, | |
# "-v", str(1.4), | |
# "-s", str(2), | |
"--only_center_face", | |
"--bg_upsampler", 'realesrgan' | |
] | |
print(cmd) | |
proc = subprocess.Popen(cmd, shell=True) | |
proc.communicate() | |
duration = datetime.now() - start | |
print(f'inference_gfpgan finished in {duration}') | |
start = datetime.now() | |
cmd = [ | |
"python", | |
"merge.py", | |
"-j", folder_path, | |
"-a", audio_path, | |
"-o", output_mp4, | |
] | |
proc = subprocess.Popen(cmd, shell=True) | |
proc.communicate() | |
duration = datetime.now() - start | |
print(f'Merge output in {duration}') | |
print(output_mp4) | |
# from google.colab import files | |
# files.download(finalProcessedOuputVideo) | |
if __name__ == '__main__': | |
run_with_ngrok(app, auth_token=auth_token) | |
app.run() | |