Video transcoding is a process of decoding first and then 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, etc.
In traditional video transcoding, a very common tool is FFmpeg. FFmpeg is an open source computer program that can be used to record, convert digital audio, video, and convert them into streams.
In nodejs, there is also FFmpeg, called node-fluent-ffmpeg. node-fluent-ffmpeg is a ffmpeg available on a node. ffmpeg must be installed before use.
ffmpeg
FFmpeg is an open source computer program that can be used to record, convert digital audio, video, and convert them into streams. Use an LGPL or GPL license. It provides a complete solution for recording, converting and streaming audio and video. It contains a very advanced audio/video codec library, libavcodec. In order to ensure high portability and codec quality, many codes in libavcodec are developed from scratch
Let me introduce to you how to implement video transcoding by NodeJS.
1. Install FFmpeg (under MAC)
Under MAC, you can directly use the brew command to install: brew install ffmpeg.
After the installation is successful, we can perform it on the command line and use the ffmpeg command to transcode the video. For example, if the video code rate is converted to and set to 640kbps, the commands that need to be executed are as follows:
ffmpeg -i -b:v 640k
2. Basic usage method of node-fluent-ffmpeg module
node-fluent-ffmpeg is essentially executing FFmpeg commands in the terminal through nodejs. The installation and basic introduction of node-fluent-ffmpeg are the same as other npm modules. After installing through npm (npm install fluent-ffmpeg), use require to reference it in the js file. Basic use is as follows:
var ffmpeg = require(‘fluent-ffmpeg'); var command = new ffmpeg(Here you can pass the video file path,Or stream);
This instantiates a command line, which needs to be passed in the video file to be transcoded, and also supports the form of data streams.
3. Examples of use
Here is an example of video transcoding using node-fluent-ffmpeg:
var ffmpeg = require('../index'); var command = ffmpeg('') // 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() { ('file has been converted succesfully'); }) .on('error', function(err) { ('an error happened: ' + ); }) // save to file .save('');
node-fluent-ffmpeg can be called in a chain. After instantiation, the chain sets a series of video attributes and finally listens for the end event, indicating that the video has been transcoded. After completion, you can call the save() method to store the video file to the corresponding directory.
4. What can node-fluent-ffmpeg do
node-fluent-ffmpeg can do anything ffmpeg can do and do anything to the video. In addition, with nodejs, node-fluent-ffmpeg can do many other things, such as reading and dynamically returning the video length, transcoding the video into any format according to needs, etc. You can check the specific methods on github, which has an introduction to the use of methods. When using it, you can easily complete the video transcoding by yourself.
5. Some elements of video transcoding
The above functions set the various attributes of the video. Here are some of the more important attributes in video transcoding and their corresponding methods in node-fluent-ffmpeg.
: That is the frame rate of the video, indicating how many times the picture is refreshed in 1s. When playing games, you know that if the fps are low, the game will be very stuck. This is what fps does. The higher the fps within a certain range, the higher the number of refreshes of the video in 1s and the smoother it will be displayed in people's eyes. The corresponding method of this property is fps(), and you can just pass in the frame rate value you want to set.
2. Resolution: used to set the size of the video. The corresponding method is size(), passing in width x height.
3. Bit rate: In the video field, the bit rate generally refers to the bit rate. It indicates the number of data bits transmitted per unit time during data transmission, unit kbps. This value affects the video quality. The larger the value, the higher the video quality, but at the same time, the larger the size of the video file will be. Therefore, when transcoding, we must set the code rate reasonably to ensure that the file size does not exceed the limit as high as possible.
4. Format: No need to say more about this, you can set the format through the format method.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.