Kims12 commited on
Commit
ddf3982
ยท
verified ยท
1 Parent(s): 4680eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -13
app.py CHANGED
@@ -41,10 +41,43 @@ def generate_thumbnail(video_clip, time_point):
41
  thumbnail_img = Image.fromarray(frame)
42
  return thumbnail_img
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def process_video(video,
45
  start_time_str,
46
  end_time_str,
47
- resolution_factor,
48
  frame_rate_factor,
49
  speed_factor,
50
  repeat_count):
@@ -87,12 +120,11 @@ def process_video(video,
87
  add_log("[LOG 6] ์˜์ƒ ์ž๋ฅด๊ธฐ ์ž‘์—… ์ง„ํ–‰...")
88
  clip = input_video.subclip(start_sec, end_sec)
89
 
90
- # ํ•ด์ƒ๋„ ์กฐ์ ˆ (ํŒŒ์ผ ํฌ๊ธฐ ์ ˆ๊ฐ์„ ์œ„ํ•ด)
91
- if abs(resolution_factor - 1.0) > 1e-3:
92
- add_log(f"[LOG 7] ํ•ด์ƒ๋„ {resolution_factor*100:.1f}%๋กœ ์กฐ์ ˆ ์ค‘...")
93
- clip = clip.resize(resolution_factor)
94
 
95
- # ์žฌ์ƒ์†๋„ ์กฐ์ ˆ: speedx๋ฅผ ์ ์šฉํ•˜๋ฉด clip.duration์ด ์ค„์–ด๋“ค์ง€๋งŒ fps๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€๋จ.
96
  if abs(speed_factor - 1.0) > 1e-3:
97
  add_log(f"[LOG 8] ์žฌ์ƒ์†๋„ {speed_factor}๋ฐฐ๋กœ ์กฐ์ ˆ ์ค‘...")
98
  clip = clip.fx(mp.vfx.speedx, speed_factor)
@@ -110,7 +142,6 @@ def process_video(video,
110
  add_log("[LOG 11] GIF ์ƒ์„ฑ ์ค‘...")
111
  output_filename = f"temp_{uuid.uuid4().hex}.gif"
112
  try:
113
- # loop=0์ด๋ฉด ๋ฌดํ•œ๋ฐ˜๋ณต, ๊ทธ ์™ธ์—๋Š” ํ•ด๋‹น ๊ฐ’ ์‚ฌ์šฉ
114
  loop_param = 0 if int(repeat_count) == 0 else int(repeat_count)
115
  final_clip.write_gif(output_filename, fps=target_fps, program='ffmpeg', loop=loop_param)
116
  add_log("[LOG 12] GIF ์ƒ์„ฑ ์™„๋ฃŒ! ํŒŒ์ผ๋ช…: " + output_filename)
@@ -165,9 +196,14 @@ with gr.Blocks() as demo:
165
  # ์ธ๋„ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
166
  start_thumb_output = gr.Image(label="์‹œ์ž‘ ์ธ๋„ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
167
  end_thumb_output = gr.Image(label="์ข…๋ฃŒ ์ธ๋„ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
168
- # ํ•ด์ƒ๋„, ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ, ์žฌ์ƒ ์†๋„, ๋ฐ˜๋ณต ํšŸ์ˆ˜ ์กฐ์ ˆ
169
- resolution_slider = gr.Slider(label="ํ•ด์ƒ๋„ ๋น„์œจ ์กฐ์ ˆ (0.1 ~ 1.0)", minimum=0.1, maximum=1.0, step=0.1, value=0.5)
170
- fps_slider = gr.Slider(label="ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ ๋ฐฐ์œจ ์กฐ์ ˆ (0.1 ~ 2.0)", minimum=0.1, maximum=2.0, step=0.1, value=1.0)
 
 
 
 
 
171
  speed_slider = gr.Slider(label="์žฌ์ƒ ์†๋„ ์กฐ์ ˆ (0.5 ~ 2.0)", minimum=0.5, maximum=2.0, step=0.1, value=1.0)
172
  repeat_slider = gr.Slider(label="GIF ๋ฐ˜๋ณต ํšŸ์ˆ˜ (0: ๋ฌดํ•œ๋ฐ˜๋ณต, 1~10: ๋ฐ˜๋ณต ํšŸ์ˆ˜)", minimum=0, maximum=10, step=1, value=0)
173
  # GIF ์ƒ์„ฑ ๋ฒ„ํŠผ ๋ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
@@ -190,14 +226,15 @@ with gr.Blocks() as demo:
190
  # GIF ์ƒ์„ฑ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
191
  generate_button.click(fn=process_video,
192
  inputs=[video_input, start_time, end_time,
193
- resolution_slider, fps_slider, speed_slider, repeat_slider],
194
  outputs=[gif_preview_output, download_output, logs_output])
195
 
196
  gr.Markdown(
197
  "### [์‚ฌ์šฉ ๊ฐ€์ด๋“œ]\n"
198
  "1. ๋™์˜์ƒ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”.\n"
199
- "2. ์‹œ์ž‘/๋ ์‹œ๊ฐ„์„ ์ ์ ˆํžˆ ์ž…๋ ฅํ•˜์„ธ์š”.\n"
200
- "3. ํ•ด์ƒ๋„, ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ ๋ฐฐ์œจ, ์žฌ์ƒ ์†๏ฟฝ๏ฟฝ, ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์กฐ์ ˆํ•œ ๋’ค `GIF ์ƒ์„ฑํ•˜๊ธฐ` ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด GIF๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.\n"
 
201
  " - ๋ฐ˜๋ณต ํšŸ์ˆ˜: 0์„ ์„ ํƒํ•˜๋ฉด ๋ฌดํ•œ๋ฐ˜๋ณต, 1~10์€ ํ•ด๋‹น ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.\n"
202
  "4. ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์™€ ๋‹ค์šด๋กœ๋“œ ๋งํฌ๋ฅผ ํ†ตํ•ด GIF๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."
203
  )
 
41
  thumbnail_img = Image.fromarray(frame)
42
  return thumbnail_img
43
 
44
+ def adjust_aspect_ratio(clip, option):
45
+ """
46
+ ์˜์ƒ ํด๋ฆฝ์„ ์„ ํƒํ•œ ํ”Œ๋žซํผ์˜ ๊ถŒ์žฅ ๋น„์œจ๋กœ ์ค‘์•™ ํฌ๋กญํ•˜์—ฌ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
47
+ """
48
+ if option == "์›๋ณธ ์œ ์ง€":
49
+ return clip
50
+
51
+ if option == "์œ ํŠœ๋ธŒ (16:9)":
52
+ target_ratio = 16 / 9
53
+ elif option == "์‡ผ์ธ /๋ฆด์Šค (9:16)":
54
+ target_ratio = 9 / 16
55
+ elif option == "์ •์‚ฌ๊ฐํ˜• (1:1)":
56
+ target_ratio = 1.0
57
+ elif option == "์ธ์Šคํƒ€๊ทธ๋žจ (4:5)":
58
+ target_ratio = 4 / 5
59
+ elif option == "ํด๋ž˜์‹ (4:3)":
60
+ target_ratio = 4 / 3
61
+ else:
62
+ return clip
63
+
64
+ width, height = clip.size
65
+ current_ratio = width / height
66
+
67
+ # ์˜์ƒ์ด ๋„ˆ๋ฌด ๋„“๋‹ค๋ฉด ๊ฐ€๋กœ๋ฅผ, ๋„ˆ๋ฌด ๋†’๋‹ค๋ฉด ์„ธ๋กœ๋ฅผ ํฌ๋กญํ•ฉ๋‹ˆ๋‹ค.
68
+ if current_ratio > target_ratio:
69
+ new_width = int(height * target_ratio)
70
+ new_height = height
71
+ else:
72
+ new_width = width
73
+ new_height = int(width / target_ratio)
74
+
75
+ return clip.crop(x_center=width / 2, y_center=height / 2, width=new_width, height=new_height)
76
+
77
  def process_video(video,
78
  start_time_str,
79
  end_time_str,
80
+ platform_option, # ํ•ด์ƒ๋„ ๋น„์œจ ์˜ต์…˜ ์„ ํƒ
81
  frame_rate_factor,
82
  speed_factor,
83
  repeat_count):
 
120
  add_log("[LOG 6] ์˜์ƒ ์ž๋ฅด๊ธฐ ์ž‘์—… ์ง„ํ–‰...")
121
  clip = input_video.subclip(start_sec, end_sec)
122
 
123
+ # ํ”Œ๋žซํผ๋ณ„ ํ•ด์ƒ๋„(๋น„์œจ) ์กฐ์ •
124
+ add_log(f"[LOG 7] ํ”Œ๋žซํผ ๋น„์œจ ์กฐ์ •: {platform_option}")
125
+ clip = adjust_aspect_ratio(clip, platform_option)
 
126
 
127
+ # ์žฌ์ƒ์†๋„ ์กฐ์ ˆ: speedx๋ฅผ ์ ์šฉํ•˜๋ฉด clip.duration์€ ์ค„์–ด๋“ค์ง€๋งŒ fps๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€๋จ.
128
  if abs(speed_factor - 1.0) > 1e-3:
129
  add_log(f"[LOG 8] ์žฌ์ƒ์†๋„ {speed_factor}๋ฐฐ๋กœ ์กฐ์ ˆ ์ค‘...")
130
  clip = clip.fx(mp.vfx.speedx, speed_factor)
 
142
  add_log("[LOG 11] GIF ์ƒ์„ฑ ์ค‘...")
143
  output_filename = f"temp_{uuid.uuid4().hex}.gif"
144
  try:
 
145
  loop_param = 0 if int(repeat_count) == 0 else int(repeat_count)
146
  final_clip.write_gif(output_filename, fps=target_fps, program='ffmpeg', loop=loop_param)
147
  add_log("[LOG 12] GIF ์ƒ์„ฑ ์™„๋ฃŒ! ํŒŒ์ผ๋ช…: " + output_filename)
 
196
  # ์ธ๋„ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
197
  start_thumb_output = gr.Image(label="์‹œ์ž‘ ์ธ๋„ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
198
  end_thumb_output = gr.Image(label="์ข…๋ฃŒ ์ธ๋„ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
199
+ # ํ”Œ๋žซํผ๋ณ„ ํ•ด์ƒ๋„(๋น„์œจ) ์„ ํƒ
200
+ platform_option = gr.Dropdown(
201
+ label="ํ•ด์ƒ๋„/๋น„์œจ ์„ ํƒ",
202
+ choices=["์›๋ณธ ์œ ์ง€", "์œ ํŠœ๋ธŒ (16:9)", "์‡ผ์ธ /๋ฆด์Šค (9:16)", "์ •์‚ฌ๊ฐํ˜• (1:1)", "์ธ์Šคํƒ€๊ทธ๋žจ (4:5)", "ํด๋ž˜์‹ (4:3)"],
203
+ value="์›๋ณธ ์œ ์ง€"
204
+ )
205
+ # ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ, ์žฌ์ƒ ์†๋„, ๋ฐ˜๋ณต ํšŸ์ˆ˜ ์กฐ์ ˆ
206
+ frame_rate_slider = gr.Slider(label="ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ ๋ฐฐ์œจ ์กฐ์ ˆ (0.1 ~ 2.0)", minimum=0.1, maximum=2.0, step=0.1, value=1.0)
207
  speed_slider = gr.Slider(label="์žฌ์ƒ ์†๋„ ์กฐ์ ˆ (0.5 ~ 2.0)", minimum=0.5, maximum=2.0, step=0.1, value=1.0)
208
  repeat_slider = gr.Slider(label="GIF ๋ฐ˜๋ณต ํšŸ์ˆ˜ (0: ๋ฌดํ•œ๋ฐ˜๋ณต, 1~10: ๋ฐ˜๋ณต ํšŸ์ˆ˜)", minimum=0, maximum=10, step=1, value=0)
209
  # GIF ์ƒ์„ฑ ๋ฒ„ํŠผ ๋ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
 
226
  # GIF ์ƒ์„ฑ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
227
  generate_button.click(fn=process_video,
228
  inputs=[video_input, start_time, end_time,
229
+ platform_option, frame_rate_slider, speed_slider, repeat_slider],
230
  outputs=[gif_preview_output, download_output, logs_output])
231
 
232
  gr.Markdown(
233
  "### [์‚ฌ์šฉ ๊ฐ€์ด๋“œ]\n"
234
  "1. ๋™์˜์ƒ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”.\n"
235
+ "2. ์‹œ์ž‘/๋ ์‹œ๊ฐ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.\n"
236
+ "3. ํ”Œ๋žซํผ๋ณ„ ๊ถŒ์žฅ ํ•ด์ƒ๋„/๋น„์œจ(์›๋ณธ ์œ ์ง€, ์œ ํŠœ๋ธŒ, ์‡ผ์ธ /๋ฆด์Šค, ์ •์‚ฌ๊ฐํ˜•, ์ธ์Šคํƒ€๊ทธ๋žจ, ํด๋ž˜์‹)์„ ์„ ํƒํ•˜๊ณ ,\n"
237
+ " ํ”„๋ ˆ์ž„ ๋ ˆ์ดํŠธ, ์žฌ์ƒ ์†๋„, ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์กฐ์ ˆํ•œ ํ›„ `GIF ์ƒ์„ฑํ•˜๊ธฐ` ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด GIF๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.\n"
238
  " - ๋ฐ˜๋ณต ํšŸ์ˆ˜: 0์„ ์„ ํƒํ•˜๋ฉด ๋ฌดํ•œ๋ฐ˜๋ณต, 1~10์€ ํ•ด๋‹น ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.\n"
239
  "4. ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์™€ ๋‹ค์šด๋กœ๋“œ ๋งํฌ๋ฅผ ํ†ตํ•ด GIF๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."
240
  )