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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import os
|
| 4 |
+
#import json
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
import uuid
|
| 8 |
+
import moviepy
|
| 9 |
+
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
| 10 |
+
import moviepy.video.io.ImageSequenceClip
|
| 11 |
+
from moviepy.editor import *
|
| 12 |
+
from PIL import Image
|
| 13 |
+
uid=uuid.uuid4()
|
| 14 |
+
if not os.path.exists(f'{uid}'): os.makedirs(f'{uid}')
|
| 15 |
+
if not os.path.exists(f'{uid}-frames'): os.makedirs(f'{uid}-frames')
|
| 16 |
+
if not os.path.exists(f'{uid}-rembg'): os.makedirs(f'{uid}-rembg')
|
| 17 |
+
esr = gr.Interface.load("spaces/Omnibus/Real-ESRGAN-mod")
|
| 18 |
+
|
| 19 |
+
load_js = """
|
| 20 |
+
function(text_input, url_params) {
|
| 21 |
+
console.log(text_input, url_params);
|
| 22 |
+
const params = new URLSearchParams(window.location.search);
|
| 23 |
+
url_params = Object.fromEntries(params);
|
| 24 |
+
return [text_input, url_params]
|
| 25 |
+
}
|
| 26 |
+
"""
|
| 27 |
+
def load_video(vid):
|
| 28 |
+
new_video_in = str(vid)
|
| 29 |
+
capture = cv2.VideoCapture(new_video_in)
|
| 30 |
+
fps = capture.get(cv2.CAP_PROP_FPS)
|
| 31 |
+
msc = capture.get(cv2.CAP_PROP_POS_MSEC)
|
| 32 |
+
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 33 |
+
msc = float(frame_count/fps)
|
| 34 |
+
capture.release()
|
| 35 |
+
vid_t = round(msc)
|
| 36 |
+
hours = int(vid_t/360)
|
| 37 |
+
minutes = int(vid_t/60)-(hours*360)
|
| 38 |
+
seconds = vid_t-(minutes*60)-(hours*360)
|
| 39 |
+
vid_len = f'{hours}:{minutes}:{seconds}'
|
| 40 |
+
return frame_count, fps, vid_len, vid
|
| 41 |
+
def im_2_vid(images,fps):
|
| 42 |
+
this_im=cv2.imread(images[0])
|
| 43 |
+
height=this_im.shape[0]
|
| 44 |
+
width= this_im.shape[1]
|
| 45 |
+
size = (width, height)
|
| 46 |
+
movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(images, fps)
|
| 47 |
+
movie_clip.write_videofile(f'{uid}-rembg/bg_removed-{uid}.mp4')
|
| 48 |
+
return (f'{uid}-rembg/bg_removed-{uid}.mp4', f'{uid}-rembg/bg_removed-{uid}.mp4')
|
| 49 |
+
def predict(text, url_params):
|
| 50 |
+
mod_url=""
|
| 51 |
+
mod=gr.HTML("")
|
| 52 |
+
out = None
|
| 53 |
+
valid=gr.update(visible=False)
|
| 54 |
+
mod_url = url_params.get('url')
|
| 55 |
+
return ["" + text + "", mod_url]
|
| 56 |
+
def dl(inp,img):
|
| 57 |
+
fps="Error"
|
| 58 |
+
out = None
|
| 59 |
+
out_file=[]
|
| 60 |
+
if img == None and inp !="":
|
| 61 |
+
try:
|
| 62 |
+
inp_out=inp.replace("https://","")
|
| 63 |
+
inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_")
|
| 64 |
+
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 65 |
+
out = f"{uid}/{inp_out}.mp4"
|
| 66 |
+
capture = cv2.VideoCapture(out)
|
| 67 |
+
fps = capture.get(cv2.CAP_PROP_FPS)
|
| 68 |
+
capture.release()
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(e)
|
| 71 |
+
out = None
|
| 72 |
+
elif img !=None and inp == "":
|
| 73 |
+
capture = cv2.VideoCapture(img)
|
| 74 |
+
fps = capture.get(cv2.CAP_PROP_FPS)
|
| 75 |
+
capture.release()
|
| 76 |
+
out = f"{img}"
|
| 77 |
+
return out,out,out,out,fps
|
| 78 |
+
def trim_vid(vid,start_time,end_time):
|
| 79 |
+
start_hr=float(start_time.split(":",2)[0])*360
|
| 80 |
+
start_min=int(start_time.split(":",2)[1])*60
|
| 81 |
+
start_sec=int(start_time.split(":",2)[2])
|
| 82 |
+
end_hr=int(end_time.split(":",2)[0])*360
|
| 83 |
+
end_min=int(end_time.split(":",2)[1])*60
|
| 84 |
+
end_sec=float(end_time.split(":",2)[2])
|
| 85 |
+
start=start_hr+start_min+start_sec
|
| 86 |
+
end=end_hr+end_min+end_sec
|
| 87 |
+
ffmpeg_extract_subclip(vid, start, end, targetname=f"{uid}-clip.mp4")
|
| 88 |
+
out= f"{uid}-clip.mp4"
|
| 89 |
+
capture = cv2.VideoCapture(out)
|
| 90 |
+
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 91 |
+
capture.release()
|
| 92 |
+
return out,frame_count
|
| 93 |
+
|
| 94 |
+
def gif_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscale):
|
| 95 |
+
_=None
|
| 96 |
+
fps=float(fps)
|
| 97 |
+
new_video_in = str(f"{uid}-clip.mp4")
|
| 98 |
+
capture = cv2.VideoCapture(new_video_in)
|
| 99 |
+
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 100 |
+
fbox=[]
|
| 101 |
+
out_box=[]
|
| 102 |
+
|
| 103 |
+
cnt=0
|
| 104 |
+
frame_count1= int(frame_count)
|
| 105 |
+
for i in range(int(frame_count1)-1):
|
| 106 |
+
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 107 |
+
ret, frame_f = capture.read(i)
|
| 108 |
+
#frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
|
| 109 |
+
cv2.imwrite(f'{uid}-frames/{i+1}.png',frame_f)
|
| 110 |
+
fbox.append(f'{uid}-frames/{i+1}.png')
|
| 111 |
+
mes=f'Working on {i+1} of {frame_count1}'
|
| 112 |
+
yield _,_,mes
|
| 113 |
+
yield _,_,"Frames Complete"
|
| 114 |
+
if qual:
|
| 115 |
+
mes = "Improving Quality"
|
| 116 |
+
yield _,_,mes
|
| 117 |
+
if qual:
|
| 118 |
+
for i,img in enumerate(fbox):
|
| 119 |
+
#print (img)
|
| 120 |
+
out = os.path.abspath(img)
|
| 121 |
+
out_url = f'https://omnibus-Vid-2-GIF-ERSGAN.hf.space/file={out}'
|
| 122 |
+
mes=f'Improving frame {i+1} of {frame_count}'
|
| 123 |
+
yield _,_,mes
|
| 124 |
+
|
| 125 |
+
out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
|
| 126 |
+
#yield _,_,mes
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
#cv2.imwrite(f'{img}',out)
|
| 131 |
+
out_box.append(f'{out}')
|
| 132 |
+
|
| 133 |
+
print (f'https://omnibus-Vid-2-GIF-ERSGAN.hf.space/file={out}')
|
| 134 |
+
#print (f'out::{out}')
|
| 135 |
+
print (f'out_box::{out_box}')
|
| 136 |
+
clip = ImageSequenceClip(out_box, fps = fps)
|
| 137 |
+
else:
|
| 138 |
+
mes="Writing GIF"
|
| 139 |
+
clip = ImageSequenceClip(fbox, fps = fps)
|
| 140 |
+
yield _,_,"Writing GIF"
|
| 141 |
+
clip.speedx(2).to_gif(f"{uid}/clip_gif.gif",program=program)
|
| 142 |
+
yield _,_,"Saving GIF"
|
| 143 |
+
out = f"{uid}/clip_gif.gif"
|
| 144 |
+
yield out,out,"GIF Complete"
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def update_speed(inp,clip_speed,fps):
|
| 148 |
+
if "-split.mp4" in inp:
|
| 149 |
+
out_inp = f'{inp.split("-split.mp4",1)[0]}-split.mp4'
|
| 150 |
+
else:
|
| 151 |
+
out_inp = f'{inp.split(".mp4",1)[0]}-split.mp4'
|
| 152 |
+
mod_fps=float(fps)*float(clip_speed)
|
| 153 |
+
clip = VideoFileClip(inp)
|
| 154 |
+
final = moviepy.video.fx.all.speedx(clip, factor=clip_speed)
|
| 155 |
+
final.write_videofile(f'{out_inp}', fps=mod_fps)
|
| 156 |
+
out = f'{out_inp}'
|
| 157 |
+
return out,out,out
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
css='''
|
| 165 |
+
#component-0{
|
| 166 |
+
background:repeating-radial-gradient(white, cornflowerblue);
|
| 167 |
+
}
|
| 168 |
+
.padded.svelte-90oupt{
|
| 169 |
+
background:cornflowerblue;
|
| 170 |
+
}
|
| 171 |
+
.dark .gr-box{
|
| 172 |
+
background:#344d74;!important;
|
| 173 |
+
opacity:1;!important;
|
| 174 |
+
}
|
| 175 |
+
.p-2 {
|
| 176 |
+
background:#344d74;!important;
|
| 177 |
+
opacity:1;!important;
|
| 178 |
+
}
|
| 179 |
+
.gap-4 {
|
| 180 |
+
background:#6681ab;!important;
|
| 181 |
+
opacity:1;!important;
|
| 182 |
+
}
|
| 183 |
+
.dark .gr-padded{
|
| 184 |
+
background:#21314a;!important;
|
| 185 |
+
opacity:1;!important;
|
| 186 |
+
}
|
| 187 |
+
'''
|
| 188 |
+
with gr.Blocks(css=css) as app:
|
| 189 |
+
|
| 190 |
+
with gr.Row():
|
| 191 |
+
gr.Column()
|
| 192 |
+
with gr.Column():
|
| 193 |
+
with gr.Box():
|
| 194 |
+
with gr.Group():
|
| 195 |
+
inp_url = gr.Textbox(label="Video URL")
|
| 196 |
+
go_btn = gr.Button("Run")
|
| 197 |
+
outp_vid=gr.Video(format="mp4")
|
| 198 |
+
with gr.Row():
|
| 199 |
+
frame_count=gr.Textbox(label="Frame Count",interactive = False)
|
| 200 |
+
fps=gr.Textbox(label="FPS",interactive = False)
|
| 201 |
+
outp_file=gr.Files()
|
| 202 |
+
clip_speed = gr.Slider(label="Speed", minimum=0.01, maximum=2, value=1, step=0.01)
|
| 203 |
+
speed_btn = gr.Button("Update Speed")
|
| 204 |
+
with gr.Row():
|
| 205 |
+
start_f = gr.Textbox(label = "Start", value = "0:00:00", placeholder = "0:00:23",interactive = True)
|
| 206 |
+
end_f = gr.Textbox(label = "End", value = "0:00:05", placeholder = "0:00:54",interactive = True)
|
| 207 |
+
trim_count = gr.Textbox(label="Trimmed Frames")
|
| 208 |
+
trim_btn=gr.Button("Trim")
|
| 209 |
+
out_trim=gr.Video(format="mp4")
|
| 210 |
+
hid_box = gr.Textbox(visible=True)
|
| 211 |
+
hid_fps = gr.Textbox(visible=True)
|
| 212 |
+
with gr.Row():
|
| 213 |
+
choose_prog=gr.Radio(label="GIF Engine",choices=['imageio','ImageMagick','ffmpeg'], value = "imageio")
|
| 214 |
+
with gr.Row():
|
| 215 |
+
qual=gr.Checkbox(label="Improve Quality (slow)",value=False)
|
| 216 |
+
with gr.Accordion("Quality Options", open=False):
|
| 217 |
+
with gr.Row():
|
| 218 |
+
model_name = gr.Dropdown(label="Real-ESRGAN Model",
|
| 219 |
+
choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B",
|
| 220 |
+
"RealESRGAN_x2plus", "realesr-general-x4v3"],
|
| 221 |
+
value="realesr-general-x4v3", show_label=True)
|
| 222 |
+
face_enhance = gr.Checkbox(label="Face Enhancement using GFPGAN",
|
| 223 |
+
value=False, show_label=True)
|
| 224 |
+
denoise_strength = gr.Slider(label="Denoise Strength",
|
| 225 |
+
minimum=0, maximum=1, step=0.1, value=0.5)
|
| 226 |
+
outscale = gr.Slider(label="Image Upscaling Factor",
|
| 227 |
+
minimum=1, maximum=10, step=1, value=1, show_label=True)
|
| 228 |
+
with gr.Accordion("Sample",open=False):
|
| 229 |
+
with gr.Row():
|
| 230 |
+
frame_targ=gr.Number(label="Frame",value=1)
|
| 231 |
+
samp_btn=gr.Button("Get Sample")
|
| 232 |
+
samp_im = gr.Image(label="Sample Frame")
|
| 233 |
+
|
| 234 |
+
with gr.Row():
|
| 235 |
+
gif_btn = gr.Button("Make GIF")
|
| 236 |
+
gif_stat=gr.Textbox(label="Status")
|
| 237 |
+
with gr.Row():
|
| 238 |
+
with gr.Column():
|
| 239 |
+
gif_show = gr.Video()
|
| 240 |
+
with gr.Column():
|
| 241 |
+
gif_file = gr.Files()
|
| 242 |
+
gr.Column()
|
| 243 |
+
with gr.Row(visible=False):
|
| 244 |
+
text_input=gr.Textbox()
|
| 245 |
+
text_output=gr.Textbox()
|
| 246 |
+
url_params=gr.JSON()
|
| 247 |
+
|
| 248 |
+
def echo_fn(inp):
|
| 249 |
+
return inp
|
| 250 |
+
|
| 251 |
+
def check_load(inp_url,outp_vid,hid_box,start_f,end_f):
|
| 252 |
+
if outp_vid == None and inp_url !="":
|
| 253 |
+
out_trim,in_vid,trim_count=trim_vid(hid_box,start_f,end_f)
|
| 254 |
+
elif outp_vid !=None and inp_url == "":
|
| 255 |
+
out_trim = None
|
| 256 |
+
in_vid=outp_vid
|
| 257 |
+
trim_count = ""
|
| 258 |
+
return out_trim,in_vid,trim_count
|
| 259 |
+
def run_samp(program,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ):
|
| 260 |
+
_=None
|
| 261 |
+
#fps=float(fps)
|
| 262 |
+
new_video_in = str(f"{uid}-clip.mp4")
|
| 263 |
+
capture = cv2.VideoCapture(new_video_in)
|
| 264 |
+
#frame_count1= int(frame_count)
|
| 265 |
+
i=int(frame_targ)
|
| 266 |
+
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 267 |
+
ret, frame_f = capture.read(i)
|
| 268 |
+
#frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
|
| 269 |
+
cv2.imwrite(f'{uid}-frames/{i}.png',frame_f)
|
| 270 |
+
out = os.path.abspath(f'{uid}-frames/{i}.png')
|
| 271 |
+
out_url = f'https://omnibus-Vid-2-GIF-ERSGAN.hf.space/file={out}'
|
| 272 |
+
yield _,"Creating Sample Frame"
|
| 273 |
+
out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
|
| 274 |
+
yield out,"Sample Frame Complete"
|
| 275 |
+
samp_btn.click(run_samp,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ],[samp_im,gif_stat])
|
| 276 |
+
speed_btn.click(update_speed,[hid_box,clip_speed,hid_fps],[outp_vid,hid_box,outp_file])
|
| 277 |
+
gif_btn.click(gif_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,gif_stat])
|
| 278 |
+
trim_btn.click(trim_vid,[hid_box,start_f,end_f],[out_trim,trim_count])
|
| 279 |
+
outp_vid.change(load_video,outp_vid,[frame_count,fps,end_f,hid_box]).then(trim_vid,[hid_box,start_f,end_f],[out_trim,trim_count])
|
| 280 |
+
go_btn.click(dl,[inp_url,outp_vid],[outp_vid,outp_file,out_trim,hid_box,hid_fps])
|
| 281 |
+
app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
|
| 282 |
+
app.launch(show_api=False)
|