File size: 3,616 Bytes
4c59373
 
ddce0c5
4c59373
66e897d
8750e2b
66e897d
8750e2b
ddce0c5
 
 
7d035a6
ddce0c5
 
 
 
 
8750e2b
4c59373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
import os
import sys

# Ensure moviepy is properly imported
try:
    sys.path.append("/usr/local/lib/python3.10/site-packages")
    import moviepy.editor as mp
except ModuleNotFoundError:
    print("moviepy νŒ¨ν‚€μ§€κ°€ μ„€μΉ˜λ˜μ–΄ μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ„€μΉ˜λ₯Ό 진행 μ€‘μž…λ‹ˆλ‹€...")
    os.system(f"{sys.executable} -m pip install moviepy")
    sys.path.append("/usr/local/lib/python3.10/site-packages")
    try:
        import moviepy.editor as mp
    except ModuleNotFoundError:
        print("moviepy λͺ¨λ“ˆμ„ λ‘œλ“œν•  수 μ—†μŠ΅λ‹ˆλ‹€. μ„€μΉ˜ 확인 ν›„ λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
        sys.exit(1)

def process_video(video, start_time, end_time, resolution, fps, speed, loop):
    try:
        # Load video
        clip = mp.VideoFileClip(video.name)

        # Get video info
        duration = clip.duration
        width, height = clip.size
        info = {
            "duration": duration,
            "resolution": f"{width}x{height}"
        }

        # Cut clip based on input times
        start_time = float(start_time) if start_time else 0
        end_time = float(end_time) if end_time else duration
        clip = clip.subclip(start_time, end_time)

        # Create thumbnails
        start_thumbnail = clip.get_frame(start_time) if start_time < duration else None
        end_thumbnail = clip.get_frame(end_time) if end_time <= duration else None

        # Adjust resolution, fps, speed, and loop count
        if resolution:
            width, height = map(int, resolution.split('x'))
            clip = clip.resize((width, height))

        if fps:
            clip = clip.set_fps(int(fps))

        if speed:
            clip = clip.fx(mp.vfx.speedx, float(speed))

        gif_path = f"output.gif"
        clip.write_gif(gif_path, loop=int(loop))

        return start_thumbnail, end_thumbnail, gif_path, info
    except Exception as e:
        return None, None, None, {"error": str(e)}

def main():
    with gr.Blocks() as app:
        gr.Markdown("## λ™μ˜μƒμ„ GIF둜 λ³€ν™˜ν•˜λŠ” 도ꡬ")

        with gr.Row():
            video_input = gr.Video(label="λ™μ˜μƒ μ—…λ‘œλ“œ")
            video_info = gr.JSON(label="μ˜μƒ 정보")

        with gr.Row():
            start_time = gr.Textbox(label="μ‹œμž‘ μ‹œκ°„ (초)", placeholder="예: 0")
            end_time = gr.Textbox(label="끝 μ‹œκ°„ (초)", placeholder="예: 10")

        with gr.Row():
            resolution = gr.Textbox(label="해상도 (예: 640x480)", placeholder="예: 640x480")
            fps = gr.Slider(label="ν”„λ ˆμž„ 속도 (FPS)", minimum=1, maximum=60, step=1, value=24)
            speed = gr.Slider(label="μž¬μƒ 속도 (배속)", minimum=0.1, maximum=3.0, step=0.1, value=1.0)
            loop = gr.Slider(label="GIF 반볡 횟수", minimum=0, maximum=10, step=1, value=0)

        with gr.Row():
            start_thumbnail = gr.Image(label="μ‹œμž‘ 썸넀일")
            end_thumbnail = gr.Image(label="끝 썸넀일")

        gif_preview = gr.Image(label="GIF 미리보기")
        gif_download = gr.File(label="GIF λ‹€μš΄λ‘œλ“œ")

        generate_button = gr.Button("GIF 생성")

        def generate(video, start, end, res, frame_rate, playback_speed, repeat):
            return process_video(video, start, end, res, frame_rate, playback_speed, repeat)

        generate_button.click(
            generate,
            inputs=[video_input, start_time, end_time, resolution, fps, speed, loop],
            outputs=[start_thumbnail, end_thumbnail, gif_preview, gif_download]
        )

    return app

if __name__ == "__main__":
    app = main()
    app.launch()