eienmojiki commited on
Commit
59bdc9f
1 Parent(s): dcacdc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -57
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
- info = VideoFileClip(video_path)
10
- return 0, info.duration, info.fps, max(info.size)
11
 
12
  def convert_video_to_gif(
13
- input: str,
14
- start: float = 0,
15
- end: float | None = None,
16
- fps: int | None = None,
17
- quality: int | None = None
18
  ):
19
- """
20
- Convert video to GIF image
 
 
 
 
 
 
 
21
 
22
- Args:
23
- input (str): Path to video file.
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
- Returns:
30
- Image.Image: GIF image
31
 
32
- Examples:
33
- >>> convert_video_to_gif("input.mp4")
34
-
35
- """
36
- # Get video input & info
37
- clip = VideoFileClip(input)
38
 
39
- max_fps = clip.fps
40
- max_duration = clip.duration
41
- max_res = max(clip.size)
42
 
43
- if end is None:
44
- end = max_duration
45
-
46
- if end > max_duration:
47
- raise ValueError(f'End time {end} is longer than video duration {max_duration}')
48
 
49
- if fps > max_fps:
50
- raise ValueError(f'FPS {fps} is greater than video FPS {max_fps}')
51
 
52
- if quality is None:
53
- quality = max_res
54
 
55
- if quality > max_res:
56
- raise ValueError(f'Quality must be less than video max resolution {max_res}, but is {quality}')
57
 
 
 
 
 
58
 
59
- clip = clip.subclip(start, end)
60
- target_height = quality
61
- aspect_ratio = clip.size[0] / clip.size[1]
62
- target_width = int(target_height * aspect_ratio)
63
- clip = clip.resize((target_width, target_height))
64
 
65
- clip = clip.set_fps(fps)
66
 
67
- # Create a temporary file
68
- with tempfile.NamedTemporaryFile(suffix='.gif') as temp_file:
69
- # Write the GIF to the temporary file
70
- clip.write_gif(temp_file.name)
71
 
72
- # Read the GIF from the temporary file
73
- gif_image = Image.open(temp_file.name)
74
 
75
- return gif_image
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.Image(
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(