File size: 1,503 Bytes
9ff211c
 
 
 
 
 
 
 
 
 
bdb472e
9ff211c
 
 
 
 
bdb472e
 
 
 
 
 
9ff211c
bdb472e
9ff211c
 
 
 
 
bdb472e
 
 
 
9ff211c
 
 
 
 
 
 
 
 
 
 
 
bdb472e
9ff211c
bdb472e
 
 
9ff211c
 
 
 
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
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 [shin0624]

import gradio as gr
import cv2
import tempfile
import zipfile
from pathlib import Path

def extract_frames_per_second(video_file):
    temp_dir = tempfile.mkdtemp()
    frames_dir = Path(temp_dir) / "frames"
    frames_dir.mkdir(exist_ok=True)

    cap = cv2.VideoCapture(video_file)
    fps = cap.get(cv2.CAP_PROP_FPS)
    if fps == 0:
        return "FPSλ₯Ό κ°€μ Έμ˜¬ 수 μ—†μŠ΅λ‹ˆλ‹€."

    frame_interval = int(fps)  # 1μ΄ˆλ‹Ή 1μž₯

    frame_count = 0
    saved_count = 0

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        if frame_count % frame_interval == 0:
            frame_path = frames_dir / f"frame_{saved_count:05d}.png"
            cv2.imwrite(str(frame_path), frame)
            saved_count += 1
        frame_count += 1

    cap.release()

    zip_path = Path(temp_dir) / "frames.zip"
    with zipfile.ZipFile(zip_path, 'w') as zipf:
        for frame_file in frames_dir.glob("*.png"):
            zipf.write(frame_file, arcname=frame_file.name)

    return str(zip_path)

demo = gr.Interface(
    fn=extract_frames_per_second,
    inputs=gr.Video(label="λ™μ˜μƒ μ—…λ‘œλ“œ"),
    outputs=gr.File(label="1초 간격 ν”„λ ˆμž„ ZIP"),
    title="1초 간격 ν”„λ ˆμž„ μΆ”μΆœκΈ°",
    description="λ™μ˜μƒμ—μ„œ 1μ΄ˆλ§ˆλ‹€ 1μž₯의 ν”„λ ˆμž„μ„ PNG둜 μΆ”μΆœν•˜μ—¬ ZIP 파일둜 μ œκ³΅ν•©λ‹ˆλ‹€."
)

if __name__ == "__main__":
    demo.launch()