Sangwon78 commited on
Commit
91df438
ยท
verified ยท
1 Parent(s): 61eaed0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -51
app.py CHANGED
@@ -1,33 +1,44 @@
1
  import os
2
  import gradio as gr
3
- from moviepy.editor import VideoFileClip
4
- from moviepy.video.fx import speedx
5
  import tempfile
 
6
 
7
- # ์—…๋กœ๋“œ๋œ ์˜์ƒ์˜ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ (์˜์ƒ ๊ธธ์ด, ํ•ด์ƒ๋„, FPS ๋“ฑ)
8
  def get_video_info(video_path):
9
- clip = VideoFileClip(video_path)
10
- info = f"์˜์ƒ ๊ธธ์ด: {clip.duration:.2f}์ดˆ\nํ•ด์ƒ๋„: {clip.w} x {clip.h}\nFPS: {clip.fps}"
11
- clip.close()
 
 
 
 
 
 
 
12
  return info
13
 
14
- # ํŠน์ • ์‹œ๊ฐ„(HH:MM:SS)์˜ ํ”„๋ ˆ์ž„(์ธ๋„ค์ผ)์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
15
  def extract_frame(video_path, time_str):
16
  try:
17
  h, m, s = map(int, time_str.split(":"))
18
  time_sec = h * 3600 + m * 60 + s
19
  except Exception as e:
20
  return None
21
- clip = VideoFileClip(video_path)
22
- if time_sec > clip.duration:
23
- time_sec = clip.duration / 2 # ๋งŒ์•ฝ ์ž…๋ ฅ ์‹œ๊ฐ„์ด ์˜์ƒ ๊ธธ์ด๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ์ค‘๊ฐ„ ํ”„๋ ˆ์ž„ ์‚ฌ์šฉ
24
- frame = clip.get_frame(time_sec)
25
- clip.close()
26
- # Gradio๋Š” numpy ๋ฐฐ์—ด ํ˜•์‹์˜ ์ด๋ฏธ์ง€๋ฅผ ์ง€์›ํ•จ
 
 
 
 
 
 
27
  return frame
28
 
29
- # ์ง€์ • ์‹œ๊ฐ„ ๊ตฌ๊ฐ„์˜ ์˜์ƒ์„ GIF๋กœ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜
30
- def generate_gif(video_path, start_time, end_time, scale_factor, frame_rate, speed_factor, repeat_count):
31
  try:
32
  sh, sm, ss = map(int, start_time.split(":"))
33
  start_sec = sh * 3600 + sm * 60 + ss
@@ -39,31 +50,48 @@ def generate_gif(video_path, start_time, end_time, scale_factor, frame_rate, spe
39
  if start_sec >= end_sec:
40
  return None, "์‹œ์ž‘ ์‹œ๊ฐ„์ด ์ข…๋ฃŒ ์‹œ๊ฐ„๋ณด๋‹ค ๋น ๋ฅด๊ฑฐ๋‚˜ ๊ฐ™์Šต๋‹ˆ๋‹ค."
41
 
42
- clip = VideoFileClip(video_path).subclip(start_sec, end_sec)
43
-
44
- # ์žฌ์ƒ ์†๋„ ์กฐ์ ˆ (๊ธฐ๋ณธ 1๋ฐฐ์†)
45
- if speed_factor != 1:
46
- clip = clip.fx(speedx, factor=speed_factor)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- # ํ•ด์ƒ๋„ ์กฐ์ ˆ (scale_factor: 0.1 ~ 1.0)
49
- clip = clip.resize(scale_factor)
50
-
51
- # GIF๋ฅผ ์ €์žฅํ•  ์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ
52
  temp_gif = tempfile.NamedTemporaryFile(delete=False, suffix=".gif")
53
  temp_gif.close()
54
-
55
  try:
56
- clip.write_gif(temp_gif.name, fps=frame_rate, loop=repeat_count)
57
  msg = "GIF ์ƒ์„ฑ ์™„๋ฃŒ"
58
  except Exception as e:
59
  msg = f"GIF ์ƒ์„ฑ ์‹คํŒจ: {e}"
60
  temp_gif.name = None
61
- finally:
62
- clip.close()
63
-
64
  return temp_gif.name, msg
65
 
66
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ (์ŠคํŽ˜์ด์Šค ์ œ๋ชฉ: GIF์ƒ์„ฑ๊ธฐ)
67
  with gr.Blocks(title="GIF์ƒ์„ฑ๊ธฐ") as demo:
68
  gr.Markdown("## GIF์ƒ์„ฑ๊ธฐ")
69
 
@@ -93,32 +121,12 @@ with gr.Blocks(title="GIF์ƒ์„ฑ๊ธฐ") as demo:
93
  start_img = extract_frame(video_path, start_time)
94
  end_img = extract_frame(video_path, end_time)
95
  return start_img, end_img
96
-
97
  video_input.change(fn=update_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumb, end_thumb])
98
  start_time_input.change(fn=update_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumb, end_thumb])
99
  end_time_input.change(fn=update_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumb, end_thumb])
100
 
101
- # ํ•ด์ƒ๋„(์Šค์ผ€์ผ), ํ”„๋ ˆ์ž„ ์†๋„, ์†๋„ ์กฐ์ ˆ, ๋ฐ˜๋ณต ํšŸ์ˆ˜ ์กฐ์ ˆ ์Šฌ๋ผ์ด๋”
102
  with gr.Row():
103
  scale_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=1.0, label="ํ•ด์ƒ๋„ (์Šค์ผ€์ผ)")
104
  fps_slider = gr.Slider(minimum=5, maximum=30, step=1, value=15, label="ํ”„๋ ˆ์ž„ ์†๋„ (FPS)")
105
  speed_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="์†๋„ ์กฐ์ ˆ")
106
- repeat_slider = gr.Slider(minimum=1, maximum=20, step=1, value=10, label="๋ฐ˜๋ณต ํšŸ์ˆ˜")
107
-
108
- gif_output = gr.Image(label="์ƒ์„ฑ๋œ GIF ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
109
- download_output = gr.File(label="GIF ๋‹ค์šด๋กœ๋“œ")
110
- status_output = gr.Textbox(label="์ƒํƒœ ๋ฉ”์‹œ์ง€", interactive=False)
111
-
112
- # GIF ์ƒ์„ฑ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
113
- def process_video(video_path, start_time, end_time, scale_factor, fps, speed_factor, repeat_count):
114
- if video_path is None:
115
- return None, None, "์˜์ƒ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•ด์ฃผ์„ธ์š”."
116
- gif_path, status = generate_gif(video_path, start_time, end_time, scale_factor, fps, speed_factor, repeat_count)
117
- return gif_path, gif_path, status
118
-
119
- generate_btn = gr.Button("GIF ์ƒ์„ฑ")
120
- generate_btn.click(fn=process_video,
121
- inputs=[video_input, start_time_input, end_time_input, scale_slider, fps_slider, speed_slider, repeat_slider],
122
- outputs=[gif_output, download_output, status_output])
123
-
124
- demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ import cv2
4
+ import imageio
5
  import tempfile
6
+ import numpy as np
7
 
 
8
  def get_video_info(video_path):
9
+ cap = cv2.VideoCapture(video_path)
10
+ if not cap.isOpened():
11
+ return "์˜์ƒ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
12
+ fps = cap.get(cv2.CAP_PROP_FPS)
13
+ frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
14
+ duration = frame_count / fps if fps > 0 else 0
15
+ width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
16
+ height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
17
+ cap.release()
18
+ info = f"์˜์ƒ ๊ธธ์ด: {duration:.2f}์ดˆ\nํ•ด์ƒ๋„: {int(width)} x {int(height)}\nFPS: {fps:.2f}"
19
  return info
20
 
 
21
  def extract_frame(video_path, time_str):
22
  try:
23
  h, m, s = map(int, time_str.split(":"))
24
  time_sec = h * 3600 + m * 60 + s
25
  except Exception as e:
26
  return None
27
+ cap = cv2.VideoCapture(video_path)
28
+ if not cap.isOpened():
29
+ return None
30
+ fps = cap.get(cv2.CAP_PROP_FPS)
31
+ frame_index = int(time_sec * fps)
32
+ cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
33
+ ret, frame = cap.read()
34
+ cap.release()
35
+ if not ret:
36
+ return None
37
+ # cv2๋Š” BGR ํฌ๋งท์œผ๋กœ ์ฝ์œผ๋ฏ€๋กœ RGB๋กœ ๋ณ€ํ™˜
38
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
39
  return frame
40
 
41
+ def generate_gif(video_path, start_time, end_time, scale_factor, output_fps, speed_factor, repeat_count):
 
42
  try:
43
  sh, sm, ss = map(int, start_time.split(":"))
44
  start_sec = sh * 3600 + sm * 60 + ss
 
50
  if start_sec >= end_sec:
51
  return None, "์‹œ์ž‘ ์‹œ๊ฐ„์ด ์ข…๋ฃŒ ์‹œ๊ฐ„๋ณด๋‹ค ๋น ๋ฅด๊ฑฐ๋‚˜ ๊ฐ™์Šต๋‹ˆ๋‹ค."
52
 
53
+ cap = cv2.VideoCapture(video_path)
54
+ if not cap.isOpened():
55
+ return None, "์˜์ƒ ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
56
+ orig_fps = cap.get(cv2.CAP_PROP_FPS)
57
+ if orig_fps <= 0:
58
+ orig_fps = 25 # ๊ธฐ๋ณธ FPS
59
+
60
+ frames = []
61
+ t = start_sec
62
+ # output_fps์— ๋”ฐ๋ผ ์ผ์ • ๊ฐ„๊ฒฉ์œผ๋กœ ํ”„๋ ˆ์ž„ ์ถ”์ถœ
63
+ while t <= end_sec:
64
+ frame_index = int(t * orig_fps)
65
+ cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
66
+ ret, frame = cap.read()
67
+ if not ret:
68
+ break
69
+ # ํ•ด์ƒ๋„(์Šค์ผ€์ผ) ์กฐ์ ˆ
70
+ if scale_factor != 1.0:
71
+ new_w = int(frame.shape[1] * scale_factor)
72
+ new_h = int(frame.shape[0] * scale_factor)
73
+ frame = cv2.resize(frame, (new_w, new_h))
74
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
75
+ frames.append(frame)
76
+ t += 1.0 / output_fps
77
+ cap.release()
78
+
79
+ if not frames:
80
+ return None, "ํ”„๋ ˆ์ž„์„ ์ถ”์ถœํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
81
+
82
+ # ๊ฐ ํ”„๋ ˆ์ž„์˜ ํ‘œ์‹œ ์‹œ๊ฐ„ (์ดˆ) : output_fps์— ๋”ฐ๋ฅธ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์„ ์†๋„ ์กฐ์ ˆ๋กœ ๋ณด์ •
83
+ duration_per_frame = (1.0 / output_fps) / speed_factor
84
 
 
 
 
 
85
  temp_gif = tempfile.NamedTemporaryFile(delete=False, suffix=".gif")
86
  temp_gif.close()
 
87
  try:
88
+ imageio.mimsave(temp_gif.name, frames, duration=duration_per_frame, loop=repeat_count)
89
  msg = "GIF ์ƒ์„ฑ ์™„๋ฃŒ"
90
  except Exception as e:
91
  msg = f"GIF ์ƒ์„ฑ ์‹คํŒจ: {e}"
92
  temp_gif.name = None
 
 
 
93
  return temp_gif.name, msg
94
 
 
95
  with gr.Blocks(title="GIF์ƒ์„ฑ๊ธฐ") as demo:
96
  gr.Markdown("## GIF์ƒ์„ฑ๊ธฐ")
97
 
 
121
  start_img = extract_frame(video_path, start_time)
122
  end_img = extract_frame(video_path, end_time)
123
  return start_img, end_img
 
124
  video_input.change(fn=update_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumb, end_thumb])
125
  start_time_input.change(fn=update_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumb, end_thumb])
126
  end_time_input.change(fn=update_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumb, end_thumb])
127
 
 
128
  with gr.Row():
129
  scale_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=1.0, label="ํ•ด์ƒ๋„ (์Šค์ผ€์ผ)")
130
  fps_slider = gr.Slider(minimum=5, maximum=30, step=1, value=15, label="ํ”„๋ ˆ์ž„ ์†๋„ (FPS)")
131
  speed_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="์†๋„ ์กฐ์ ˆ")
132
+ repeat_slider = gr.Slider(minimum=1, maximum=20, step=1, v