jschwab21 commited on
Commit
5c99594
·
verified ·
1 Parent(s): 9bb1123

Update video_processing.py

Browse files
Files changed (1) hide show
  1. video_processing.py +23 -12
video_processing.py CHANGED
@@ -32,14 +32,23 @@ def sanitize_filename(filename):
32
  return "".join([c if c.isalnum() or c in " .-_()" else "_" for c in filename])
33
 
34
  def ensure_video_format(video_path):
35
- temp_path = f"temp_videos/formatted_{uuid.uuid4()}.mp4"
 
 
36
  command = ['ffmpeg', '-i', video_path, '-c', 'copy', temp_path]
37
- subprocess.run(command, check=True)
38
- return temp_path
 
 
 
 
39
 
40
  def find_scenes(video_path):
41
- video_path = ensure_video_format(video_path)
42
- video_manager = open_video(video_path)
 
 
 
43
  scene_manager = SceneManager()
44
  scene_manager.add_detector(ContentDetector(threshold=30.0))
45
  scene_manager.detect_scenes(video_manager)
@@ -47,6 +56,7 @@ def find_scenes(video_path):
47
  scenes = [(scene[0].get_seconds(), scene[1].get_seconds()) for scene in scene_list]
48
  return scenes
49
 
 
50
  def convert_timestamp_to_seconds(timestamp):
51
  return float(timestamp)
52
 
@@ -116,15 +126,16 @@ def extract_best_scene(video_path, scene):
116
  return video_clip
117
 
118
  def process_video(video_input, description, is_url=True):
119
- if is_url:
120
- video_path = download_video(video_input)
121
- else:
122
- video_path = video_input
123
-
124
  scenes = find_scenes(video_path)
 
 
 
125
  best_scene = analyze_scenes(video_path, scenes, description)
 
 
 
126
  final_clip = extract_best_scene(video_path, best_scene)
127
-
128
  if final_clip:
129
  output_dir = "output"
130
  os.makedirs(output_dir, exist_ok=True)
@@ -143,4 +154,4 @@ def cleanup_temp_files():
143
  if os.path.isfile(file_path):
144
  os.unlink(file_path)
145
  except Exception as e:
146
- print(f"Error: {e}")
 
32
  return "".join([c if c.isalnum() or c in " .-_()" else "_" for c in filename])
33
 
34
  def ensure_video_format(video_path):
35
+ output_dir = "temp_videos"
36
+ os.makedirs(output_dir, exist_ok=True)
37
+ temp_path = os.path.join(output_dir, f"formatted_{uuid.uuid4()}.mp4")
38
  command = ['ffmpeg', '-i', video_path, '-c', 'copy', temp_path]
39
+ try:
40
+ subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
41
+ return temp_path
42
+ except subprocess.CalledProcessError as e:
43
+ print(f"Error processing video with ffmpeg: {e.stderr.decode()}")
44
+ return None
45
 
46
  def find_scenes(video_path):
47
+ formatted_video_path = ensure_video_format(video_path)
48
+ if formatted_video_path is None:
49
+ print("Video formatting failed. Exiting scene detection.")
50
+ return []
51
+ video_manager = open_video(formatted_video_path)
52
  scene_manager = SceneManager()
53
  scene_manager.add_detector(ContentDetector(threshold=30.0))
54
  scene_manager.detect_scenes(video_manager)
 
56
  scenes = [(scene[0].get_seconds(), scene[1].get_seconds()) for scene in scene_list]
57
  return scenes
58
 
59
+
60
  def convert_timestamp_to_seconds(timestamp):
61
  return float(timestamp)
62
 
 
126
  return video_clip
127
 
128
  def process_video(video_input, description, is_url=True):
129
+ video_path = download_video(video_input) if is_url else video_input
 
 
 
 
130
  scenes = find_scenes(video_path)
131
+ if not scenes:
132
+ print("No scenes detected. Exiting.")
133
+ return None
134
  best_scene = analyze_scenes(video_path, scenes, description)
135
+ if not best_scene:
136
+ print("No suitable scenes found. Exiting.")
137
+ return None
138
  final_clip = extract_best_scene(video_path, best_scene)
 
139
  if final_clip:
140
  output_dir = "output"
141
  os.makedirs(output_dir, exist_ok=True)
 
154
  if os.path.isfile(file_path):
155
  os.unlink(file_path)
156
  except Exception as e:
157
+ print(f"Error cleaning up temporary files: {e}")