UNPKG

video-thumbnails

Version:

Get thumbnail images from any public video URL.

65 lines (64 loc) 2.98 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVideoThumbnail = void 0; const ffmpegInstaller = require("@ffmpeg-installer/ffmpeg"); const ffprobeInstaller = require("@ffprobe-installer/ffprobe"); const ffmpeg = require("fluent-ffmpeg"); ffmpeg.setFfmpegPath(ffmpegInstaller.path); ffmpeg.setFfprobePath(ffprobeInstaller.path); /** * Get a video thumbnail image via a public video URL. * * @param url - a public video URL * @param coverTime - the seconds mark to take the screenshot (higher = slower) */ function getVideoThumbnail(url, coverTime = 0) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { // format the cover time let coverTimeNew = '00:00:01'; if (coverTime) { coverTimeNew = new Date(coverTime * 1000).toISOString().substring(11, 22); } try { const command = ffmpeg(url) .seekInput(coverTimeNew) .outputOptions(['-f image2', '-vcodec png', '-f rawvideo']) .noAudio() .frames(1) .size('500x?') .on('error', () => { resolve({ ok: false, error: 'Error getting video thumbnail.' }); }); const ffstream = command.pipe(); const buffers = []; ffstream.on('data', (chunk) => { buffers.push(chunk); }); ffstream.on('end', () => __awaiter(this, void 0, void 0, function* () { const outputBuffer = Buffer.concat(buffers); if (buffers.length) { resolve({ ok: true, buffer: outputBuffer }); } else { resolve({ ok: false, error: 'No buffers found while getting video thumbnail.' }); } })); } catch (error) { console.log('Error:', error); resolve({ ok: false, error: 'Error getting video thumbnail (2).' }); } })); }); } exports.getVideoThumbnail = getVideoThumbnail;