AndreySokolov01 commited on
Commit
a76b13e
1 Parent(s): 2b7f3dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -1,36 +1,34 @@
1
  import gradio as gr
2
- import ffmpeg
3
  from math import ceil
4
 
5
  def split_video(input_video, duration):
6
- video_info = ffmpeg.probe(input_video)
7
- video_duration = float(video_info['format']['duration'])
8
- num_segments = ceil(video_duration / duration)
9
-
10
- segments = []
11
- for i in range(num_segments):
12
- start_time = i * duration
13
- end_time = min((i + 1) * duration, video_duration)
14
- output_video = f"segment_{i+1}.mp4"
15
-
16
- stream = ffmpeg.input(input_video)
17
- stream = ffmpeg.trim(stream, start=start_time, end=end_time)
18
- stream = ffmpeg.output(stream, output_video)
19
- ffmpeg.run(stream, overwrite_output=True)
20
-
21
- segments.append(output_video)
22
-
23
- return segments
24
 
25
  interface = gr.Interface(
26
- split_video,
27
- inputs=[
28
- gr.File(label="Upload Video"),
29
- gr.Number(label="Segment Duration (seconds)"),
30
- ],
31
- outputs=gr.Files(label="Video Segments"),
32
- title="Video Splitter",
33
- description="Split a video into equal-duration segments.",
34
  )
35
 
36
  interface.launch()
 
1
  import gradio as gr
2
+ import moviepy.editor as mp
3
  from math import ceil
4
 
5
  def split_video(input_video, duration):
6
+ video_clip = mp.VideoFileClip(input_video)
7
+ video_duration = video_clip.duration
8
+ num_segments = ceil(video_duration / duration)
9
+
10
+ segments = []
11
+ for i in range(num_segments):
12
+ start_time = i * duration
13
+ end_time = min((i + 1) * duration, video_duration)
14
+ output_video = f"segment_{(i+1).zfill(2)}.mp4"
15
+
16
+ video_segment = video_clip.subclip(start_time, end_time)
17
+ video_segment.write_videofile(output_video, codec='libx264', threads=4)
18
+
19
+ segments.append(output_video)
20
+
21
+ return segments
 
 
22
 
23
  interface = gr.Interface(
24
+ split_video,
25
+ inputs=[
26
+ gr.File(label="Upload Video"),
27
+ gr.Number(label="Segment Duration (seconds)"),
28
+ ],
29
+ outputs=gr.Files(label="Video Segments"),
30
+ title="Video Splitter",
31
+ description="Split a video into equal-duration segments. The segments will be named 'segment_01.mp4', 'segment_02.mp4', etc.",
32
  )
33
 
34
  interface.launch()