How to capture pictures in nodejs
这篇文章主要为大家详细介绍了nodejs实现截取上传视频中一帧作为预览图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
客户有个上传视频的需求,上传的视频呢,需要能在线播放并且列表中必须出现类似优酷等视频首页上的那种缩略图,成品如下图所示:
当然了,上传视频的界面就不贴出来了,毕竟我们这篇文章的重点不在于如何上传,而在于如何用nodejs截取视频中的帧!~
这里我们需要一个开源的第三方插件----大名鼎鼎的多媒体编解码框架ffmpeg,需要安装在服务器上由nodejs调用,
代码贴出如下:
function fecthVideoThumbnail(entryid, index){ var filename = path.join(imageDir, entryid, index + videoSuffix); var thumb = path.join(imageDir, entryid, "overview",index + thumbSuffix); var thumbPath = path.join(imageDir, entryid, "overview"); if (!fs.existsSync(thumbPath)) { <span style="white-space:pre"> </span>fs.mkdirSync(thumbPath); <span style="white-space:pre"> </span>} var that = this; filename = filename.replaceAll("\\\\","\\\\"); var cmdthumb = thumb.replaceAll("\\\\","\\\\"); if(!fs.existsSync(thumb)){ exec("ffmpeg -ss 00:00:10 -i "+filename+" -y -f image2 -t 0.001 "+cmdthumb+"", function() { console.log(arguments[0]); console.log('success'); readFileEntry(thumb,that.res); }); }else{ readFileEntry(thumb,that.res); } }
function readFileEntry(filename, response) { path.exists(filename, function(exists) { if (!filename || !exists) { response.writeHead(404); response.end(); return; } fs.readFile(filename, "binary", function(err, file) { if (err) { response.writeHead(404); response.end(); return; } var contentType = 'none'; var ext = path.extname(filename); switch (ext) { case ".xml": contentType = 'application/xml;charset=utf-8'; break; case ".json": contentType = 'application/json;charset=utf-8'; break; case ".png": contentType = 'image/png'; break; case ".jpg": contentType = 'image/jpeg'; break; case ".flv": contentType = "video/flv"; break; } response.writeHead(200, { 'Content-Type' : contentType, 'Content-Length' : file.length, 'Accept-Ranges' : 'bytes', 'Server' : 'Microsoft-IIS/7.5', 'X-Powered-By' : 'ASP.NET' }); response.write(file, "binary"); response.end(); }); }); }
重点就是这段
exec("ffmpeg -ss 00:00:10 -i "+filename+" -y -f image2 -t 0.001 "+cmdthumb+"", function() { console.log(arguments[0]); console.log('success'); readFileEntry(thumb,that.res); });
exec函数可以像cmd DOS命令台一样直接执行系统命令,ffmpeg提供的正是这样的接口。具体的API可以参照ffmpeg的文档,-ss代表指定视频初始进度,-i代表入参视频文件位置,-y代表Overwrite output files without asking.直接覆盖已存在文件而不必询问,-t代表截取时长(图片的话0.001即可),-f代表
-f fmt (input/output)
Force input or output file format. The format is normally auto detected for input files and guessed from the file extension for output files, so this option is not needed in most cases.
强制输出文件格式,基本上用不到……最后cmdthumb代表输出文件名。nodejs的exec执行完成之后,会通知回调函数。此时返回生成的缩略图即可,将此过程写成rest服务,直接将url填充在img标签的src属性中即可!
nodejs写这种服务端程序非常简单,方便,轻量。比java要简洁得多,并且不需要像tomcat那么麻烦。唯一的缺点可能就是调试比较麻烦了……
另外,上图中所示的视频服务我也是用nodejs实现的,效率还不错~
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
如何把vue2.0 和 animate.css合并在一起使用(详细教程)
The above is the detailed content of How to capture pictures in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
