AndreySokolov01 commited on
Commit
abdd75f
1 Parent(s): cef68e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -33
app.py CHANGED
@@ -1,35 +1,49 @@
1
- import subprocess
2
- import sys
3
  import gradio as gr
4
 
5
- def install_opencv():
6
- try:
7
- import cv2
8
- print("OpenCV is already installed.")
9
- except ImportError:
10
- print("Installing OpenCV...")
11
- subprocess.check_call([sys.executable, "-m", "pip", "install", "opencv-python"])
12
- print("OpenCV installation completed.")
13
-
14
- def extract_clip(video_path, clip_start, clip_duration):
15
- # ... (rest of your extract_clip function)
16
-
17
- def display_frames(frames):
18
- num_cols = 4 # Number of columns in the image grid
19
- num_rows = (len(frames) + num_cols - 1) // num_cols
20
- grid_image = cv2.hconcat([cv2.hconcat(frames[i:i+num_cols]) for i in range(0, len(frames), num_cols)])
21
- return grid_image
22
-
23
- # Install OpenCV if not already installed
24
- install_opencv()
25
-
26
- # Create Gradio interface
27
- video_input = gr.inputs.Video(type="file")
28
- start_input = gr.inputs.Number(default=5, label="Start Time (seconds)")
29
- duration_input = gr.inputs.Number(default=3, label="Duration (seconds)")
30
- image_output = gr.outputs.Image(type="pil")
31
-
32
- gr.Interface(fn=lambda video, start, duration: display_frames(extract_clip(video, start, duration)),
33
- inputs=[video_input, start_input, duration_input],
34
- outputs=image_output,
35
- title="Extract Video Clip").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
  import gradio as gr
4
 
5
+ def split_video(video_path, split_duration):
6
+ video_capture = cv2.VideoCapture(video_path)
7
+ frame_rate = video_capture.get(cv2.CAP_PROP_FPS)
8
+
9
+ total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
10
+ total_duration = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) / frame_rate
11
+
12
+ num_parts = total_duration // split_duration
13
+ print(f"Total duration: {total_duration} seconds")
14
+ print(f"Splitting into {num_parts} parts of {split_duration} seconds each")
15
+
16
+ head, tail = os.path.split(video_path)
17
+ video_filename = tail.split('.')[0]
18
+ output_path = os.path.join(head, video_filename)
19
+
20
+ for i in range(num_parts):
21
+ start_frame = int(i * split_duration * frame_rate)
22
+ end_frame = int((i + 1) * split_duration * frame_rate)
23
+ output_filename = f"{output_path}_part{i+1}.mp4"
24
+
25
+ output_file = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'MP4V'), frame_rate, (int(video_capture.get(3)), int(video_capture.get(4))))
26
+
27
+ video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
28
+
29
+ while video_capture.get(1):
30
+ success, frame = video_capture.read()
31
+ if not success:
32
+ break
33
+ if start_frame <= int(video_capture.get(cv2.CAP_PROP_POS_FRAMES)) < end_frame:
34
+ output_file.write(frame)
35
+
36
+ output_file.release()
37
+
38
+ video_capture.release()
39
+
40
+ iface = gr.Interface(
41
+ fn=split_video,
42
+ title="Split Video by Time",
43
+ description="Split a video into multiple parts based on specified duration.",
44
+ inputs=[gr.Video(type="filepath", label="Video File"), gr.Number(default=5, label="Split Duration (seconds)")],
45
+ outputs=[gr.File(file_type="mp4", label="Video Parts")],
46
+ live=True
47
+ )
48
+
49
+ iface.launch()