'm trying to extract frames from a video using OpenCV and Python, but hitting a wall with a moov atom not found
error. This error is preventing cv2.VideoCapture
from correctly opening the video file, resulting in an empty array of frames.
Here’s the weird part: this exact workflow, using the same video file (which has been validated and is a valid MP4), worked perfectly fine yesterday. The issue started occurring after a PC restart.
FFmpeg is installed and confirmed to be working on the system.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2a78480] moov atom not found
import cv2
import numpy as np
import base64
def extract_evenly_distributed_frames_from_base64(base64_string, max_frames=90):
# Decode the Base64 string into bytes
video_bytes = base64.b64decode(base64_string)
# Write the bytes to a temporary file
video_path = '/tmp/temp_video.mp4'
with open(video_path, 'wb') as video_file:
video_file.write(video_bytes)
# Open the video file using OpenCV
video_capture = cv2.VideoCapture(video_path)
# Get the total number of frames in the video
total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
# Calculate the step size to take 'max_frames' evenly distributed frames
step_size = max(1, total_frames // (max_frames - 1))
# List to store selected frames as base64
selected_frames_base64 = []
for i in range(0, total_frames, step_size):
# Set the current frame position
video_capture.set(cv2.CAP_PROP_POS_FRAMES, i)
# Read the frame
ret, frame = video_capture.read()
if ret:
# Convert frame (NumPy array) to a Base64 string
frame_base64 = convert_frame_to_base64(frame)
selected_frames_base64.append(frame_base64)
if len(selected_frames_base64) >= max_frames:
break
# Release the video capture object
video_capture.release()
return selected_frames_base64
def convert_frame_to_base64(frame):
# Convert the frame (NumPy array) to JPEG format
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
return None
# Encode JPEG image to Base64
frame_base64 = base64.b64encode(buffer).decode('utf-8')
return frame_base64
base64_video = _input.item.binary.data.data
frames_base64 = extract_evenly_distributed_frames_from_base64(base64_video, max_frames=90)
return { "output": frames_base64 }
Would appreciate help on this