AndreySokolov01 commited on
Commit
709adea
1 Parent(s): 55eaf15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -49
app.py CHANGED
@@ -1,54 +1,24 @@
1
- pip install opencv-python
2
-
3
- import cv2
4
- import os
5
  import gradio as gr
6
-
7
- def split_video(video_path, split_duration):
8
- video_capture = cv2.VideoCapture(video_path)
9
- frame_rate = video_capture.get(cv2.CAP_PROP_FPS)
10
-
11
- total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
12
- total_duration = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) / frame_rate
13
-
14
- num_parts = total_duration // split_duration
15
- print(f"Total duration: {total_duration} seconds")
16
- print(f"Splitting into {num_parts} parts of {split_duration} seconds each")
17
-
18
- head, tail = os.path.split(video_path)
19
- video_filename = tail.split('.')[0]
20
- output_path = os.path.join(head, video_filename)
21
-
22
- for i in range(num_parts):
23
- start_frame = int(i * split_duration * frame_rate)
24
- end_frame = int((i + 1) * split_duration * frame_rate)
25
- output_filename = f"{output_path}_part{i+1}.mp4"
26
-
27
- output_file = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'MP4V'), frame_rate, (int(video_capture.get(3)), int(video_capture.get(4))))
28
-
29
- video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
30
-
31
- while video_capture.get(1):
32
- success, frame = video_capture.read()
33
- if not success:
34
- break
35
- if start_frame <= int(video_capture.get(cv2.CAP_PROP_POS_FRAMES)) < end_frame:
36
- output_file.write(frame)
37
-
38
- output_file.release()
39
-
40
- video_capture.release()
41
-
42
- iface = gr.Interface(
43
- fn=split_video,
44
- title="Split Video by Time",
45
- description="Split a video into multiple parts based on specified duration.",
46
  inputs=[
47
- gr.inputs.File(type="video", label="Select Video File"),
48
- gr.Number(default=5, label="Split Duration (seconds)")
 
49
  ],
50
- outputs=[gr.File(file_type="mp4", label="Video Parts")],
51
- live=True
 
52
  )
53
 
54
- iface.launch()
 
 
 
 
 
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()