Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

406
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda