Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,25 @@ import json
|
|
| 5 |
import re
|
| 6 |
from moviepy.editor import VideoFileClip
|
| 7 |
from moviepy.audio.AudioClip import AudioClip
|
|
|
|
| 8 |
|
| 9 |
hf_token = os.environ.get("HF_TKN")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def extract_audio(video_in):
|
| 12 |
input_video = video_in
|
| 13 |
output_audio = 'audio.wav'
|
|
@@ -136,21 +152,27 @@ def get_tango(prompt):
|
|
| 136 |
print(result)
|
| 137 |
return result
|
| 138 |
|
| 139 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
caption = get_caption(image_in)
|
| 141 |
if chosen_model == "MAGNet" :
|
| 142 |
-
|
| 143 |
-
return magnet_result
|
| 144 |
elif chosen_model == "AudioLDM-2" :
|
| 145 |
-
|
| 146 |
-
return audioldm_result
|
| 147 |
elif chosen_model == "AudioGen" :
|
| 148 |
-
|
| 149 |
-
return audiogen_result
|
| 150 |
elif chosen_model == "Tango" :
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
css="""
|
| 155 |
#col-container{
|
| 156 |
margin: 0 auto;
|
|
@@ -162,25 +184,28 @@ with gr.Blocks(css=css) as demo:
|
|
| 162 |
with gr.Column(elem_id="col-container"):
|
| 163 |
gr.HTML("""
|
| 164 |
<h2 style="text-align: center;">
|
| 165 |
-
|
| 166 |
</h2>
|
| 167 |
<p style="text-align: center;">
|
| 168 |
-
|
| 169 |
</p>
|
| 170 |
""")
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
| 179 |
|
| 180 |
submit_btn.click(
|
| 181 |
fn=infer,
|
| 182 |
-
inputs=[
|
| 183 |
-
outputs=[audio_o],
|
| 184 |
concurrency_limit = 2
|
| 185 |
)
|
| 186 |
|
|
|
|
| 5 |
import re
|
| 6 |
from moviepy.editor import VideoFileClip
|
| 7 |
from moviepy.audio.AudioClip import AudioClip
|
| 8 |
+
import cv2
|
| 9 |
|
| 10 |
hf_token = os.environ.get("HF_TKN")
|
| 11 |
|
| 12 |
+
def extract_firstframe():
|
| 13 |
+
vidcap = cv2.VideoCapture('yourvideo.mp4') # replace yourvideo.mp4 with actual filename of your video
|
| 14 |
+
success,image = vidcap.read()
|
| 15 |
+
count = 0
|
| 16 |
+
while success:
|
| 17 |
+
if count == 0:
|
| 18 |
+
cv2.imwrite("first_frame.jpg", image) # save first extracted frame as jpg file named first_frame.jpg
|
| 19 |
+
else:
|
| 20 |
+
break # exit loop after saving first frame
|
| 21 |
+
success,image = vidcap.read()
|
| 22 |
+
print ('Read a new frame: ', success)
|
| 23 |
+
count += 1
|
| 24 |
+
print ("Done extracted first frame!")
|
| 25 |
+
return "first_frame.jpg"
|
| 26 |
+
|
| 27 |
def extract_audio(video_in):
|
| 28 |
input_video = video_in
|
| 29 |
output_audio = 'audio.wav'
|
|
|
|
| 152 |
print(result)
|
| 153 |
return result
|
| 154 |
|
| 155 |
+
def blend_vsfx(video_in, audio_result):
|
| 156 |
+
audioClip = AudioFileClip(audio_result)
|
| 157 |
+
clip = VideoFileClip(video_in)
|
| 158 |
+
final_clip = clip.set_audio(audioClip)
|
| 159 |
+
final_clip.write_videofile('final_video_with_sound.mp4')
|
| 160 |
+
return "final_video_with_sound.mp4"
|
| 161 |
+
|
| 162 |
+
def infer(video_in, chosen_model):
|
| 163 |
+
image_in = extract_firstframe(video_in)
|
| 164 |
caption = get_caption(image_in)
|
| 165 |
if chosen_model == "MAGNet" :
|
| 166 |
+
audio_result = get_magnet(caption)
|
|
|
|
| 167 |
elif chosen_model == "AudioLDM-2" :
|
| 168 |
+
audio_result = get_audioldm(caption)
|
|
|
|
| 169 |
elif chosen_model == "AudioGen" :
|
| 170 |
+
audio_result = get_audiogen(caption)
|
|
|
|
| 171 |
elif chosen_model == "Tango" :
|
| 172 |
+
audio_result = get_tango(caption)
|
| 173 |
+
|
| 174 |
+
final_res = blend_vsfx(video_in, audio_result)
|
| 175 |
+
return audio_result, final_res
|
| 176 |
css="""
|
| 177 |
#col-container{
|
| 178 |
margin: 0 auto;
|
|
|
|
| 184 |
with gr.Column(elem_id="col-container"):
|
| 185 |
gr.HTML("""
|
| 186 |
<h2 style="text-align: center;">
|
| 187 |
+
Video to SoundFX
|
| 188 |
</h2>
|
| 189 |
<p style="text-align: center;">
|
| 190 |
+
Get sound effectsfor from video while comparing models from image caption.
|
| 191 |
</p>
|
| 192 |
""")
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
|
| 196 |
+
with gr.Column():
|
| 197 |
+
video_in = gr.Video(sources=["upload"], type="filepath", label="Video input")
|
| 198 |
+
with gr.Row():
|
| 199 |
+
chosen_model = gr.Dropdown(label="Choose a model", choices=["MAGNet", "AudioLDM-2", "AudioGen", "Tango"], value="AudioLDM-2")
|
| 200 |
+
submit_btn = gr.Button("Submit")
|
| 201 |
+
with gr.Column():
|
| 202 |
+
audio_o = gr.Audio(label="Audio output")
|
| 203 |
+
video_o gr.Video(label="Video with soundFX")
|
| 204 |
|
| 205 |
submit_btn.click(
|
| 206 |
fn=infer,
|
| 207 |
+
inputs=[video_in_in, chosen_model],
|
| 208 |
+
outputs=[audio_o, video_o],
|
| 209 |
concurrency_limit = 2
|
| 210 |
)
|
| 211 |
|