In this tutorial, you will learn how to generate thumbnails from a video file.
Warning!!! Please use small video files only, if your machine is low-end.
We will primarily use the fluent-ffmpeg library to perform the action.
For this, download the ffmpeg library from the official site of ffmpeg https://ffmpeg.org/.
Extract the zip and add the bin folder to your environment variables path.
Steps
-
Create a folder for the conversion, name it as
video-thumbnail-generator. -
Initialize it as a Node.js package.
npm init -y -
Install the fluent-ffmpeg library
npm install fluent-ffmpeg -
Import the library
const ffmpeg = require('fluent-ffmpeg') -
The first argument of ffmpeg will be the path to the video file
ffmpeg('path_to_videofile') -
Take a screenshot and save it to the folder
ffmpeg('path_to_videofile')
.screenshots({
count: 1,
filename: 'thumbnail-%s.png',
folder: 'output_path/'
})
Entire Code
const ffmpeg = require('fluent-ffmpeg')
ffmpeg('path_to_videofile')
.on('filenames', (filenames) => {
console.log(filenames.join(', '))
})
.on('end', () => {
console.log('Screenshots taken!')
})
.screenshots({
count: 1,
filename: 'thumbnail-%s.png',
folder: 'output_path/'
})
