import express from "express" import ffmpeg from "fluent-ffmpeg" import {PassThrough} from "stream" const app = express() const PORT = process.env.PORT || 3100 app.get("/thumbnail", async (req, res) => { const videoUrl = req.query.url if (!videoUrl) { return res.status(400).json({error: "Missing 'url' query parameter"}) } try { const passthrough = new PassThrough() ffmpeg(videoUrl) .seekInput(1) .frames(1) .format("image2") .outputOptions("-vcodec", "mjpeg") .on("error", err => { if (!res.headersSent) { res.status(500).json({error: "Failed to generate thumbnail"}) } }) .pipe(passthrough, {end: true}) res.setHeader("Content-Type", "image/jpeg") res.setHeader("Cache-Control", "public, max-age=86400") passthrough.pipe(res) } catch { res.status(500).json({error: "Failed to process video"}) } }) app.get("/health", (_req, res) => { res.json({status: "ok"}) }) app.listen(PORT, () => { console.log(`Video thumbnail service running on port ${PORT}`) })