Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Extract frames from a video URL in Python without downloading the file

The usual way to get thumbnails from a video is to download it and run ffmpeg -i video.mp4 -vf fps=1/10 out_%03d.jpg . That decodes every frame of the file to keep one in three hundred. For ten thumbnails from an hour-long recording sitting on S3, it is a lot of bandwidth and CPU for nothing. PyAV binds the FFmpeg libraries directly, so you can do what a video player does when you drag the seek…

The provided Python code demonstrates how to extract evenly spaced frames from a video file hosted on a URL without downloading the entire file. It uses the PyAV library, which directly binds the FFmpeg libraries, allowing the code to seek near the desired moment and decode only from there. This approach saves bandwidth and CPU resources compared to downloading the entire video.

Key steps in the code include:

1. Defining a function `open_video(url)` that opens the video container from the given URL using PyAV. It retrieves the video stream and sets threading for faster processing.

2. Creating a function `frames_at(url, times)` that yields a tuple of seconds and a PIL (Python Imaging Library) image for each requested time. It seeks to the appropriate time in the video stream, skips frames with None pts, and converts the frame to an image.

3. Implementing a function `evenly_spaced(url, n)` that calculates the step size between evenly spaced frames based on the total duration of the video and the desired number of frames. It returns a list of frame times.

4. Opening the video container using `open_video(url)` and calculating the evenly spaced frame times using `evenly_spaced(url, 5)`.

5. Iterating through the frame times and saving each frame as an image file with the corresponding timestamp.

6. The `scene_cuts(url, threshold=0.075, min_gap=1.0)` function is also defined but not used in the main code snippet. It yields the start time, score, and image for each detected scene cut in the video based on a given threshold and minimum gap between cuts.

The code efficiently extracts frames from a video without downloading the entire file, utilizing PyAV's capabilities to jump to specific times and decode only the necessary frames. This technique is particularly useful when dealing with large video files, as it minimizes bandwidth usage and computational resources.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

More from Dev.to on urgent.news

Report a problem with this story

Get the day's news by email. Daily, weekly or monthly. Sign up

More in Tech

Parametric CNC Plasma Flat-Pack Fire Pits in TypeScript: 3D Frustum Dihedral Slots, 4x8 Plate Nesting & .TAP CAM

Flat-pack interlocking steel fire pits are one of the best engineering exercises in computational sheet-metal geometry — and one of the highest-margin jobs for CNC plasma shops running Langmuir…

  • Precise 3D geometry calculates steel plate usage for fire pits
  • 4x8 steel plate nesting maximizes material efficiency
  • .TAP CAM generates toolpaths for efficient plasma cutting

PostgreSQL slow queries: a practical checklist before adding a partial index

A query can look index-friendly and still be slow in production. Before suggesting CREATE INDEX , I prefer to record a small set of facts: what the planner estimates, what actually executes, how…

  • Examine real query plans with EXPLAIN (ANALYZE, BUFFERS) in a secure environment
  • Consider tenant skew and predicate selectivity, test representative workloads
  • Evaluate index coverage, impact of additional writes, locks, and maintenance

More from Wednesday, 2 December 2026 →