Home  >  Article  >  Web Front-end  >  How to set video duration in nodejs

How to set video duration in nodejs

PHPz
PHPzOriginal
2023-05-18 09:51:37685browse

Node.js is a very popular JavaScript runtime environment that can be used to build various types of applications. Among them, web applications are the most popular one. As more and more users use the Internet to watch video content, the length of the video becomes very important. This article will introduce how to set the video duration in Node.js.

First of all, we need to understand the basic concept of video. Video consists of a series of frames, each frame represents an image in the video. Each frame of a video has a timestamp that indicates when that frame was played in the video.

The FFmpeg library can be used in Node.js to process video files. FFmpeg is a popular open source library for processing audio and video files. It can read the metadata of video files, including video duration, frame rate, resolution and other information.

The following is a sample code for getting the video duration in Node.js:

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

function getVideoDuration(filename) {
  return new Promise((resolve, reject) => {
    const ffmpeg = spawn('ffmpeg', ['-i', filename]);
    let duration = null, stderr = '';
    ffmpeg.stderr.on('data', (data) => {
      stderr += data.toString();
    });
    ffmpeg.stderr.on('end', () => {
      const match = stderr.match(/Duration: (d{2}):(d{2}):(d{2})/);
      if (match !== null) {
        const hours = parseInt(match[1], 10);
        const minutes = parseInt(match[2], 10);
        const seconds = parseInt(match[3], 10);
        duration = (hours * 60 * 60) + (minutes * 60) + seconds;
      }
      resolve(duration);
    });
    ffmpeg.stderr.on('error', (error) => {
      reject(error);
    });
  });
}

getVideoDuration('example.mp4').then((duration) => {
  console.log(`The video duration is ${duration} seconds.`);
}).catch((error) => {
  console.error(error);
});

The above code uses the spawn function to start the FFmpeg process and passes the -i parameter and the video file name as parameters. The output stream of the FFmpeg process is automatically sent to the stderr stream by the thread. Use the stderr.on function to listen to the output of the stderr stream to grab the video duration from the metadata of the video file. The code finally uses Promise to return the video duration.

Of course, there is more than one library for processing videos in Node.js: FFmpeg. Another popular library is GStreamer. The following is a sample code that uses GStreamer to obtain the video duration:

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

function getVideoDuration(filename) {
  return new Promise((resolve, reject) => {
    const gst = spawn('gst-discoverer-1.0', filename);
    let duration = null, stderr = '';
    gst.stderr.on('data', (data) => {
      stderr += data.toString();
    });
    gst.stderr.on('end', () => {
      const match = stderr.match(/Duration: (d{2}):(d{2}):(d{2})/);
      if (match !== null) {
        const hours = parseInt(match[1], 10);
        const minutes = parseInt(match[2], 10);
        const seconds = parseInt(match[3], 10);
        duration = (hours * 60 * 60) + (minutes * 60) + seconds;
      }
      resolve(duration);
    });
    gst.stderr.on('error', (error) => {
      reject(error);
    });
  });
}

getVideoDuration('example.mp4').then((duration) => {
  console.log(`The video duration is ${duration} seconds.`);
}).catch((error) => {
  console.error(error);
});

The above code starts a GStreamer process and passes the file name as a parameter. Then use the stderr stream to listen to the process output and extract the video duration information from the metadata.

Overall, Node.js can easily obtain video duration information by using libraries such as FFmpeg and GStreamer. Here are just some of the sample codes, developers can choose the most suitable processing method according to their own needs.

The above is the detailed content of How to set video duration in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:nodejs modify js fileNext article:nodejs modify js file