AndreySokolov01 commited on
Commit
1107c78
1 Parent(s): a0ad634

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -1,24 +1,39 @@
 
 
 
1
  import gradio as gr
2
  import ffmpeg
 
3
 
4
- def trim_video(input_video, start_time, end_time):
5
- output_video = f"{start_time}_{end_time}.mp4"
6
- stream = ffmpeg.input(input_video)
7
- stream = ffmpeg.trim(stream, start=start_time, end=end_time)
8
- stream = ffmpeg.output(stream, output_video)
9
- ffmpeg.run(stream, overwrite_output=True)
10
- return output_video
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  interface = gr.Interface(
13
- trim_video,
14
  inputs=[
15
  gr.File(label="Upload Video"),
16
- gr.Number(label="Start Time (seconds)"),
17
- gr.Number(label="End Time (seconds)"),
18
  ],
19
- outputs=gr.File(label="Trimmed Video"),
20
- title="Video Trimmer",
21
- description="Trim videos by specifying start and end times.",
22
  )
23
 
24
- interface.launch()
 
1
+ Понял, вам нужно разделить видеофайл на равные промежутки времени, заданные пользователем. Вот исправленный код, который должен выполнять эту задачу:
2
+
3
+ ```python
4
  import gradio as gr
5
  import ffmpeg
6
+ from math import ceil
7
 
8
+ def split_video(input_video, duration):
9
+ video_info = ffmpeg.probe(input_video)
10
+ video_duration = float(video_info['format']['duration'])
11
+ num_segments = ceil(video_duration / duration)
12
+
13
+ segments = []
14
+ for i in range(num_segments):
15
+ start_time = i * duration
16
+ end_time = min((i + 1) * duration, video_duration)
17
+ output_video = f"segment_{i+1}.mp4"
18
+
19
+ stream = ffmpeg.input(input_video)
20
+ stream = ffmpeg.trim(stream, start=start_time, end=end_time)
21
+ stream = ffmpeg.output(stream, output_video)
22
+ ffmpeg.run(stream, overwrite_output=True)
23
+
24
+ segments.append(output_video)
25
+
26
+ return segments
27
 
28
  interface = gr.Interface(
29
+ split_video,
30
  inputs=[
31
  gr.File(label="Upload Video"),
32
+ gr.Number(label="Segment Duration (seconds)"),
 
33
  ],
34
+ outputs=gr.Files(label="Video Segments"),
35
+ title="Video Splitter",
36
+ description="Split a video into equal-duration segments.",
37
  )
38
 
39
+ interface.launch()