Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +28 -10
video_processing.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import cv2
|
2 |
-
from scenedetect import open_video, SceneManager
|
3 |
from scenedetect.detectors import ContentDetector
|
4 |
-
from moviepy.editor import VideoFileClip
|
5 |
from transformers import CLIPProcessor, CLIPModel
|
6 |
import torch
|
7 |
import yt_dlp
|
@@ -12,7 +12,7 @@ def process_video(video_url, description):
|
|
12 |
video_path = download_video(video_url)
|
13 |
|
14 |
# Segment video into scenes
|
15 |
-
scenes =
|
16 |
|
17 |
# Extract frames and analyze with CLIP model
|
18 |
best_scene = analyze_scenes(video_path, scenes, description)
|
@@ -33,14 +33,30 @@ def process_video(video_url, description):
|
|
33 |
|
34 |
return final_clip_path
|
35 |
|
36 |
-
def
|
37 |
-
video
|
|
|
38 |
scene_manager = SceneManager()
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
scene_list = scene_manager.get_scene_list()
|
|
|
|
|
42 |
return scene_list
|
43 |
|
|
|
|
|
|
|
|
|
|
|
44 |
def analyze_scenes(video_path, scenes, description):
|
45 |
# Load CLIP model and processor
|
46 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
@@ -82,9 +98,11 @@ def extract_best_scene(video_path, scene):
|
|
82 |
if scene is None:
|
83 |
return VideoFileClip(video_path) # Return the entire video if no scene is found
|
84 |
|
85 |
-
start_time = scene[0].
|
86 |
-
end_time = scene[1].
|
87 |
-
|
|
|
|
|
88 |
return video_clip
|
89 |
|
90 |
def download_video(video_url):
|
|
|
1 |
import cv2
|
2 |
+
from scenedetect import open_video, SceneManager, VideoManager
|
3 |
from scenedetect.detectors import ContentDetector
|
4 |
+
from moviepy.editor import VideoFileClip
|
5 |
from transformers import CLIPProcessor, CLIPModel
|
6 |
import torch
|
7 |
import yt_dlp
|
|
|
12 |
video_path = download_video(video_url)
|
13 |
|
14 |
# Segment video into scenes
|
15 |
+
scenes = find_scenes(video_path)
|
16 |
|
17 |
# Extract frames and analyze with CLIP model
|
18 |
best_scene = analyze_scenes(video_path, scenes, description)
|
|
|
33 |
|
34 |
return final_clip_path
|
35 |
|
36 |
+
def find_scenes(video_path):
|
37 |
+
# Create a video manager object for the video
|
38 |
+
video_manager = VideoManager([video_path])
|
39 |
scene_manager = SceneManager()
|
40 |
+
|
41 |
+
# Add ContentDetector algorithm with a threshold. Adjust threshold as needed.
|
42 |
+
scene_manager.add_detector(ContentDetector(threshold=30))
|
43 |
+
|
44 |
+
# Start the video manager and perform scene detection
|
45 |
+
video_manager.set_downscale_factor()
|
46 |
+
video_manager.start()
|
47 |
+
scene_manager.detect_scenes(frame_source=video_manager)
|
48 |
+
|
49 |
+
# Obtain list of detected scenes as timecodes
|
50 |
scene_list = scene_manager.get_scene_list()
|
51 |
+
video_manager.release()
|
52 |
+
|
53 |
return scene_list
|
54 |
|
55 |
+
def convert_timestamp_to_seconds(timestamp):
|
56 |
+
"""Convert a timestamp in HH:MM:SS format to seconds."""
|
57 |
+
h, m, s = map(float, timestamp.split(':'))
|
58 |
+
return int(h) * 3600 + int(m) * 60 + s
|
59 |
+
|
60 |
def analyze_scenes(video_path, scenes, description):
|
61 |
# Load CLIP model and processor
|
62 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
|
|
98 |
if scene is None:
|
99 |
return VideoFileClip(video_path) # Return the entire video if no scene is found
|
100 |
|
101 |
+
start_time = scene[0].get_timecode()
|
102 |
+
end_time = scene[1].get_timecode()
|
103 |
+
start_seconds = convert_timestamp_to_seconds(start_time)
|
104 |
+
end_seconds = convert_timestamp_to_seconds(end_time)
|
105 |
+
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
106 |
return video_clip
|
107 |
|
108 |
def download_video(video_url):
|