ryo2 commited on
Commit
edcc531
·
1 Parent(s): 84c5f65

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +49 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from tqdm import tqdm
4
+ import os
5
+ import tempfile
6
+
7
+
8
+ def resize(movie, x, y, original_x, original_y, progress=gr.Progress(track_tqdm=True)):
9
+ mp4_path = os.path.splitext(os.path.basename(movie))[0]
10
+ cap = cv2.VideoCapture(movie)
11
+ width, height = cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # 動画のサイズを取得
12
+
13
+ if original_x == 0:
14
+ original_x = width
15
+ original_y = height
16
+ # リサイズを動画のサイズに合わせる
17
+ target_w = round(width / (original_x / x))
18
+ target_h = round(height / (original_y / y))
19
+ fps = cap.get(cv2.CAP_PROP_FPS)
20
+ fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
21
+ with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as temp_file:
22
+ temp_file_path = f"{mp4_path}_{x}_{y}_resize.mp4"
23
+ writer = cv2.VideoWriter(temp_file_path, fourcc, fps, (target_w, target_h))
24
+ for i in tqdm(range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))):
25
+ ret, frame = cap.read()
26
+ if not ret:
27
+ break
28
+ frame = cv2.resize(frame, (target_w, target_h))
29
+ writer.write(frame)
30
+ return temp_file_path
31
+
32
+
33
+ # Create the Gradio interface
34
+ iface = gr.Interface(
35
+ fn=resize,
36
+ inputs=[
37
+ gr.Video(type="mp4", label="Input movie"),
38
+ gr.inputs.Slider(minimum=0, maximum=3840, step=10, default=1920, label="X"),
39
+ gr.inputs.Slider(minimum=0, maximum=2160, step=10, default=1080, label="Y"),
40
+ gr.inputs.Number(default=0, label="Xの元の解像度"),
41
+ gr.inputs.Number(default=0, label="Yの元の解像度")
42
+ ],
43
+ outputs=gr.Video(label="Result"),
44
+ title="mp4_resize",
45
+ description="動画をリサイズします"
46
+ )
47
+
48
+ if __name__ == "__main__":
49
+ iface.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ opencv-python
2
+ tqdm