FFmpeg is free software that can record, convert, and stream audio and video in a variety of formats. It includes libavCodec, an audio and video decoder library for multiple projects, and libavFormat, an audio and video conversion library. The “FF” in the word “FFmpeg” means “Fast Forward”. The FFmpeg plug-in provides a set of functions and utilities to abstract the command line usage of FFmpeg.
When you find that there are all kinds of strange requirements in the actual development, uploading files and so on is a small thing, snatching frames from video, etc., can only be implemented in a way 😢. Fortunately, The Node plug-in library is very rich, and FFMPEG plug-in can meet some of our basic needs for video operation. According to my simple analysis, the plug-in should call the EXCu () method of node child process module, and directly call the console command of FFMpeg software. Methods that are not provided by other plug-ins can be implemented using this idea.
Install FFMPEG
- Install FFMPEG software to use FFMPEG plug-in first need a computer FFMPEG software environment, install FFMPEG requires a BREW environment.
-
Install the brew, execute the following command/usr/bin/ruby – e “$(curl – fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” this process VPN is a little slow, you can search for other installation methods, not detailed here.
-
Install ffmpeg
brew install ffmpeg
This process also needs to install a lot of things, a bit of a long time, you can also download the installation package from the official website, but it needs to configure, Google it.
-
Install the plug-in execute the following command 👇 in the project
npm install ffmpeg
-
The introduction of the plugin
var ffmpeg = require('ffmpeg');
-
All methods in the FFMPEG plug-in can be called either using callback functions or promises.
- Callback function mode
new ffmpeg('/path/to/your_movie.avi'.function (err, video) {
if(! err) {console.log('The video is ready to be processed');
} else {
console.log('Error: '+ err); }});Copy the code
- Promise way
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
console.log('The video is ready to be processed');
}, function (err) {
console.log('Error: ' + err);
});
Copy the code
2. Video object
Video Object Properties
Each time a new instance is created, the library provides a new object to retrieve the video information, the FFMPEG configuration, and all the methods to make the necessary transformations:
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
console.log(video.metadata); // Video metadata
console.log(video.metadata.duration.seconds) // The duration of the Video
console.log(video.info_configuration); // FFmpeg configuration
}, function (err) {
console.log('Error: ' + err);
});
Copy the code
Video Object Method
The video object presets a set of methods that can operate independently of the preset configuration.
- Video. FnExtractSoundToMP3 (destionationFileName, callback) this method will place video audio stream extraction for mp3 files.
Parameters:
- DestionationFileName: the full path to the file to export:
/path/to/your_audio_file.mp3
- Callback: (Optional) If specified at the end of the process, it will return the path to the new audio file:
function (error, file)
Ex. :
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
// Callback mode
video.fnExtractSoundToMP3('/path/to/your_audio_file.mp3'.function (error, file) {
if(! error)console.log('Audio file: ' + file);
});
}, function (err) {
console.log('Error: ' + err);
});
Copy the code
-
Video. FnExtractFrameToJPG (destinationFolder, Settings, the callback) this method is to help achieve the demand of main method, it can capture video of one or more frames, returned to give us an image array.
Parameters:
-
DestinationFolder: the destinationFolder of the generated frames
/path/to/you_file
-
Setting :(Optional) Changes the Settings of the default Settings:
{
start_time: number, // Start time is not supported
duration_time: number, // The duration
frame_rate: number, // Frames per second
keep_aspect_ratio: false.// Keep the aspect ratio
size: number, // The size of the captured image is not supported
number: number, // Number of frames to capture
every_n_frames: number, // Truncate several frames apart
every_n_seconds: number, // Not supported
every_n_percentage: number,// Interval percentage interception is not supported
keep_pixel_aspect_ratio: true.// Mantain the original pixel video aspect ratio
padding_color: 'black'.// Padding color
file_name: null,}Copy the code
- Callback: (Optional) A list of paths to return created frames if specified at the end of the process:
function (error, file)
Ex. :
video.fnExtractFrameToJPG('/path/to/save_your_frames', {
frame_rate : 1.number : 5.file_name : 'my_frame_%t_%s'
}, function (error, files) {
if(! error)console.log('Frames: ' + files);
});
Copy the code
-
Video.fnaddwatermark (watermarkPath, newFilepath, Settings, callback this method is responsible for adding watermarks to the video you are developing. You can specify the exact location of the image location.
Parameters:
-
WatermarkPath: The full path to store the image
/path/to/retrieve/watermark_file.png
-
NewFilepath: (Optional) Name of the new video. If not specified, it will be created by this function. /path/to/save/your_file_video.mp4
-
Settings: (Optional) Change the Settings of the default Settings.
{
position: "SW".// Position: NE NC NW SE SC SW C CE CW
margin_nord: null.// Margin nord
margin_sud: null.// Margin sud
margin_east: null.// Margin east
margin_west: null.// Margin west
};
Copy the code
-
Callback: (Optional) If specified at the end of the process, it returns the path of the new video with the watermark.
function (error, file)
Ex. :
video.fnAddWatermark('/path/to/retrieve/watermark_file.png'.'/path/to/save/your_file_video.mp4', {
position : 'SE'
}, function (error, file) {
if(! error)console.log('New video file: ' + file);
});
Copy the code
- For other methods, see NPMffmpeg.