如何在 node.js 中生成视频缩略图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13079742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to generate video thumbnail in node.js?
提问by paynestrike
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.
我正在用 node.js 构建一个应用程序,我成功上传了视频,但我需要为它生成一个视频缩略图。目前我使用 node exec 执行 ffmpeg 的系统命令来制作缩略图。
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/
此代码来自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.
上面的代码确实生成了一个 jpg 文件,但它不是缩略图而是视频屏幕截图,我想知道有没有其他方法可以生成视频缩略图,或者如何执行 ffmpeg 命令来制作真正的缩略图(调整大小),我更喜欢.png 文件。
采纳答案by av501
Resize by adding a -s widthxheight option to your command.
通过向命令添加 -s widthxheight 选项来调整大小。
回答by Risadinha
Reference to GitHub fluent-ffmpeg project.
Repeating example from original StackOverflow answer:
来自原始 StackOverflow 答案的重复示例:
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')
});
回答by Matt Palmerlee
There is a node module for this: video-thumb
有一个节点模块: video-thumb
It basically just wraps a call to exec ffmpeg
它基本上只是包装了对 exec ffmpeg 的调用
回答by Daniel Stejskal
I recommend using https://www.npmjs.com/package/fluent-ffmpegto call ffmpeg from Node.js
我建议使用https://www.npmjs.com/package/fluent-ffmpeg从 Node.js 调用 ffmpeg
回答by ryanafrish7
Instead I would recommend using thumbsupply. In addition to provide you with thumbnails, it caches them to improve performance significantly.
相反,我建议使用thumbsupply。除了为您提供缩略图,它还缓存它们以显着提高性能。
npm install --save thumbsuppply
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
})
回答by Rajat Saxena
Using media-thumbnail, you can easily generate thumbnails from your videos. The module basically wraps the ffmpeg thumbnail functionality.
使用media-thumbnail,您可以轻松地从视频中生成缩略图。该模块基本上包装了 ffmpeg 缩略图功能。
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.
您还可以使用此包从您的图像创建缩略图。

