Spaces:
Runtime error
Runtime error
eienmojiki
commited on
Commit
•
59bdc9f
1
Parent(s):
dcacdc3
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,79 @@
|
|
1 |
-
from moviepy.editor import *
|
2 |
-
|
3 |
from PIL import Image
|
4 |
-
|
5 |
import tempfile
|
6 |
import gradio as gr
|
7 |
|
8 |
def get_video_detail(video_path):
|
9 |
-
|
10 |
-
|
11 |
|
12 |
def convert_video_to_gif(
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
):
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
start (float, optional): Start time in seconds. Defaults to 0.
|
25 |
-
end (float, optional): End time in seconds. Defaults to None.
|
26 |
-
fps (int, optional): Frames per second. Defaults to None (max fps).
|
27 |
-
quality (int, optional): Image quality. Defaults to None (max quality).
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
"""
|
36 |
-
# Get video input & info
|
37 |
-
clip = VideoFileClip(input)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
if end > max_duration:
|
47 |
-
raise ValueError(f'End time {end} is longer than video duration {max_duration}')
|
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 |
with gr.Blocks(
|
79 |
theme = gr.themes.Base(
|
@@ -108,10 +108,8 @@ with gr.Blocks(
|
|
108 |
)
|
109 |
|
110 |
run_btn = gr.Button("Convert")
|
111 |
-
output = gr.
|
112 |
-
label="GIF"
|
113 |
-
format="gif",
|
114 |
-
type="pil"
|
115 |
)
|
116 |
|
117 |
input.change(
|
|
|
|
|
|
|
1 |
from PIL import Image
|
2 |
+
from moviepy.editor import *
|
3 |
import tempfile
|
4 |
import gradio as gr
|
5 |
|
6 |
def get_video_detail(video_path):
|
7 |
+
info = VideoFileClip(video_path)
|
8 |
+
return 0, info.duration, info.fps, max(info.size)
|
9 |
|
10 |
def convert_video_to_gif(
|
11 |
+
input: str,
|
12 |
+
start: float = 0,
|
13 |
+
end: float | None = None,
|
14 |
+
fps: int | None = None,
|
15 |
+
quality: int | None = None
|
16 |
):
|
17 |
+
"""
|
18 |
+
Convert video to GIF image
|
19 |
+
|
20 |
+
Args:
|
21 |
+
input (str): Path to video file.
|
22 |
+
start (float, optional): Start time in seconds. Defaults to 0.
|
23 |
+
end (float, optional): End time in seconds. Defaults to None.
|
24 |
+
fps (int, optional): Frames per second. Defaults to None (max fps).
|
25 |
+
quality (int, optional): Image quality. Defaults to None (max quality).
|
26 |
|
27 |
+
Returns:
|
28 |
+
str: Path to the GIF file
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
Examples:
|
31 |
+
>>> convert_video_to_gif("input.mp4")
|
32 |
|
33 |
+
"""
|
34 |
+
# Get video input & info
|
35 |
+
clip = VideoFileClip(input)
|
|
|
|
|
|
|
36 |
|
37 |
+
max_fps = clip.fps
|
38 |
+
max_duration = clip.duration
|
39 |
+
max_res = max(clip.size)
|
40 |
|
41 |
+
if end is None:
|
42 |
+
end = max_duration
|
|
|
|
|
|
|
43 |
|
44 |
+
if end > max_duration:
|
45 |
+
raise ValueError(f"End time {end} is longer than video duration {max_duration}")
|
46 |
|
47 |
+
if fps > max_fps:
|
48 |
+
raise ValueError(f"FPS {fps} is greater than video FPS {max_fps}")
|
49 |
|
50 |
+
if quality is None:
|
51 |
+
quality = max_res
|
52 |
|
53 |
+
if quality > max_res:
|
54 |
+
raise ValueError(
|
55 |
+
f"Quality must be less than video max resolution {max_res}, but is {quality}"
|
56 |
+
)
|
57 |
|
58 |
+
clip = clip.subclip(start, end)
|
59 |
+
target_height = quality
|
60 |
+
aspect_ratio = clip.size[0] / clip.size[1]
|
61 |
+
target_width = int(target_height * aspect_ratio)
|
62 |
+
clip = clip.resize((target_width, target_height))
|
63 |
|
64 |
+
clip = clip.set_fps(fps)
|
65 |
|
66 |
+
# Create a temporary file
|
67 |
+
with tempfile.NamedTemporaryFile(suffix=".gif") as temp_file:
|
68 |
+
# Write the GIF to the temporary file
|
69 |
+
clip.write_gif(temp_file.name)
|
70 |
|
71 |
+
# Return the path to the GIF file
|
72 |
+
gif_file_path = temp_file.name
|
73 |
|
74 |
+
return gif_file_path
|
75 |
|
76 |
+
# Gradio code...
|
77 |
|
78 |
with gr.Blocks(
|
79 |
theme = gr.themes.Base(
|
|
|
108 |
)
|
109 |
|
110 |
run_btn = gr.Button("Convert")
|
111 |
+
output = gr.File(
|
112 |
+
label="GIF"
|
|
|
|
|
113 |
)
|
114 |
|
115 |
input.change(
|