File size: 1,722 Bytes
edcc531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c65d7e
 
 
 
 
edcc531
 
 
 
 
 
 
 
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
import gradio as gr
import cv2
from tqdm import tqdm
import os
import tempfile


def resize(movie, x, y, original_x, original_y, progress=gr.Progress(track_tqdm=True)):
   mp4_path = os.path.splitext(os.path.basename(movie))[0]
   cap = cv2.VideoCapture(movie)
   width, height = cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # 動画のサイズを取得

   if original_x == 0:
      original_x = width
      original_y = height
   # リサイズを動画のサイズに合わせる
   target_w = round(width / (original_x / x))
   target_h = round(height / (original_y / y))
   fps = cap.get(cv2.CAP_PROP_FPS)
   fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
   with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as temp_file:
      temp_file_path = f"{mp4_path}_{x}_{y}_resize.mp4"
      writer = cv2.VideoWriter(temp_file_path, fourcc, fps, (target_w, target_h))
      for i in tqdm(range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))):
         ret, frame = cap.read()
         if not ret:
            break
         frame = cv2.resize(frame, (target_w, target_h))
         writer.write(frame)
   return temp_file_path


# Create the Gradio interface
iface = gr.Interface(
    fn=resize,
    inputs=[
        gr.Video(type="mp4", label="Input movie"),
        gr.Slider(minimum=0, maximum=3840, step=10, default=1920, label="X"),
        gr.Slider(minimum=0, maximum=2160, step=10, default=1080, label="Y"),
        gr.Number(default=0, label="X_original"),
        gr.Number(default=0, label="Y_original")
    ],
    outputs=gr.Video(label="Result"),
    title="mp4_resize",
    description="動画をリサイズします"
)

if __name__ == "__main__":
   iface.queue().launch()