NodeJS to implement sample code for video transfer

Video transfer is a process of first decoding, re-encoding, thereby converting the original video into the video we need. This conversion may include various types (MP4 / Avi / FLV, etc.), resolution, code rate, frame rate, and the like.

Traditional video transcodes, very common tools are FFMPEG. Ffmpeg is an open source computer program that can be used to record, convert digital audio, video, and convert it into streams.

In NodeJS, there is also FFMPEG called Node-flunt-ffmpeg. Node-fluent-ffmpeg is a Node available for FFMPEG, and FFMPEG must be installed before use.


FFMPEG is an open source computer that can be used to record, convert digital audio, video, and convert it into streams program. Using LGPL or GPL license. It provides a complete solution for recording, conversion, and streaming video. It contains very advanced audio / video codec libavcodec. In order to ensure high-portability and codec quality, many CODEs in libavcodec are developed from head , let’s introduce NodeJs to implement video. Method of code.

First, install FFMPEG (Mac)

Under Mac, the BREW command can be installed directly: BREW Install Ffmpeg.

After the installation is successful, we can perform video transcoding by using ffmpeg commands. For example, input.avi transcathes to Output.ts and sets the video code rate 640kbps, the commands that need to be executed are as follows:

FFMPEG -I INPUT.AVI -B : v 640k Output.ts

Node-fluent-FFMPEG module Basic method
 Node-flunt-ffmpeg is actually inherently commands that ffmpeg executed in the terminal through NodeJs. The installation and basic introduction of Node-Fluent-Ffmpeg are the same as other NPM modules. After the NPM installation (NPM install fluent-ffmpeg), use Require to reference in the JS file. Based on the following:   
VAR ffmpeg = Require (‘flunt-ffmpeg’); var command = new ffmpeg (here you can pass to video file path, or flow);

This will instantiate a command line, which requires the transfer of video files to be transferred, and also supports the data stream.
Third, using example

The following is an example of using node-fluent-ffmpeg for video transfer:
VAR FFMPEG = Require (‘../ index’); var command = ffmpeg (‘testvideo-5m.mpg’) // set video bitrate .VideObitrate (1024) // set aspect ratio .aspect (’16: 9 ‘) // set size in percent .size (‘50%’) // set fps .fps (24) // set audio bitrate .audiobitrate (‘128k’) // set audio Codec.AudioCodec (‘libmp3lame’) // set number of audio channels .audiochannels (2) // set custom option .addoption (‘- vtag’, ‘divx’) // set output format to force .format (‘avi’) / / setup Event Handlers .on (‘end’, function () {console.log (‘file has been control “;}) .on (‘ error ‘, function (err) {console.log (‘ An error happened : ‘+ err.Message);}) // Save to file .save (‘ outtest.avi ‘);
Node-fluent-ffmpeg can be chained After instantiation, the chain sets a series of video properties, and finally listens the END event, indicating that the video has been transferred. After completing, you can call the save () method to store the video file to the appropriate directory.

What can do

Node-fluent-ffmpeg can do anything that FFmpeg can do, anything to the video deal with. In addition, with nodejs, Node-fluent-ffmpeg can also do many other things, such as reading and dynamically returning video length, and transfer video into any format as needed. Specific various methods can be viewed on Github, which is described above. When you use it, you can easily complete the video transfer.

V. Some elements of video transcoding
 The above functions set the individual properties of the video, and the following is simple to introduce some important attributes in video transcodes. And methods in the Node-fluent-ffmpeg they correspond to their corresponding.   1.FPS: That is, the frame rate of the video, indicates how many times the picture refresher in 1s is. When you play games, you will be clear if the FPS is low, this is the role of FPS. The higher the FPS is, the higher the number of times the video 1s refreshes, the more smooth it in the human eye. The method corresponding to this property is FPS (), which is incoming the frame rate value to be set. 
2. Resolution: The size of the video is used to set the video, the corresponding method is Size (), incoming width X height.

3. Code rate: In the field of video, the quotation rate refers to the bit rate. He indicates the number of data digits transmitted when the data transmission is transmitted, and the unit kbps. This value affects the quality of the video. The greater the value, the higher the quality of the video, but at the same time, the size of the video file will be larger. Therefore, we must set the code rate at the time of transcoding, and the quality of the file size does not exceed the limitations as high as possible.

4. Format: This is not to say more, you can set the format through the Format method.
The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment