File size: 1,629 Bytes
f9f8c19
bbce39c
 
20bee2f
 
bbce39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20bee2f
bbce39c
 
20bee2f
bbce39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
pip install opencv-python
import cv2
import os
import gradio as gr

def extract_clip(video_path, clip_start, clip_duration):
    video = cv2.VideoCapture(video_path)
    
    success, frame = video.read()
    count = 0
    frames = []
    while success:
        current_time = video.get(cv2.CAP_PROP_POS_MSEC) / 1000
        
        # Seek to the desired starting point
        if current_time < clip_start:
            video.set(cv2.CAP_PROP_POS_MSEC, clip_start * 1000)
            success, frame = video.read()
            continue
        
        # Check duration after reaching the clip_start timestamp
        elif current_time - clip_start <= clip_duration:
            frames.append(frame)
            
            success, frame = video.read()
            count += 1

        else:
            break

    video.release()
    return frames

def display_frames(frames):
    num_cols = 4  # Number of columns in the image grid
    num_rows = (len(frames) + num_cols - 1) // num_cols
    grid_image = cv2.hconcat([cv2.hconcat(frames[i:i+num_cols]) for i in range(0, len(frames), num_cols)])
    return grid_image

# Create Gradio interface
video_input = gr.inputs.Video(type="file")
start_input = gr.inputs.Number(default=5, label="Start Time (seconds)")
duration_input = gr.inputs.Number(default=3, label="Duration (seconds)")
image_output = gr.outputs.Image(type="pil")

gr.Interface(fn=lambda video, start, duration: display_frames(extract_clip(video, start, duration)),
             inputs=[video_input, start_input, duration_input],
             outputs=image_output,
             title="Extract Video Clip").launch()