mp4_resize / app.py
ryo2's picture
Update app.py
9c65d7e
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()