paynestrike
paynestrike

Reputation: 4658

how to generate video thumbnail in node.js?

I am building an app with node.js, I successfully uploaded the video, but I need to generate a video thumbnail for it. Currently I use node exec to execute a system command of ffmpeg to make the thumbnail.

   exec("C:/ffmpeg/bin/ffmpeg -i Video/" + Name  + " -ss 00:01:00.00 -r 1 -an -vframes 1 -f mjpeg Video/" + Name  + ".jpg")

This code is coming from a tutorial from http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-resumable-video-uploade-in-node-js/

the code above did generate a jpg file but it's not a thumbnail but a video screen shot, I wonder is there any other method to generate video thumbnail, or how to exec the ffmpeg command to make a real thumbnail (resized), and I prefer png file.

Upvotes: 17

Views: 58619

Answers (8)

Marco Bertelli
Marco Bertelli

Reputation: 425

2024 update:

function getVideoPreviewImage(file) {
    return new Promise((resolve, reject) => {
        let fileNames = [];

        ffmpeg(file.path)
            .on('filenames', function (filenames) {
                console.log('screenshots are ' + filenames.join(', '));

                fileNames = filenames;
            })
            .on('end', function () {
                resolve(`${os.tmpdir()}/${fileNames[0]}`);
            })
            .on('error', function (err) {
                reject(err);
            })
            .takeScreenshots({ count: 1, timemarks: ['00:00:02.000'], size: '1280x720', folder: os.tmpdir()});
    });
}

Upvotes: 0

Abhi Ghadge
Abhi Ghadge

Reputation: 1

import * as thumbsupply from 'thumbsupply'; 
     const thumbNail = await thumbsupply.default.generateThumbnail("video(720p).mp4", {
        size: thumbsupply.default.ThumbSize.LARGE,
        timestamp: '10%',
        forceCreate: true,
        mimetype: 'video/mp4',
      });

Upvotes: 0

vishal sharma
vishal sharma

Reputation: 49

app.post('/convert', upload.any(), (req, res) => {
    console.log("calling", req.files)
    let thumbNailName = req.files[0].filename.split('.')
    var gm = require('gm');

    gm('./src/Upload/'+req.files[0].filename)// get pdf file from storage folder
    .thumb(
        50, // Width
        50, // Height
        './src/thumbnail/'+thumbNailName[0]+'.png', // Output file name
        80, // Quality from 0 to 100
        function (error, stdout, stderr, command) {
            if (!error) {
                console.log("processing");
            } else {
                console.log("error")
            }
        }
    );
})

Upvotes: -2

ryanafrish7
ryanafrish7

Reputation: 3410

Instead I would recommend using thumbsupply. In addition to provide you with thumbnails, it caches them to improve performance significantly.

npm install --save thumbsupply

After installing the module, you can use it in a following way.

const thumbsupply = require('thumbsupply')("com.example.application");

thumbsupply.generateThumbnail('some-video.mp4')
.then(thumb => {
    // serve thumbnail
})

Upvotes: -1

Rajat Saxena
Rajat Saxena

Reputation: 3915

Using media-thumbnail, you can easily generate thumbnails from your videos. The module basically wraps the ffmpeg thumbnail functionality.

const mt = require('media-thumbnail')

mt.forVideo(
  './path/to/video.mp4',
  './path/to/thumbnail.png', {
    width: 200
  })
  .then(() => console.log('Success'), err => console.error(err)) 

You can also create thumbnails from your images using this package.

Upvotes: -1

Risadinha
Risadinha

Reputation: 16666

Reference to GitHub fluent-ffmpeg project.

Repeating example from original StackOverflow answer:

var proc = new ffmpeg('/path/to/your_movie.avi')
  .takeScreenshots({
      count: 1,
      timemarks: [ '600' ] // number of seconds
    }, '/path/to/thumbnail/folder', function(err) {
    console.log('screenshots were saved')
  });

Upvotes: 20

Matt Palmerlee
Matt Palmerlee

Reputation: 2668

There is a node module for this: video-thumb

It basically just wraps a call to exec ffmpeg

Upvotes: 2

av501
av501

Reputation: 6729

Resize by adding a -s widthxheight option to your command.

Upvotes: 2

Related Questions