jschwab21 commited on
Commit
fbf2779
·
verified ·
1 Parent(s): 9f9566b

Update video_processing.py

Browse files
Files changed (1) hide show
  1. video_processing.py +12 -15
video_processing.py CHANGED
@@ -1,5 +1,5 @@
1
  import cv2
2
- from scenedetect import VideoManager, SceneManager
3
  from scenedetect.detectors import ContentDetector
4
  from moviepy.editor import VideoFileClip, concatenate_videoclips
5
  from transformers import CLIPProcessor, CLIPModel
@@ -15,10 +15,10 @@ def process_video(video_url, description):
15
  scenes = detect_scenes(video_path)
16
 
17
  # Extract frames and analyze with CLIP model
18
- best_scenes = analyze_scenes(scenes, description)
19
 
20
  # Combine best scenes into a final clip
21
- final_clip = combine_scenes(best_scenes)
22
 
23
  # Save and return the final clip
24
  final_clip_path = "output/final_clip.mp4"
@@ -26,18 +26,14 @@ def process_video(video_url, description):
26
  return final_clip_path
27
 
28
  def detect_scenes(video_path):
29
- video_manager = VideoManager([video_path])
30
  scene_manager = SceneManager()
31
  scene_manager.add_detector(ContentDetector())
32
- video_manager.start()
33
-
34
- scene_manager.detect_scenes(frame_source=video_manager)
35
  scene_list = scene_manager.get_scene_list()
36
- video_manager.release()
37
-
38
  return scene_list
39
 
40
- def analyze_scenes(scenes, description):
41
  # Load CLIP model and processor
42
  model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
43
  processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
@@ -46,7 +42,7 @@ def analyze_scenes(scenes, description):
46
 
47
  for scene in scenes:
48
  # Extract every 5th frame from the scene
49
- frames = extract_frames(scene)
50
 
51
  # Analyze frames with CLIP
52
  for frame in frames:
@@ -62,10 +58,10 @@ def analyze_scenes(scenes, description):
62
 
63
  return best_scenes
64
 
65
- def extract_frames(scene):
66
  frames = []
67
  start_frame, end_frame = scene[0].get_frames(), scene[1].get_frames()
68
- video_clip = VideoFileClip(scene[0].get_filename())
69
 
70
  for frame_num in range(start_frame, end_frame, 5):
71
  frame = video_clip.get_frame(frame_num / video_clip.fps)
@@ -73,8 +69,8 @@ def extract_frames(scene):
73
 
74
  return frames
75
 
76
- def combine_scenes(scenes):
77
- final_clip = concatenate_videoclips([VideoFileClip(scene[0].get_filename()).subclip(scene[0].get_seconds(), scene[1].get_seconds()) for scene in scenes])
78
  return final_clip
79
 
80
  def download_video(video_url):
@@ -89,3 +85,4 @@ def download_video(video_url):
89
  video_file = ydl.prepare_filename(info_dict)
90
 
91
  return video_file
 
 
1
  import cv2
2
+ from scenedetect import open_video, SceneManager
3
  from scenedetect.detectors import ContentDetector
4
  from moviepy.editor import VideoFileClip, concatenate_videoclips
5
  from transformers import CLIPProcessor, CLIPModel
 
15
  scenes = detect_scenes(video_path)
16
 
17
  # Extract frames and analyze with CLIP model
18
+ best_scenes = analyze_scenes(video_path, scenes, description)
19
 
20
  # Combine best scenes into a final clip
21
+ final_clip = combine_scenes(video_path, best_scenes)
22
 
23
  # Save and return the final clip
24
  final_clip_path = "output/final_clip.mp4"
 
26
  return final_clip_path
27
 
28
  def detect_scenes(video_path):
29
+ video = open_video(video_path)
30
  scene_manager = SceneManager()
31
  scene_manager.add_detector(ContentDetector())
32
+ scene_manager.detect_scenes(video)
 
 
33
  scene_list = scene_manager.get_scene_list()
 
 
34
  return scene_list
35
 
36
+ def analyze_scenes(video_path, scenes, description):
37
  # Load CLIP model and processor
38
  model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
39
  processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
 
42
 
43
  for scene in scenes:
44
  # Extract every 5th frame from the scene
45
+ frames = extract_frames(video_path, scene)
46
 
47
  # Analyze frames with CLIP
48
  for frame in frames:
 
58
 
59
  return best_scenes
60
 
61
+ def extract_frames(video_path, scene):
62
  frames = []
63
  start_frame, end_frame = scene[0].get_frames(), scene[1].get_frames()
64
+ video_clip = VideoFileClip(video_path)
65
 
66
  for frame_num in range(start_frame, end_frame, 5):
67
  frame = video_clip.get_frame(frame_num / video_clip.fps)
 
69
 
70
  return frames
71
 
72
+ def combine_scenes(video_path, scenes):
73
+ final_clip = concatenate_videoclips([VideoFileClip(video_path).subclip(scene[0].get_seconds(), scene[1].get_seconds()) for scene in scenes])
74
  return final_clip
75
 
76
  def download_video(video_url):
 
85
  video_file = ydl.prepare_filename(info_dict)
86
 
87
  return video_file
88
+