|
import cv2 |
|
import os |
|
import gradio as gr |
|
|
|
|
|
def increase_fps(input_video_path, target_fps=60): |
|
output_path = "output.mp4" |
|
cap = cv2.VideoCapture(input_video_path) |
|
input_fps = cap.get(cv2.CAP_PROP_FPS) |
|
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
codec = cv2.VideoWriter_fourcc(*"mp4v") |
|
|
|
|
|
if input_fps >= target_fps: |
|
cap.release() |
|
return input_video_path |
|
|
|
|
|
ratio = target_fps / input_fps |
|
output = cv2.VideoWriter(output_path, codec, target_fps, (width, height)) |
|
|
|
ret, prev_frame = cap.read() |
|
|
|
if not ret: |
|
cap.release() |
|
output.release() |
|
raise ValueError("Не удалось прочитать кадры из видео.") |
|
|
|
prev_frame_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) |
|
|
|
while True: |
|
ret, next_frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
next_frame_gray = cv2.cvtColor(next_frame, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
flow = cv2.calcOpticalFlowFarneback(prev_frame_gray, next_frame_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) |
|
|
|
for i in range(1, int(ratio)): |
|
alpha = i / ratio |
|
flow_intermediate = flow * alpha |
|
intermediate_frame = cv2.remap(prev_frame, flow_intermediate, None, cv2.INTER_LINEAR) |
|
output.write(intermediate_frame) |
|
|
|
output.write(next_frame) |
|
prev_frame_gray = next_frame_gray |
|
prev_frame = next_frame |
|
|
|
cap.release() |
|
output.release() |
|
|
|
return output_path |
|
|
|
|
|
def process_video(video_path): |
|
return increase_fps(video_path) |
|
|
|
interface = gr.Interface( |
|
fn=process_video, |
|
inputs=gr.Video(label="Загрузите видео для обработки"), |
|
outputs=gr.Video(label="Видео с увеличенным FPS"), |
|
title="Увеличение FPS до 60", |
|
description=( |
|
"Этот инструмент увеличивает частоту кадров видео до 60 FPS с использованием " |
|
"оптического потока. Загрузите видео с низким FPS, чтобы получить плавный результат." |
|
), |
|
examples=[] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |