kijeoung commited on
Commit
3def092
Β·
verified Β·
1 Parent(s): 66e897d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -97
app.py CHANGED
@@ -1,99 +1,112 @@
1
  import gradio as gr
 
2
  import os
3
- import sys
4
-
5
- # Ensure moviepy is properly imported
6
- try:
7
- sys.path.append("/usr/local/lib/python3.10/site-packages")
8
- import moviepy.editor as mp
9
- except ModuleNotFoundError:
10
- print("moviepy νŒ¨ν‚€μ§€κ°€ μ„€μΉ˜λ˜μ–΄ μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ„€μΉ˜λ₯Ό 진행 μ€‘μž…λ‹ˆλ‹€...")
11
- os.system(f"{sys.executable} -m pip install moviepy")
12
- sys.path.append("/usr/local/lib/python3.10/site-packages")
13
- try:
14
- import moviepy.editor as mp
15
- except ModuleNotFoundError:
16
- print("moviepy λͺ¨λ“ˆμ„ λ‘œλ“œν•  수 μ—†μŠ΅λ‹ˆλ‹€. μ„€μΉ˜ 확인 ν›„ λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
17
- sys.exit(1)
18
-
19
- def process_video(video, start_time, end_time, resolution, fps, speed, loop):
20
- try:
21
- # Load video
22
- clip = mp.VideoFileClip(video.name)
23
-
24
- # Get video info
25
- duration = clip.duration
26
- width, height = clip.size
27
- info = {
28
- "duration": duration,
29
- "resolution": f"{width}x{height}"
30
- }
31
-
32
- # Cut clip based on input times
33
- start_time = float(start_time) if start_time else 0
34
- end_time = float(end_time) if end_time else duration
35
- clip = clip.subclip(start_time, end_time)
36
-
37
- # Create thumbnails
38
- start_thumbnail = clip.get_frame(start_time) if start_time < duration else None
39
- end_thumbnail = clip.get_frame(end_time) if end_time <= duration else None
40
-
41
- # Adjust resolution, fps, speed, and loop count
42
- if resolution:
43
- width, height = map(int, resolution.split('x'))
44
- clip = clip.resize((width, height))
45
-
46
- if fps:
47
- clip = clip.set_fps(int(fps))
48
-
49
- if speed:
50
- clip = clip.fx(mp.vfx.speedx, float(speed))
51
-
52
- gif_path = f"output.gif"
53
- clip.write_gif(gif_path, loop=int(loop))
54
-
55
- return start_thumbnail, end_thumbnail, gif_path, info
56
- except Exception as e:
57
- return None, None, None, {"error": str(e)}
58
-
59
- def main():
60
- with gr.Blocks() as app:
61
- gr.Markdown("## λ™μ˜μƒμ„ GIF둜 λ³€ν™˜ν•˜λŠ” 도ꡬ")
62
-
63
- with gr.Row():
64
- video_input = gr.Video(label="λ™μ˜μƒ μ—…λ‘œλ“œ")
65
- video_info = gr.JSON(label="μ˜μƒ 정보")
66
-
67
- with gr.Row():
68
- start_time = gr.Textbox(label="μ‹œμž‘ μ‹œκ°„ (초)", placeholder="예: 0")
69
- end_time = gr.Textbox(label="끝 μ‹œκ°„ (초)", placeholder="예: 10")
70
-
71
- with gr.Row():
72
- resolution = gr.Textbox(label="해상도 (예: 640x480)", placeholder="예: 640x480")
73
- fps = gr.Slider(label="ν”„λ ˆμž„ 속도 (FPS)", minimum=1, maximum=60, step=1, value=24)
74
- speed = gr.Slider(label="μž¬μƒ 속도 (배속)", minimum=0.1, maximum=3.0, step=0.1, value=1.0)
75
- loop = gr.Slider(label="GIF 반볡 횟수", minimum=0, maximum=10, step=1, value=0)
76
-
77
- with gr.Row():
78
- start_thumbnail = gr.Image(label="μ‹œμž‘ 썸넀일")
79
- end_thumbnail = gr.Image(label="끝 썸넀일")
80
-
81
- gif_preview = gr.Image(label="GIF 미리보기")
82
- gif_download = gr.File(label="GIF λ‹€μš΄λ‘œλ“œ")
83
-
84
- generate_button = gr.Button("GIF 생성")
85
-
86
- def generate(video, start, end, res, frame_rate, playback_speed, repeat):
87
- return process_video(video, start, end, res, frame_rate, playback_speed, repeat)
88
-
89
- generate_button.click(
90
- generate,
91
- inputs=[video_input, start_time, end_time, resolution, fps, speed, loop],
92
- outputs=[start_thumbnail, end_thumbnail, gif_preview, gif_download]
93
- )
94
-
95
- return app
96
-
97
- if __name__ == "__main__":
98
- app = main()
99
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from moviepy.editor import VideoFileClip
3
  import os
4
+ from PIL import Image
5
+ import tempfile
6
+ import logging
7
+
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+
11
+ # Function to get video info
12
+ def get_video_info(video):
13
+ logging.info("Getting video info")
14
+ if not video:
15
+ return "No video uploaded."
16
+ clip = VideoFileClip(video)
17
+ duration = clip.duration
18
+ clip.close()
19
+ return f"Duration: {duration:.2f} seconds"
20
+
21
+ # Function to generate thumbnails
22
+ def generate-thumbnails(video, start_time, end_time):
23
+ logging.info("Generating thumbnails")
24
+ if not video:
25
+ return "No video uploaded."
26
+ clip = VideoFileClip(video)
27
+ if start_time > clip.duration:
28
+ start_img = None
29
+ else:
30
+ start_frame = clip.get_frame(start_time)
31
+ start_img = Image.fromarray(start_frame)
32
+ if end_time > clip.duration:
33
+ end_img = None
34
+ else:
35
+ end_frame = clip.get_frame(end_time)
36
+ end_img = Image.fromarray(end_frame)
37
+ clip.close()
38
+ return start_img, end_img
39
+
40
+ # Function to generate GIF
41
+ def generate_gif(video, start_time, end_time, resolution, frame_rate, playback_speed, loop_count):
42
+ logging.info("Generating GIF")
43
+ if not video:
44
+ return None, None
45
+ clip = VideoFileClip(video)
46
+ # Apply start and end times
47
+ if start_time > clip.duration:
48
+ start_time = 0
49
+ if end_time > clip.duration:
50
+ end_time = clip.duration
51
+ subclip = clip.subclip(start_time, end_time)
52
+ # Apply playback speed
53
+ subclip = subclip.set_fps(subclip.fps * playback_speed)
54
+ # Resize if resolution is changed
55
+ if resolution != subclip.size[0]:
56
+ ratio = resolution / subclip.size[0]
57
+ new_height = int(subclip.size[1] * ratio)
58
+ subclip = subclip.resize((resolution, new_height))
59
+ # Write GIF
60
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as temp:
61
+ gif_path = temp.name
62
+ subclip.write_gif(gif_path, fps=frame_rate, loop=loop_count)
63
+ # Read GIF for preview
64
+ preview = Image.open(gif_path)
65
+ return preview, gif_path
66
+
67
+ # Gradio interface
68
+ with gr.Blocks() as demo:
69
+ gr.Markdown("<h1>Video to GIF Converter</h1>")
70
+
71
+ # Upload video
72
+ video_input = gr.Video(label="Upload Video")
73
+
74
+ # Button to get video info
75
+ with gr.Row():
76
+ get_info_button = gr.Button("Get Video Info")
77
+ video_info = gr.Textbox(label="Video Information")
78
+
79
+ # Display uploaded video
80
+ video_output = gr.Video(label="Uploaded Video")
81
+
82
+ # Inputs for start and end times
83
+ start_time = gr.Number(label="Start Time (seconds)", value=0)
84
+ end_time = gr.Number(label="End Time (seconds)", value=0)
85
+
86
+ # Thumbnails
87
+ with gr.Row():
88
+ start_thumbnail = gr.Image(label="Start Thumbnail")
89
+ end_thumbnail = gr.Image(label="End Thumbnail")
90
+
91
+ # Sliders
92
+ resolution = gr.Slider(100, 1920, step=100, value=1920, label="Resolution")
93
+ frame_rate = gr.Slider(1, 30, step=1, value=10, label="Frame Rate")
94
+ playback_speed = gr.Slider(0.1, 2.0, step=0.1, value=1.0, label="Playback Speed")
95
+ loop_count = gr.Slider(0, 10, step=1, value=0, label="GIF Loop Count (0 for infinite)")
96
+
97
+ # GIF Generation button
98
+ generate_button = gr.Button("GIF 생성")
99
+
100
+ # GIF Preview and Download
101
+ gif_preview = gr.Image(label="GIF Preview")
102
+ gif_download = gr.Download(label="Download GIF")
103
+
104
+ # Button functions
105
+ get_info_button.click(get_video_info, inputs=video_input, outputs=video_info)
106
+ video_input.change(lambda x: x, inputs=video_input, outputs=video_output)
107
+ video_input.change(generate-thumbnails, inputs=[video_input, start_time, end_time], outputs=[start_thumbnail, end_thumbnail])
108
+ generate_button.click(generate_gif,
109
+ inputs=[video_input, start_time, end_time, resolution, frame_rate, playback_speed, loop_count],
110
+ outputs=[gif_preview, gif_download])
111
+
112
+ demo.launch()