Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +17 -17
video_processing.py
CHANGED
@@ -1,11 +1,11 @@
|
|
|
|
1 |
import cv2
|
2 |
-
from scenedetect import
|
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
|
8 |
-
import os
|
9 |
|
10 |
def process_video(video_url, description):
|
11 |
# Download or load the video from the URL
|
@@ -29,7 +29,7 @@ def process_video(video_url, description):
|
|
29 |
try:
|
30 |
if os.path.exists(final_clip_path):
|
31 |
os.remove(final_clip_path)
|
32 |
-
final_clip.write_videofile(final_clip_path)
|
33 |
except Exception as e:
|
34 |
return str(e)
|
35 |
|
@@ -52,7 +52,7 @@ def find_scenes(video_path):
|
|
52 |
scene_list = scene_manager.get_scene_list()
|
53 |
video_manager.release()
|
54 |
|
55 |
-
#
|
56 |
scenes = [(start.get_timecode(), end.get_timecode()) for start, end in scene_list]
|
57 |
return scenes
|
58 |
|
@@ -61,6 +61,18 @@ def convert_timestamp_to_seconds(timestamp):
|
|
61 |
h, m, s = map(float, timestamp.split(':'))
|
62 |
return int(h) * 3600 + int(m) * 60 + s
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
def analyze_scenes(video_path, scenes, description):
|
65 |
# Load CLIP model and processor
|
66 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
@@ -69,7 +81,7 @@ def analyze_scenes(video_path, scenes, description):
|
|
69 |
best_scene = None
|
70 |
highest_prob = 0.0
|
71 |
|
72 |
-
for
|
73 |
# Extract every 5th frame from the scene
|
74 |
frames = extract_frames(video_path, start_time, end_time)
|
75 |
|
@@ -87,18 +99,6 @@ def analyze_scenes(video_path, scenes, description):
|
|
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
|
101 |
-
|
102 |
def extract_best_scene(video_path, scene):
|
103 |
if scene is None:
|
104 |
return VideoFileClip(video_path) # Return the entire video if no scene is found
|
|
|
1 |
+
import os
|
2 |
import cv2
|
3 |
+
from scenedetect import VideoManager, SceneManager
|
4 |
from scenedetect.detectors import ContentDetector
|
5 |
from moviepy.editor import VideoFileClip
|
6 |
from transformers import CLIPProcessor, CLIPModel
|
7 |
import torch
|
8 |
import yt_dlp
|
|
|
9 |
|
10 |
def process_video(video_url, description):
|
11 |
# Download or load the video from the URL
|
|
|
29 |
try:
|
30 |
if os.path.exists(final_clip_path):
|
31 |
os.remove(final_clip_path)
|
32 |
+
final_clip.write_videofile(final_clip_path, codec='libx264', audio_codec='aac')
|
33 |
except Exception as e:
|
34 |
return str(e)
|
35 |
|
|
|
52 |
scene_list = scene_manager.get_scene_list()
|
53 |
video_manager.release()
|
54 |
|
55 |
+
# Format the list of scenes as start and end timecodes
|
56 |
scenes = [(start.get_timecode(), end.get_timecode()) for start, end in scene_list]
|
57 |
return scenes
|
58 |
|
|
|
61 |
h, m, s = map(float, timestamp.split(':'))
|
62 |
return int(h) * 3600 + int(m) * 60 + s
|
63 |
|
64 |
+
def extract_frames(video_path, start_time, end_time):
|
65 |
+
frames = []
|
66 |
+
start_seconds = convert_timestamp_to_seconds(start_time)
|
67 |
+
end_seconds = convert_timestamp_to_seconds(end_time)
|
68 |
+
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
69 |
+
|
70 |
+
for frame_time in range(0, int(video_clip.duration), 5):
|
71 |
+
frame = video_clip.get_frame(frame_time)
|
72 |
+
frames.append(frame)
|
73 |
+
|
74 |
+
return frames
|
75 |
+
|
76 |
def analyze_scenes(video_path, scenes, description):
|
77 |
# Load CLIP model and processor
|
78 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
|
|
81 |
best_scene = None
|
82 |
highest_prob = 0.0
|
83 |
|
84 |
+
for start_time, end_time in scenes:
|
85 |
# Extract every 5th frame from the scene
|
86 |
frames = extract_frames(video_path, start_time, end_time)
|
87 |
|
|
|
99 |
|
100 |
return best_scene
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
def extract_best_scene(video_path, scene):
|
103 |
if scene is None:
|
104 |
return VideoFileClip(video_path) # Return the entire video if no scene is found
|