Node.js ffprobe API Library to Get Information of Media Video MP4 & Audio Files as JSON Object in Command Line Using Javascript Full Project For Beginners

 

 

npm i ffprobe

 

 

 

Usage

 

 

callback api

 

 

var ffprobe = require('ffprobe'),
    ffprobeStatic = require('ffprobe-static');
 
ffprobe('./file.mp4', { path: ffprobeStatic.path }, function (err, info) {
  if (err) return done(err);
  console.log(info);
/***
{
    "streams": [
        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "High",
            "codec_type": "video",
            "codec_time_base": "1/50",
            "codec_tag_string": "avc1",
            "codec_tag": "0x31637661",
            "width": 1280,
            "height": 720,
            "coded_width": 1280,
            "coded_height": 720,
            "has_b_frames": 0,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuv420p",
            "level": 31,
            "chroma_location": "left",
            "refs": 1,
            "is_avc": "1",
            "nal_length_size": "4",
            "r_frame_rate": "25/1",
            "avg_frame_rate": "25/1",
            "time_base": "1/25",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 299,
            "duration": "11.960000",
            "bit_rate": "1031739",
            "bits_per_raw_sample": "8",
            "nb_frames": "299",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0
            },
            "tags": {
                "language": "und",
                "handler_name": "VideoHandler"
            }
        },
        {
            "index": 1,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "LC",
            "codec_type": "audio",
            "codec_time_base": "1/44100",
            "codec_tag_string": "mp4a",
            "codec_tag": "0x6134706d",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/44100",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 528384,
            "duration": "11.981497",
            "bit_rate": "192287",
            "max_bit_rate": "203120",
            "nb_frames": "516",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0
            },
            "tags": {
                "creation_time": "2015-11-16 00:48:42",
                "language": "eng",
                "handler_name": "IsoMedia File Produced by Google, 5-11-2011"
            }
        }
    ]
}
 **/
});

 

 

promise api

 

 

var ffprobe = require('ffprobe'),
    ffprobeStatic = require('ffprobe-static');
 
ffprobe('./file.mp4', { path: ffprobeStatic.path })
  .then(function (info) {
    console.log(info);
    /***
    {
        "streams": [
            {
                "index": 0,
                "codec_name": "h264",
                "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
                "profile": "High",
 
                ...
                }
            }
        ]
    }
     **/
  })
  .catch(function (err) {
    console.error(err);
  })
});

 

 

npm i express multer

 

 

const ffprobe = require('ffprobe')

const express = require('express')

const multer = require('multer')

const path = require('path')

const bodyparser = require('body-parser')

const app = express()

app.use(express.static(path.join(__dirname + "/uploads")))

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads");
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname)); //Appending extension
  },
});

const upload = multer({storage:storage})

app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())

const ffprobestatic = require('ffprobe-static')

app.get('/', (req, res) => {
    res.sendFile(__dirname + "/index.html")
})

app.post('/mediainfo', upload.single('file'), (req, res) => {
    console.log(req.file.path)
    ffprobe(req.file.path, { path: ffprobestatic.path }, (err, info) => {
      if (err) {
        console.log(err);
      } else {
          console.log(info);
          res.json({
              info:info
          })
      }
    });
})

app.listen(5000, () => {
    console.log("App is listening on port 5000")
})

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="/mediainfo" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" required id="">
    <button type="submit">Get Media Info</button>
    </form>
</body>
</html>

Leave a Reply