Salimshakeel commited on
Commit
deda5ff
·
1 Parent(s): c41bf9c

use movie for video compilation

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -0
  2. services/summarizer.py +7 -36
requirements.txt CHANGED
@@ -8,3 +8,4 @@ opencv-python
8
  torch
9
  torchvision
10
  tqdm
 
 
8
  torch
9
  torchvision
10
  tqdm
11
+ moviepy
services/summarizer.py CHANGED
@@ -5,6 +5,7 @@ from services.model_loader import load_model
5
  import subprocess
6
  import os
7
  import numpy as np
 
8
 
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
  model = load_model("Model/epoch-199.pkl")
@@ -31,54 +32,24 @@ def get_selected_indices(scores, picks, threshold=SCORE_THRESHOLD):
31
  return selected
32
 
33
  def save_summary_video(video_path, selected_indices, output_path, fps=15):
34
- import cv2
35
-
36
  cap = cv2.VideoCapture(video_path)
37
  selected = set(selected_indices)
38
- frame_id = 0
39
  frames = []
 
40
 
41
- while cap.isOpened():
42
  ret, frame = cap.read()
43
  if not ret:
44
  break
45
  if frame_id in selected:
 
46
  frames.append(frame)
47
  frame_id += 1
48
  cap.release()
49
 
50
- if len(frames) == 0:
51
  print("No frames selected.")
52
  return
53
 
54
- h, w, _ = frames[0].shape
55
- print("Video dimensions:", w, h)
56
-
57
- ext = output_path.split('.')[-1]
58
- raw_output_path = output_path.replace(f".{ext}", f"_raw.{ext}")
59
-
60
- out = cv2.VideoWriter(raw_output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (h, w))
61
- for frame in frames:
62
- out.write(frame)
63
- out.release()
64
-
65
- print("Fixing the video with ffmpeg")
66
- fix_video_with_ffmpeg(raw_output_path, output_path)
67
-
68
- def fix_video_with_ffmpeg(input_path, output_path):
69
- # 2️⃣ Use FFmpeg to fix video (browser-compatible)
70
- try:
71
- subprocess.run([
72
- "ffmpeg",
73
- "-y", # overwrite if file exists
74
- "-i", input_path,
75
- "-vcodec", "libx264",
76
- "-acodec", "aac",
77
- output_path,
78
- ], check=True)
79
- print(f"✅ FFmpeg re-encoded video saved to: {output_path}")
80
- os.remove(input_path)
81
- except subprocess.CalledProcessError as e:
82
- print("❌ FFmpeg failed:", e)
83
- print("⚠️ Using raw video instead.")
84
- os.rename(input_path, output_path)
 
5
  import subprocess
6
  import os
7
  import numpy as np
8
+ from moviepy import ImageSequenceClip
9
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  model = load_model("Model/epoch-199.pkl")
 
32
  return selected
33
 
34
  def save_summary_video(video_path, selected_indices, output_path, fps=15):
 
 
35
  cap = cv2.VideoCapture(video_path)
36
  selected = set(selected_indices)
 
37
  frames = []
38
+ frame_id = 0
39
 
40
+ while True:
41
  ret, frame = cap.read()
42
  if not ret:
43
  break
44
  if frame_id in selected:
45
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
46
  frames.append(frame)
47
  frame_id += 1
48
  cap.release()
49
 
50
+ if not frames:
51
  print("No frames selected.")
52
  return
53
 
54
+ clip = ImageSequenceClip(frames, fps=fps)
55
+ clip.write_videofile(output_path, codec="libx264")