Commit message: Added support for hardware accelerators in ffmpeg. #5
This commit is contained in:
4
app.py
4
app.py
@@ -37,8 +37,8 @@ with gr.Blocks() as demo:
|
|||||||
with gr.Row():
|
with gr.Row():
|
||||||
gr.Markdown("# Refacer")
|
gr.Markdown("# Refacer")
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
video=gr.Video(label="Original video")
|
video=gr.Video(label="Original video",format="mp4")
|
||||||
video2=gr.Video(label="Refaced video",interactive=False)
|
video2=gr.Video(label="Refaced video",interactive=False,format="mp4")
|
||||||
|
|
||||||
for i in range(0,num_faces):
|
for i in range(0,num_faces):
|
||||||
with gr.Tab(f"Face #{i+1}"):
|
with gr.Tab(f"Face #{i+1}"):
|
||||||
|
|||||||
@@ -35,27 +35,14 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"!pip uninstall numpy -y -q\n",
|
"!pip uninstall numpy -y -q\n",
|
||||||
"!pip install numpy==1.24.3 onnxruntime-gpu gradio insightface ffmpeg_python opencv_python -q --force\n",
|
"!pip install --disable-pip-version-check --root-user-action=ignore numpy==1.24.3 onnxruntime-gpu gradio insightface==0.7.3 ffmpeg_python opencv_python -q --force\n",
|
||||||
"\n",
|
"\n",
|
||||||
"!git clone https://github.com/xaviviro/refacer.git\n",
|
"!git clone https://github.com/xaviviro/refacer.git\n",
|
||||||
"%cd refacer\n",
|
"%cd refacer\n",
|
||||||
"\n",
|
"\n",
|
||||||
"!wget --content-disposition \"https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx\""
|
"!wget --content-disposition \"https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx\"\n",
|
||||||
]
|
"\n",
|
||||||
},
|
"!python app.py --share_gradio\n"
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"colab": {
|
|
||||||
"base_uri": "https://localhost:8080/"
|
|
||||||
},
|
|
||||||
"id": "0SsyI3quBb0L",
|
|
||||||
"outputId": "56add1a6-6b47-429d-e07a-cf3fadc7a6ce"
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"!python app.py --share_gradio"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
34
refacer.py
34
refacer.py
@@ -18,6 +18,8 @@ import psutil
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from insightface.app.common import Face
|
from insightface.app.common import Face
|
||||||
from insightface.utils.storage import ensure_available
|
from insightface.utils.storage import ensure_available
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
class RefacerMode(Enum):
|
class RefacerMode(Enum):
|
||||||
CPU, CUDA, COREML, TENSORRT = range(1, 5)
|
CPU, CUDA, COREML, TENSORRT = range(1, 5)
|
||||||
@@ -25,6 +27,7 @@ class RefacerMode(Enum):
|
|||||||
class Refacer:
|
class Refacer:
|
||||||
def __init__(self,force_cpu=False):
|
def __init__(self,force_cpu=False):
|
||||||
self.force_cpu = force_cpu
|
self.force_cpu = force_cpu
|
||||||
|
self.__check_encoders()
|
||||||
self.__check_providers()
|
self.__check_providers()
|
||||||
self.total_mem = psutil.virtual_memory().total
|
self.total_mem = psutil.virtual_memory().total
|
||||||
self.__init_apps()
|
self.__init_apps()
|
||||||
@@ -96,12 +99,14 @@ class Refacer:
|
|||||||
|
|
||||||
return replacements
|
return replacements
|
||||||
def __convert_video(self,video_path,output_video_path):
|
def __convert_video(self,video_path,output_video_path):
|
||||||
|
print("Merging audio with the refaced video...")
|
||||||
new_path = output_video_path + str(random.randint(0,999)) + "_c.mp4"
|
new_path = output_video_path + str(random.randint(0,999)) + "_c.mp4"
|
||||||
#stream = ffmpeg.input(output_video_path)
|
#stream = ffmpeg.input(output_video_path)
|
||||||
in1 = ffmpeg.input(output_video_path)
|
in1 = ffmpeg.input(output_video_path)
|
||||||
in2 = ffmpeg.input(video_path)
|
in2 = ffmpeg.input(video_path)
|
||||||
out = ffmpeg.output(in1.video, in2.audio, new_path,vcodec="libx264")
|
out = ffmpeg.output(in1.video, in2.audio, new_path,vcodec=self.ffmpeg_video_encoder)
|
||||||
out.run()
|
out.run(overwrite_output=True,quiet=True)
|
||||||
|
print(f"The process has finished.\nThe refaced video can be found at {os.path.abspath(new_path)}")
|
||||||
return new_path
|
return new_path
|
||||||
|
|
||||||
def __get_faces(self,frame,max_num=0):
|
def __get_faces(self,frame,max_num=0):
|
||||||
@@ -169,3 +174,28 @@ class Refacer:
|
|||||||
output.release()
|
output.release()
|
||||||
|
|
||||||
return self.__convert_video(video_path,output_video_path)
|
return self.__convert_video(video_path,output_video_path)
|
||||||
|
|
||||||
|
def __check_encoders(self):
|
||||||
|
self.ffmpeg_video_encoder="libx264"
|
||||||
|
pattern = r"encoders: ([a-zA-Z0-9_]+(?: [a-zA-Z0-9_]+)*)"
|
||||||
|
command = ['ffmpeg', '-codecs', '--list-encoders']
|
||||||
|
commandout = subprocess.run(command, check=True, capture_output=True).stdout
|
||||||
|
result = commandout.decode('utf-8').split('\n')
|
||||||
|
for r in result:
|
||||||
|
if "264" in r:
|
||||||
|
encoders = re.search(pattern, r).group(1).split(' ')
|
||||||
|
#print(encoders)
|
||||||
|
for v_c in Refacer.VIDEO_CODECS:
|
||||||
|
if v_c in encoders:
|
||||||
|
self.ffmpeg_video_encoder=v_c
|
||||||
|
break
|
||||||
|
print(f"Video codec for FFMPEG: {self.ffmpeg_video_encoder}")
|
||||||
|
|
||||||
|
VIDEO_CODECS = [
|
||||||
|
#'h264_videotoolbox', #osx HW acceleration
|
||||||
|
'h264_nvenc', #NVIDIA HW acceleration
|
||||||
|
#'h264_qsv', #Intel HW acceleration
|
||||||
|
#'h264_vaapi', #Intel HW acceleration
|
||||||
|
#'h264_omx', #HW acceleration
|
||||||
|
'libx264', #No HW acceleration
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user