Last active
June 21, 2023 17:24
-
-
Save spencermountain/e2fdd96c94f953a17907ff8cecbf5e4b to your computer and use it in GitHub Desktop.
Video thumbnails on-the-fly with ffmpeg+express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- view random screenshot of a video, without any javascript --> | |
<img src="http://localhost/api/thumb?id=foobar.mp4"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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