Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +13 -11
video_processing.py
CHANGED
@@ -52,7 +52,9 @@ def find_scenes(video_path):
|
|
52 |
scene_list = scene_manager.get_scene_list()
|
53 |
video_manager.release()
|
54 |
|
55 |
-
|
|
|
|
|
56 |
|
57 |
def convert_timestamp_to_seconds(timestamp):
|
58 |
"""Convert a timestamp in HH:MM:SS format to seconds."""
|
@@ -67,9 +69,9 @@ def analyze_scenes(video_path, scenes, description):
|
|
67 |
best_scene = None
|
68 |
highest_prob = 0.0
|
69 |
|
70 |
-
for
|
71 |
# Extract every 5th frame from the scene
|
72 |
-
frames = extract_frames(video_path,
|
73 |
|
74 |
# Analyze frames with CLIP
|
75 |
for frame in frames:
|
@@ -81,17 +83,18 @@ def analyze_scenes(video_path, scenes, description):
|
|
81 |
max_prob = max(probs[0]).item()
|
82 |
if max_prob > highest_prob:
|
83 |
highest_prob = max_prob
|
84 |
-
best_scene =
|
85 |
|
86 |
return best_scene
|
87 |
|
88 |
-
def extract_frames(video_path,
|
89 |
frames = []
|
90 |
-
|
91 |
-
|
|
|
92 |
|
93 |
-
for
|
94 |
-
frame = video_clip.get_frame(
|
95 |
frames.append(frame)
|
96 |
|
97 |
return frames
|
@@ -100,8 +103,7 @@ def extract_best_scene(video_path, scene):
|
|
100 |
if scene is None:
|
101 |
return VideoFileClip(video_path) # Return the entire video if no scene is found
|
102 |
|
103 |
-
start_time = scene
|
104 |
-
end_time = scene[1].get_timecode()
|
105 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
106 |
end_seconds = convert_timestamp_to_seconds(end_time)
|
107 |
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
|
|
52 |
scene_list = scene_manager.get_scene_list()
|
53 |
video_manager.release()
|
54 |
|
55 |
+
# Collect the start and end times for each scene
|
56 |
+
scenes = [(start.get_timecode(), end.get_timecode()) for start, end in scene_list]
|
57 |
+
return scenes
|
58 |
|
59 |
def convert_timestamp_to_seconds(timestamp):
|
60 |
"""Convert a timestamp in HH:MM:SS format to seconds."""
|
|
|
69 |
best_scene = None
|
70 |
highest_prob = 0.0
|
71 |
|
72 |
+
for scene_id, (start_time, end_time) in enumerate(scenes):
|
73 |
# Extract every 5th frame from the scene
|
74 |
+
frames = extract_frames(video_path, start_time, end_time)
|
75 |
|
76 |
# Analyze frames with CLIP
|
77 |
for frame in frames:
|
|
|
83 |
max_prob = max(probs[0]).item()
|
84 |
if max_prob > highest_prob:
|
85 |
highest_prob = max_prob
|
86 |
+
best_scene = (start_time, end_time)
|
87 |
|
88 |
return best_scene
|
89 |
|
90 |
+
def extract_frames(video_path, start_time, end_time):
|
91 |
frames = []
|
92 |
+
start_seconds = convert_timestamp_to_seconds(start_time)
|
93 |
+
end_seconds = convert_timestamp_to_seconds(end_time)
|
94 |
+
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
95 |
|
96 |
+
for frame_time in range(0, int(video_clip.duration), 5):
|
97 |
+
frame = video_clip.get_frame(frame_time)
|
98 |
frames.append(frame)
|
99 |
|
100 |
return frames
|
|
|
103 |
if scene is None:
|
104 |
return VideoFileClip(video_path) # Return the entire video if no scene is found
|
105 |
|
106 |
+
start_time, end_time = scene
|
|
|
107 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
108 |
end_seconds = convert_timestamp_to_seconds(end_time)
|
109 |
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|