Home > Web Front-end > JS Tutorial > How to convert mp4 video to animated gif using Node.js

How to convert mp4 video to animated gif using Node.js

PHPz
Release: 2023-04-05 10:17:34
Original
980 people have browsed it

Node.js is a JavaScript running environment based on the Chrome V8 engine. It can run JavaScript code on the server side, which makes it popular in web development. At the same time, Node.js can also be used to perform various processing tasks, including converting mp4 videos to gif animations.

In this article, we will learn how to use Node.js to convert mp4 videos to animated gifs. First, we need to install some necessary tools.

The first step is to install FFmpeg, which is a widely used open source software that can perform multimedia processing tasks, such as video transcoding, video editing, etc. We will use FFmpeg to convert mp4 videos to animated gifs.

In Ubuntu system, you can use the following command to install FFmpeg:

sudo apt-get install ffmpeg
Copy after login

In Windows system, you can download the precompiled binary file from the official website and add it to the environment variable.

After the installation is complete, we will use Node.js to write code to call FFmpeg for video conversion. We will use the child_process module of Node.js to execute system commands.

First, we need to install the child_process module of Node.js. Open a terminal and enter the following command:

npm install child_process
Copy after login

Then, write the following code in Node.js:

const {exec} = require('child_process');

const convertToGif = (inputPath, outputPath) => {
  const command = `ffmpeg -i ${inputPath} -vf "scale=320:-1" -t 5 -r 10 -f gif ${outputPath}`;
  return new Promise((resolve, reject) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
        reject(error);
      } else {
        resolve();
      }
    });
  });
};

// Usage example
convertToGif('/path/to/input.mp4', '/path/to/output.gif')
  .then(() => console.log('Video converted to GIF'))
  .catch(error => console.error('Error converting video to GIF:', error));
Copy after login

This code does the following:

  • Receives input path and output path as parameters.
  • Concatenate FFmpeg commands together and pass them to Node.js’ child_process module to execute the command from JavaScript.
  • Resolve or reject the execution result as a Promise.

This command will read the mp4 file on the specified path, convert it into a 320 pixel wide GIF file, and limit the length of the file to 5 seconds, with a frame rate of 10 frames/second .

Since this is an asynchronous process, we use Promise to handle the results. You can write code as needed to wait for the conversion to complete, handle any errors, and so on.

Hope this article can help you understand how to use Node.js to convert mp4 files to gif files. Node.js provides many useful features, including handling multimedia files. By taking advantage of its features, you can easily perform video conversion or other similar tasks.

The above is the detailed content of How to convert mp4 video to animated gif using Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template