File size: 3,961 Bytes
4c59373
3def092
4c59373
3def092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
import gradio as gr
from moviepy.editor import VideoFileClip
import os
from PIL import Image
import tempfile
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)

# Function to get video info
def get_video_info(video):
    logging.info("Getting video info")
    if not video:
        return "No video uploaded."
    clip = VideoFileClip(video)
    duration = clip.duration
    clip.close()
    return f"Duration: {duration:.2f} seconds"

# Function to generate thumbnails
def generate-thumbnails(video, start_time, end_time):
    logging.info("Generating thumbnails")
    if not video:
        return "No video uploaded."
    clip = VideoFileClip(video)
    if start_time > clip.duration:
        start_img = None
    else:
        start_frame = clip.get_frame(start_time)
        start_img = Image.fromarray(start_frame)
    if end_time > clip.duration:
        end_img = None
    else:
        end_frame = clip.get_frame(end_time)
        end_img = Image.fromarray(end_frame)
    clip.close()
    return start_img, end_img

# Function to generate GIF
def generate_gif(video, start_time, end_time, resolution, frame_rate, playback_speed, loop_count):
    logging.info("Generating GIF")
    if not video:
        return None, None
    clip = VideoFileClip(video)
    # Apply start and end times
    if start_time > clip.duration:
        start_time = 0
    if end_time > clip.duration:
        end_time = clip.duration
    subclip = clip.subclip(start_time, end_time)
    # Apply playback speed
    subclip = subclip.set_fps(subclip.fps * playback_speed)
    # Resize if resolution is changed
    if resolution != subclip.size[0]:
        ratio = resolution / subclip.size[0]
        new_height = int(subclip.size[1] * ratio)
        subclip = subclip.resize((resolution, new_height))
    # Write GIF
    with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as temp:
        gif_path = temp.name
        subclip.write_gif(gif_path, fps=frame_rate, loop=loop_count)
    # Read GIF for preview
    preview = Image.open(gif_path)
    return preview, gif_path

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("<h1>Video to GIF Converter</h1>")
    
    # Upload video
    video_input = gr.Video(label="Upload Video")
    
    # Button to get video info
    with gr.Row():
        get_info_button = gr.Button("Get Video Info")
        video_info = gr.Textbox(label="Video Information")
    
    # Display uploaded video
    video_output = gr.Video(label="Uploaded Video")
    
    # Inputs for start and end times
    start_time = gr.Number(label="Start Time (seconds)", value=0)
    end_time = gr.Number(label="End Time (seconds)", value=0)
    
    # Thumbnails
    with gr.Row():
        start_thumbnail = gr.Image(label="Start Thumbnail")
        end_thumbnail = gr.Image(label="End Thumbnail")
    
    # Sliders
    resolution = gr.Slider(100, 1920, step=100, value=1920, label="Resolution")
    frame_rate = gr.Slider(1, 30, step=1, value=10, label="Frame Rate")
    playback_speed = gr.Slider(0.1, 2.0, step=0.1, value=1.0, label="Playback Speed")
    loop_count = gr.Slider(0, 10, step=1, value=0, label="GIF Loop Count (0 for infinite)")
    
    # GIF Generation button
    generate_button = gr.Button("GIF 생성")
    
    # GIF Preview and Download
    gif_preview = gr.Image(label="GIF Preview")
    gif_download = gr.Download(label="Download GIF")
    
    # Button functions
    get_info_button.click(get_video_info, inputs=video_input, outputs=video_info)
    video_input.change(lambda x: x, inputs=video_input, outputs=video_output)
    video_input.change(generate-thumbnails, inputs=[video_input, start_time, end_time], outputs=[start_thumbnail, end_thumbnail])
    generate_button.click(generate_gif, 
                         inputs=[video_input, start_time, end_time, resolution, frame_rate, playback_speed, loop_count], 
                         outputs=[gif_preview, gif_download])

demo.launch()