File size: 11,503 Bytes
3bc3a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import gradio as gr 
import yt_dlp
import os 
#import json
import numpy as np 
import cv2
import uuid
import moviepy
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import moviepy.video.io.ImageSequenceClip
from moviepy.editor import *
from PIL import Image
uid=uuid.uuid4()
if not os.path.exists(f'{uid}'): os.makedirs(f'{uid}')
if not os.path.exists(f'{uid}-frames'): os.makedirs(f'{uid}-frames')
if not os.path.exists(f'{uid}-rembg'): os.makedirs(f'{uid}-rembg')
esr = gr.Interface.load("spaces/Omnibus/Real-ESRGAN-mod")

load_js = """
function(text_input, url_params) {
    console.log(text_input, url_params);
    const params = new URLSearchParams(window.location.search);
    url_params = Object.fromEntries(params);
    return [text_input, url_params]
}
"""
def load_video(vid):
    new_video_in = str(vid)
    capture = cv2.VideoCapture(new_video_in)
    fps = capture.get(cv2.CAP_PROP_FPS)
    msc = capture.get(cv2.CAP_PROP_POS_MSEC)
    frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    msc = float(frame_count/fps)
    capture.release()
    vid_t = round(msc)
    hours = int(vid_t/360)
    minutes = int(vid_t/60)-(hours*360)
    seconds = vid_t-(minutes*60)-(hours*360)
    vid_len = f'{hours}:{minutes}:{seconds}'
    return frame_count, fps, vid_len, vid
def im_2_vid(images,fps):
    this_im=cv2.imread(images[0])
    height=this_im.shape[0]
    width= this_im.shape[1]
    size = (width, height)
    movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(images, fps)
    movie_clip.write_videofile(f'{uid}-rembg/bg_removed-{uid}.mp4')    
    return (f'{uid}-rembg/bg_removed-{uid}.mp4', f'{uid}-rembg/bg_removed-{uid}.mp4')
def predict(text, url_params):
    mod_url=""
    mod=gr.HTML("")
    out = None
    valid=gr.update(visible=False)
    mod_url = url_params.get('url')
    return ["" + text + "", mod_url]
def dl(inp,img):
    fps="Error"
    out = None
    out_file=[]
    if img == None and inp !="":
        try:
            inp_out=inp.replace("https://","")
            inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_")
            os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')  
            out = f"{uid}/{inp_out}.mp4"
            capture = cv2.VideoCapture(out)
            fps = capture.get(cv2.CAP_PROP_FPS)
            capture.release()
        except Exception as e:
            print(e)
            out = None
    elif img !=None and inp == "":
        capture = cv2.VideoCapture(img)
        fps = capture.get(cv2.CAP_PROP_FPS)
        capture.release()
        out = f"{img}"
    return out,out,out,out,fps
def trim_vid(vid,start_time,end_time):
    start_hr=float(start_time.split(":",2)[0])*360
    start_min=int(start_time.split(":",2)[1])*60
    start_sec=int(start_time.split(":",2)[2])
    end_hr=int(end_time.split(":",2)[0])*360
    end_min=int(end_time.split(":",2)[1])*60
    end_sec=float(end_time.split(":",2)[2])
    start=start_hr+start_min+start_sec
    end=end_hr+end_min+end_sec
    ffmpeg_extract_subclip(vid, start, end, targetname=f"{uid}-clip.mp4")
    out= f"{uid}-clip.mp4"
    capture = cv2.VideoCapture(out)    
    frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))    
    capture.release()
    return out,frame_count

def gif_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscale):
    _=None    
    fps=float(fps)
    new_video_in = str(f"{uid}-clip.mp4")
    capture = cv2.VideoCapture(new_video_in)
    frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    fbox=[]
    out_box=[]

    cnt=0
    frame_count1= int(frame_count)
    for i in range(int(frame_count1)-1):
        capture.set(cv2.CAP_PROP_POS_FRAMES, i)
        ret, frame_f = capture.read(i)
        #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
        cv2.imwrite(f'{uid}-frames/{i+1}.png',frame_f)
        fbox.append(f'{uid}-frames/{i+1}.png')
        mes=f'Working on {i+1} of {frame_count1}'
        yield _,_,mes
    yield _,_,"Frames Complete"
    if qual:
        mes = "Improving Quality"
        yield _,_,mes
    if qual:
        for i,img in enumerate(fbox):
            #print (img)
            out = os.path.abspath(img)
            out_url = f'https://omnibus-Vid-2-GIF-ERSGAN.hf.space/file={out}'
            mes=f'Improving frame {i+1} of {frame_count}'
            yield _,_,mes
            
            out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
            #yield _,_,mes
            
            
            
            #cv2.imwrite(f'{img}',out)
            out_box.append(f'{out}')

            print (f'https://omnibus-Vid-2-GIF-ERSGAN.hf.space/file={out}')
            #print (f'out::{out}')
        print (f'out_box::{out_box}')
        clip = ImageSequenceClip(out_box, fps = fps) 
    else:
        mes="Writing GIF"
        clip = ImageSequenceClip(fbox, fps = fps) 
    yield _,_,"Writing GIF"    
    clip.speedx(2).to_gif(f"{uid}/clip_gif.gif",program=program)    
    yield _,_,"Saving GIF"    
    out = f"{uid}/clip_gif.gif"
    yield out,out,"GIF Complete"    


def update_speed(inp,clip_speed,fps):
    if "-split.mp4" in inp:
        out_inp = f'{inp.split("-split.mp4",1)[0]}-split.mp4'
    else:
        out_inp = f'{inp.split(".mp4",1)[0]}-split.mp4'
    mod_fps=float(fps)*float(clip_speed)
    clip = VideoFileClip(inp)
    final = moviepy.video.fx.all.speedx(clip, factor=clip_speed)    
    final.write_videofile(f'{out_inp}', fps=mod_fps) 
    out = f'{out_inp}'
    return out,out,out





    
css='''
#component-0{
background:repeating-radial-gradient(white, cornflowerblue);
}
.padded.svelte-90oupt{
background:cornflowerblue;
}
.dark .gr-box{
background:#344d74;!important;
opacity:1;!important;    
}
.p-2 {
background:#344d74;!important;
opacity:1;!important;
}
.gap-4 {
background:#6681ab;!important;
opacity:1;!important;
}
.dark .gr-padded{
background:#21314a;!important;
opacity:1;!important;
}
'''    
with gr.Blocks(css=css) as app:

    with gr.Row():
        gr.Column()
        with gr.Column():
            with gr.Box():
                with gr.Group():
                    inp_url = gr.Textbox(label="Video URL")
                    go_btn = gr.Button("Run")
                    outp_vid=gr.Video(format="mp4")
                    with gr.Row():
                        frame_count=gr.Textbox(label="Frame Count",interactive = False)
                        fps=gr.Textbox(label="FPS",interactive = False)
                    outp_file=gr.Files()
                    clip_speed = gr.Slider(label="Speed", minimum=0.01, maximum=2, value=1, step=0.01)
                    speed_btn = gr.Button("Update Speed")
                    with gr.Row():
                        start_f = gr.Textbox(label = "Start", value = "0:00:00", placeholder = "0:00:23",interactive = True)
                        end_f = gr.Textbox(label = "End", value = "0:00:05", placeholder = "0:00:54",interactive = True)
                        trim_count = gr.Textbox(label="Trimmed Frames")
                    trim_btn=gr.Button("Trim")
                    out_trim=gr.Video(format="mp4")
                    hid_box = gr.Textbox(visible=True)
                    hid_fps = gr.Textbox(visible=True)
                    with gr.Row():
                        choose_prog=gr.Radio(label="GIF Engine",choices=['imageio','ImageMagick','ffmpeg'], value = "imageio")
                    with gr.Row():
                        qual=gr.Checkbox(label="Improve Quality (slow)",value=False)
                    with gr.Accordion("Quality Options", open=False):
                        with gr.Row():
                            model_name = gr.Dropdown(label="Real-ESRGAN Model",
                                                     choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B",
                                                              "RealESRGAN_x2plus", "realesr-general-x4v3"],
                                                     value="realesr-general-x4v3", show_label=True)
                            face_enhance = gr.Checkbox(label="Face Enhancement using GFPGAN",
                                                       value=False, show_label=True)
                            denoise_strength = gr.Slider(label="Denoise Strength",
                                                         minimum=0, maximum=1, step=0.1, value=0.5)
                            outscale = gr.Slider(label="Image Upscaling Factor",
                                                 minimum=1, maximum=10, step=1, value=1, show_label=True)
                        with gr.Accordion("Sample",open=False):
                            with gr.Row():
                                frame_targ=gr.Number(label="Frame",value=1)
                                samp_btn=gr.Button("Get Sample")
                            samp_im = gr.Image(label="Sample Frame")    
                        
                    with gr.Row():
                        gif_btn = gr.Button("Make GIF")
                        gif_stat=gr.Textbox(label="Status")
                    with gr.Row():
                        with gr.Column():
                            gif_show = gr.Video()
                        with gr.Column():
                            gif_file = gr.Files()   
        gr.Column()
    with gr.Row(visible=False):
        text_input=gr.Textbox()
        text_output=gr.Textbox()
        url_params=gr.JSON()

    def echo_fn(inp):
        return inp 

    def check_load(inp_url,outp_vid,hid_box,start_f,end_f):
        if outp_vid == None and inp_url !="":
            out_trim,in_vid,trim_count=trim_vid(hid_box,start_f,end_f)
        elif outp_vid !=None and inp_url == "":
            out_trim = None
            in_vid=outp_vid
            trim_count = ""
        return out_trim,in_vid,trim_count   
    def run_samp(program,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ):
        _=None    
        #fps=float(fps)
        new_video_in = str(f"{uid}-clip.mp4")
        capture = cv2.VideoCapture(new_video_in)
        #frame_count1= int(frame_count)
        i=int(frame_targ)
        capture.set(cv2.CAP_PROP_POS_FRAMES, i)
        ret, frame_f = capture.read(i)
        #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
        cv2.imwrite(f'{uid}-frames/{i}.png',frame_f)
        out = os.path.abspath(f'{uid}-frames/{i}.png')
        out_url = f'https://omnibus-Vid-2-GIF-ERSGAN.hf.space/file={out}'
        yield _,"Creating Sample Frame"
        out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
        yield out,"Sample Frame Complete"        
    samp_btn.click(run_samp,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ],[samp_im,gif_stat])    
    speed_btn.click(update_speed,[hid_box,clip_speed,hid_fps],[outp_vid,hid_box,outp_file])
    gif_btn.click(gif_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,gif_stat])
    trim_btn.click(trim_vid,[hid_box,start_f,end_f],[out_trim,trim_count])
    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])
    go_btn.click(dl,[inp_url,outp_vid],[outp_vid,outp_file,out_trim,hid_box,hid_fps])
    app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
app.launch(show_api=False)