Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -145,31 +145,44 @@ def get_video_metadata(temp_video_path: str) -> dict:
145
  return {"duration": 0, "has_duration": False}
146
 
147
  def repair_video(temp_video_path: str) -> str:
148
- """Repair the video only if metadata is invalid"""
 
149
  metadata = get_video_metadata(temp_video_path)
150
- if metadata["has_duration"]:
151
- logger.info("Video metadata is valid, skipping repair")
 
152
  return temp_video_path
 
153
 
154
  try:
155
  repaired_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".webm").name
156
- repair_command = f"ffmpeg -i {temp_video_path} -c:v libvpx -c:a libopus -y {repaired_video_path}"
157
- logger.info(f"Attempting to repair video: {repair_command}")
 
 
 
 
 
 
158
  result = subprocess.run(
159
  repair_command,
160
- shell=True,
161
  capture_output=True,
162
  text=True,
163
  check=True
164
  )
165
  logger.info(f"Video repair output: {result.stdout}")
166
- logger.info(f"Video repair stderr: {result.stderr}")
167
- logger.info(f"Video repaired and saved to: {repaired_video_path}")
168
- return repaired_video_path
 
 
 
 
 
 
169
  except subprocess.CalledProcessError as e:
170
- logger.error(f"Failed to repair video: {e}")
171
- logger.error(f"Repair stdout: {e.stdout}")
172
- logger.error(f"Repair stderr: {e.stderr}")
173
  return temp_video_path
174
  except Exception as e:
175
  logger.error(f"Unexpected error during video repair: {e}")
 
145
  return {"duration": 0, "has_duration": False}
146
 
147
  def repair_video(temp_video_path: str) -> str:
148
+ """Repair the video only if metadata is invalid or video is not playable"""
149
+ cap = cv2.VideoCapture(temp_video_path)
150
  metadata = get_video_metadata(temp_video_path)
151
+ if cap.isOpened() and metadata["has_duration"]:
152
+ cap.release()
153
+ logger.info("Video is playable and has valid metadata, skipping repair")
154
  return temp_video_path
155
+ cap.release()
156
 
157
  try:
158
  repaired_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".webm").name
159
+ repair_command = [
160
+ 'ffmpeg',
161
+ '-i', temp_video_path,
162
+ '-c:v', 'libvpx',
163
+ '-c:a', 'libopus',
164
+ '-y', repaired_video_path
165
+ ]
166
+ logger.info(f"Attempting to repair video with command: {' '.join(repair_command)}")
167
  result = subprocess.run(
168
  repair_command,
 
169
  capture_output=True,
170
  text=True,
171
  check=True
172
  )
173
  logger.info(f"Video repair output: {result.stdout}")
174
+ new_metadata = get_video_metadata(repaired_video_path)
175
+ if new_metadata["has_duration"]:
176
+ logger.info(f"Video repaired successfully, saved to: {repaired_video_path}")
177
+ os.unlink(temp_video_path)
178
+ return repaired_video_path
179
+ else:
180
+ logger.warning("Repair failed to fix metadata, using original video")
181
+ os.unlink(repaired_video_path)
182
+ return temp_video_path
183
  except subprocess.CalledProcessError as e:
184
+ logger.error(f"Failed to repair video: {e.stderr}")
185
+ os.unlink(repaired_video_path) if os.path.exists(repaired_video_path) else None
 
186
  return temp_video_path
187
  except Exception as e:
188
  logger.error(f"Unexpected error during video repair: {e}")