Spaces:
Runtime error
Runtime error
traltyaziking
commited on
Commit
•
15d5db5
1
Parent(s):
ec46769
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,134 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
import shutil
|
6 |
|
7 |
+
class VideoProcessor:
|
8 |
+
def __init__(self, segment_length):
|
9 |
+
self.segment_length = segment_length
|
10 |
+
|
11 |
+
def process(self, video, audio, progress):
|
12 |
+
self._temp_dizinlerini_baslat()
|
13 |
+
video_segmentleri, audio_segmentleri = self._segmentleri_kes(video, audio)
|
14 |
+
islenmis_segmentler = self._segmentleri_isle(video_segmentleri, audio_segmentleri, progress)
|
15 |
+
cikis_dosyasi = self._videolari_birlestir(islenmis_segmentler)
|
16 |
+
self._gecici_dosyalari_temizle(video_segmentleri + audio_segmentleri)
|
17 |
+
return cikis_dosyasi
|
18 |
+
|
19 |
+
def _temp_dizinlerini_baslat(self):
|
20 |
+
for dizin in ['temp/audio', 'temp/video']:
|
21 |
+
shutil.rmtree(dizin, ignore_errors=True)
|
22 |
+
os.makedirs(dizin, exist_ok=True)
|
23 |
+
|
24 |
+
def _segmentleri_kes(self, video_dosyasi, audio_dosyasi):
|
25 |
+
video_segmentleri = self._video_segmentlerini_kes(video_dosyasi)
|
26 |
+
audio_segmentleri = self._audio_segmentlerini_kes(audio_dosyasi)
|
27 |
+
return video_segmentleri, audio_segmentleri
|
28 |
+
|
29 |
+
def _video_segmentlerini_kes(self, video_dosyasi):
|
30 |
+
temp_dizin = 'temp/audio'
|
31 |
+
segment_sablonu = f"{temp_dizin}/{random.randint(0,1000)}_%03d.mp4"
|
32 |
+
komut = ["ffmpeg", "-i", video_dosyasi, "-c", "copy", "-f",
|
33 |
+
"segment", "-segment_time", str(self.segment_length), segment_sablonu]
|
34 |
+
subprocess.run(komut, check=True)
|
35 |
+
return [segment_sablonu % i for i in range(len(os.listdir(temp_dizin)))]
|
36 |
+
|
37 |
+
def _audio_segmentlerini_kes(self, audio_dosyasi):
|
38 |
+
temp_dizin = 'temp/video'
|
39 |
+
segment_sablonu = f"{temp_dizin}/{random.randint(0,1000)}_%03d.mp3"
|
40 |
+
komut = ["ffmpeg", "-i", audio_dosyasi, "-f", "segment",
|
41 |
+
"-segment_time", str(self.segment_length), segment_sablonu]
|
42 |
+
subprocess.run(komut, check=True)
|
43 |
+
return [segment_sablonu % i for i in range(len(os.listdir(temp_dizin)))]
|
44 |
+
|
45 |
+
def _segmentleri_isle(self, video_segmentleri, audio_segmentleri, progress):
|
46 |
+
islenmis_segmentler = []
|
47 |
+
for i, (video_seg, audio_seg) in progress.tqdm(enumerate(zip(video_segmentleri, audio_segmentleri))):
|
48 |
+
islenmis_cikti = self._segmenti_isle(video_seg, audio_seg, i)
|
49 |
+
islenmis_segmentler.append(islenmis_cikti)
|
50 |
+
return islenmis_segmentler
|
51 |
+
|
52 |
+
def _segmenti_isle(self, video_seg, audio_seg, i):
|
53 |
+
cikis_dosyasi = f"results/{random.randint(10,100000)}_{i}.mp4"
|
54 |
+
komut = ["python", "inference.py", "--face", video_seg,
|
55 |
+
"--audio", audio_seg, "--outfile", cikis_dosyasi]
|
56 |
+
subprocess.run(komut, check=True)
|
57 |
+
return cikis_dosyasi
|
58 |
+
|
59 |
+
def _videolari_birlestir(self, video_segmentleri):
|
60 |
+
cikis_dosyasi = f"results/output_{random.randint(0,1000)}.mp4"
|
61 |
+
with open("segments.txt", "w") as dosya:
|
62 |
+
for segment in video_segmentleri:
|
63 |
+
dosya.write(f"file '{segment}'\n")
|
64 |
+
komut = ["ffmpeg", "-f", "concat", "-i",
|
65 |
+
"segments.txt", "-c", "copy", cikis_dosyasi]
|
66 |
+
subprocess.run(komut, check=True)
|
67 |
+
return cikis_dosyasi
|
68 |
+
|
69 |
+
def _gecici_dosyalari_temizle(self, dosya_listesi):
|
70 |
+
for dosya_yolu in dosya_listesi:
|
71 |
+
if os.path.isfile(dosya_yolu):
|
72 |
+
os.remove(dosya_yolu)
|
73 |
+
|
74 |
+
|
75 |
+
with gr.Blocks(
|
76 |
+
title="Ses Tabanlı Dudak Senkronizasyonu",
|
77 |
+
theme=gr.themes.Base(
|
78 |
+
primary_hue=gr.themes.colors.green,
|
79 |
+
font=["Source Sans Pro", "Arial", "sans-serif"],
|
80 |
+
font_mono=['JetBrains mono', "Consolas", 'Courier New']
|
81 |
+
),
|
82 |
+
) as demo:
|
83 |
+
with gr.Row():
|
84 |
+
gr.Markdown("# Ses Tabanlı Dudak Senkronizasyonu")
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column():
|
87 |
+
with gr.Row():
|
88 |
+
seg = gr.Number(
|
89 |
+
label="Segment uzunluğu (Saniye), 0 için bölme yapılmaz")
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
v = gr.Video(label='Kaynak Yüz')
|
93 |
+
|
94 |
+
with gr.Column():
|
95 |
+
a = gr.Audio(
|
96 |
+
type='filepath', label='Hedef Ses')
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
btn = gr.Button(value="Sentezle",variant="primary")
|
100 |
+
with gr.Row():
|
101 |
+
gr.Examples(
|
102 |
+
label="Yüz Örnekleri",
|
103 |
+
examples=[
|
104 |
+
os.path.join(os.path.dirname(__file__),
|
105 |
+
"examples/face/1.mp4"),
|
106 |
+
os.path.join(os.path.dirname(__file__),
|
107 |
+
"examples/face/2.mp4"),
|
108 |
+
os.path.join(os.path.dirname(__file__),
|
109 |
+
"examples/face/3.mp4"),
|
110 |
+
os.path.join(os.path.dirname(__file__),
|
111 |
+
"examples/face/4.mp4"),
|
112 |
+
os.path.join(os.path.dirname(__file__),
|
113 |
+
"examples/face/5.mp4"),
|
114 |
+
],
|
115 |
+
inputs=[v],
|
116 |
+
fn=VideoProcessor(0).process,
|
117 |
+
)
|
118 |
+
with gr.Row():
|
119 |
+
gr.Examples(
|
120 |
+
label="Ses Örnekleri",
|
121 |
+
examples=[
|
122 |
+
os.path.join(os.path.dirname(__file__),
|
123 |
+
"examples/audio/1.wav"),
|
124 |
+
os.path.join(os.path.dirname(__file__),
|
125 |
+
"examples/audio/2.wav"),
|
126 |
+
],
|
127 |
+
inputs=[a],
|
128 |
+
fn=VideoProcessor(0).process,
|
129 |
+
)
|
130 |
+
|
131 |
+
with gr.Column():
|
132 |
+
o = gr.Video(label="Çıkış Videosu")
|
133 |
+
|
134 |
+
btn.click(fn=VideoProcessor(0).process, inputs=[seg
|