Skip to content

Instantly share code, notes, and snippets.

@spencermountain
Last active June 21, 2023 17:24
Show Gist options
  • Save spencermountain/e2fdd96c94f953a17907ff8cecbf5e4b to your computer and use it in GitHub Desktop.
Save spencermountain/e2fdd96c94f953a17907ff8cecbf5e4b to your computer and use it in GitHub Desktop.
Video thumbnails on-the-fly with ffmpeg+express
<!-- view random screenshot of a video, without any javascript -->
<img src="http://localhost/api/thumb?id=foobar.mp4">
import express from 'express'
import ffmpeg from 'fluent-ffmpeg'
const app = express()
// create video thumbnails on-the-fly
app.get('/api/thumbnail/:file', function (req, res) {
let s = Math.random() * 600 // choose a random time (in seconds)
res.contentType('jpeg') // expect jpg response
// ok, do it
ffmpeg(req.query.id)
.seekInput(s) // don't load the whole video
.frames(1)
.format('mjpeg')
.size('400x?') //resize to max width
.pipe(res, { end: true }) //save to the input stream
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment