|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
|
|
from scenedetect import open_video, SceneManager |
|
from scenedetect.detectors import ContentDetector |
|
|
|
|
|
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip |
|
|
|
outputs = ["json", "file", "gallery"] |
|
data_outputs = [] |
|
|
|
|
|
def fn(list): |
|
return tuple(list); |
|
|
|
|
|
|
|
def find_scenes(video_path, threshold=27.0): |
|
|
|
video = open_video(video_path) |
|
scene_manager = SceneManager() |
|
scene_manager.add_detector( |
|
ContentDetector(threshold=threshold)) |
|
scene_manager.detect_scenes(video, show_progress=True) |
|
scene_list = scene_manager.get_scene_list() |
|
|
|
|
|
data_outputs.append(scene_list) |
|
|
|
|
|
shots = [] |
|
stills = [] |
|
|
|
for i, scene in enumerate(scene_list): |
|
shot_in = scene[0].get_frames() / scene[0].get_framerate() |
|
shot_out = (scene[1].get_frames() - 1) / scene[0].get_framerate() |
|
target_name = str(i)+"_cut.mp4" |
|
ffmpeg_extract_subclip(video_path, shot_in, shot_out, targetname=target_name) |
|
data_outputs.append(target_name) |
|
shots.append(target_name) |
|
|
|
video = cv2.VideoCapture(video_path) |
|
|
|
fps = video.get(cv2.CAP_PROP_FPS) |
|
print('frames per second =',fps) |
|
|
|
frame_id = scene[0].get_frames() |
|
|
|
video.set(cv2.CAP_PROP_POS_FRAMES, frame_id) |
|
ret, frame = video.read() |
|
|
|
|
|
|
|
img = str(frame_id) + '_screenshot.png' |
|
cv2.imwrite(img,frame) |
|
stills.append(img) |
|
|
|
|
|
|
|
|
|
|
|
results = fn(data_outputs) |
|
print(results) |
|
print(stills) |
|
|
|
|
|
return scene_list, shots, stills |
|
|
|
video_input=gr.Video(source="upload", format="mp4"); |
|
|
|
gr.Interface(fn=find_scenes, inputs=video_input, outputs=outputs).launch() |