Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

405
Views
How to extract frames from the In-memory mp4 file (bytes buffer) in Python?

Now, my solution extracts images with saving bytes on disk using OpenCV/FFmpeg:

from fastapi import APIRouter, UploadFile, File

@router.post("/")
def extract_stream_frames(stream: UploadFile = File(...)):
    temp_filename = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()) + ".mp4")
    # print(stream.content_type)
    local_temp_file = open(temp_filename, "wb")
    local_temp_file.write(stream.file.read())
    local_temp_file.close()
    try:
        images = video.sampling(local_temp_file.name, num_frames=Config.IMAGES_PER_STREAM)
        images = list(map(lambda array: image.preprocess(array, Config.IMAGE_HEIGHT, Config.IMAGE_WIDTH), images))
        result = classify(images=images)
    finally:
        os.remove(local_temp_file.name)
    return result

Frames extracting function:

def sampling(video_path: str, num_frames: int):
    cam = cv2.VideoCapture(video_path)
    images = []
    while True:
        ret, frame = cam.read()
        if ret:
            images.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        else:
            break
    cam.release()
    cv2.destroyAllWindows()
    total_frames = len(images)
    images = [images[frame_no] for frame_no in range(0, total_frames, (total_frames + num_frames -1) // num_frames)]

    return images

Is there a any solution that doesn't require saving stream on disk?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!