Add in-memory caching to /thumbnail endpoint to prevent repeated ffmpeg processing #2

Closed
opened 2026-04-14 14:50:38 +00:00 by priyanshu_bharti · 0 comments
Contributor

Problem

Currently, the /thumbnail endpoint in vthumbs re-processes the same video every time using ffmpeg. Even if the same video URL is requested multiple times, it downloads and processes the video repeatedly. This increases CPU usage and server load unnecessarily.

As discussed in Flotilla's Issue #131 and PR #142, we need to add caching to avoid this.

Proposed Solution

  • Add in-memory caching for generated thumbnails
  • Cache key: videoUrl (query parameter)
  • Cache expiration: 1 hour (3600 seconds) as suggested by @hodlbod
  • Use node-cache package (lightweight and simple)
  • On cache hit → return the JPEG buffer directly (no ffmpeg)
  • On cache miss → generate thumbnail with ffmpeg and store in cache
  • Keep existing HTTP Cache-Control header for browsers/CDNs

Additional Improvements (Optional)

  • Add thumbnail quality control using -qscale:v to reduce memory usage (around 65% quality)
  • Proper logging for cache hit/miss

Implementation Plan

  1. Install node-cache dependency
  2. Modify /thumbnail route in index.js
  3. Store generated image as Buffer in cache
  4. Update README if needed

Once this is merged, the thumbnail service will be much more efficient, especially for repeated videos.

cc @hodlbod


### Problem Currently, the `/thumbnail` endpoint in vthumbs re-processes the **same video** every time using `ffmpeg`. Even if the same video URL is requested multiple times, it downloads and processes the video repeatedly. This increases CPU usage and server load unnecessarily. As discussed in Flotilla's Issue #131 and PR #142, we need to add caching to avoid this. ### Proposed Solution - Add **in-memory caching** for generated thumbnails - Cache key: `videoUrl` (query parameter) - Cache expiration: **1 hour** (3600 seconds) as suggested by @hodlbod - Use `node-cache` package (lightweight and simple) - On cache hit → return the JPEG buffer directly (no ffmpeg) - On cache miss → generate thumbnail with ffmpeg and store in cache - Keep existing HTTP `Cache-Control` header for browsers/CDNs ### Additional Improvements (Optional) - Add thumbnail quality control using `-qscale:v` to reduce memory usage (around 65% quality) - Proper logging for cache hit/miss ### Implementation Plan 1. Install `node-cache` dependency 2. Modify `/thumbnail` route in `index.js` 3. Store generated image as Buffer in cache 4. Update README if needed Once this is merged, the thumbnail service will be much more efficient, especially for repeated videos. cc @hodlbod ---
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: coracle/vthumbs#2