jschwab21 commited on
Commit
3f95bbc
·
verified ·
1 Parent(s): cf4ffba

Update video_processing.py

Browse files
Files changed (1) hide show
  1. video_processing.py +15 -10
video_processing.py CHANGED
@@ -52,14 +52,13 @@ def extract_frames(video_path, start_time, end_time):
52
  end_seconds = convert_timestamp_to_seconds(end_time)
53
  video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
54
  # Extract more frames: every frame in the scene
55
- for frame_time in range(0, int(video_clip.duration * video_clip.fps)):
56
  frame = video_clip.get_frame(frame_time / video_clip.fps)
57
  frames.append(frame)
58
  return frames
59
 
60
  def analyze_scenes(video_path, scenes, description):
61
- highest_prob = float('-inf')
62
- best_scene = None
63
 
64
  negative_descriptions = [
65
  "black screen",
@@ -89,18 +88,24 @@ def analyze_scenes(video_path, scenes, description):
89
  scene_prob += positive_similarity - negative_similarities
90
 
91
  scene_prob /= len(frames)
92
- print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time}, Probability={scene_prob}")
 
93
 
94
- if scene_prob > highest_prob:
95
- highest_prob = scene_prob
96
- best_scene = (start_time, end_time)
97
 
98
- if best_scene:
99
- print(f"Best Scene: Start={best_scene[0]}, End={best_scene[1]}, Probability={highest_prob}")
 
 
 
 
 
 
 
100
  else:
101
  print("No suitable scene found")
102
 
103
- return best_scene
104
 
105
  def extract_best_scene(video_path, scene):
106
  if scene is None:
 
52
  end_seconds = convert_timestamp_to_seconds(end_time)
53
  video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
54
  # Extract more frames: every frame in the scene
55
+ for frame_time in range(0, int(video_clip.duration * video_clip.fps), int(video_clip.fps / 5)):
56
  frame = video_clip.get_frame(frame_time / video_clip.fps)
57
  frames.append(frame)
58
  return frames
59
 
60
  def analyze_scenes(video_path, scenes, description):
61
+ scene_scores = []
 
62
 
63
  negative_descriptions = [
64
  "black screen",
 
88
  scene_prob += positive_similarity - negative_similarities
89
 
90
  scene_prob /= len(frames)
91
+ scene_duration = convert_timestamp_to_seconds(end_time) - convert_timestamp_to_seconds(start_time)
92
+ print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time}, Probability={scene_prob}, Duration={scene_duration}")
93
 
94
+ scene_scores.append((scene_prob, start_time, end_time, scene_duration))
 
 
95
 
96
+ # Sort scenes by probability in descending order and select the top 5
97
+ scene_scores.sort(reverse=True, key=lambda x: x[0])
98
+ top_scenes = scene_scores[:5]
99
+
100
+ # Find the longest scene among the top 5
101
+ longest_scene = max(top_scenes, key=lambda x: x[3])
102
+
103
+ if longest_scene:
104
+ print(f"Longest Scene: Start={longest_scene[1]}, End={longest_scene[2]}, Probability={longest_scene[0]}, Duration={longest_scene[3]}")
105
  else:
106
  print("No suitable scene found")
107
 
108
+ return longest_scene[1:3] if longest_scene else None
109
 
110
  def extract_best_scene(video_path, scene):
111
  if scene is None: