buzz / mediainfo.js

Extract media file metadata in the browser using WebAssembly.
https://mediainfo.js.org
BSD 2-Clause "Simplified" License
689 stars 107 forks source link

Node: MediaInfoFactory is not a function #143

Closed jessielw closed 1 year ago

jessielw commented 1 year ago

Checklist

Bug Description

I've been trying to use mediainfo.js via Node (the library (and the cli on Windows doesn't work at all)). When I try to use it via the library method https://github.com/buzz/mediainfo.js/blob/main/API.md I get MediaInfoFactory is not a function. I'm fairly new to Node so I spent the last 6 hours trying different combinations to try to solve it but with no success.

Steps to Reproduce

Install latest Node (LTS) Run npm install mediainfo.js Create a script to try to use MediaInfoFactory() Run script via node scriptname.js

Expected Behavior

To parse a mediafile

Actual Behavior

MediaInfoFactory is not a function

Environment

Additional Information

Example code: (I tried code from the CLI and issues I found that was closed here on the tracker, neither worked)

const MediaInfoFactory = require('mediainfo.js');

const createMediaInfo = async () => {
  // Create a new MediaInfo instance
  const mediainfo = await MediaInfoFactory();
  return mediainfo;
};

const extractMediaInfo = async (fileBuffer) => {
  try {
    // Create a MediaInfo instance
    const mediainfo = await createMediaInfo();

    // Analyze the media data
    const result = await mediainfo.analyzeData(
      () => fileBuffer.length,
      (size, offset) => Promise.resolve(fileBuffer.slice(offset, offset + size))
    );

    // Close the MediaInfo instance
    mediainfo.close();

    return result;
  } catch (error) {
    throw new Error(`Error analyzing media: ${error.message}`);
  }
};

// Usage example:
const fs = require('fs');

// Load the media file into a buffer (you can replace 'media.mp4' with your file path)
const fileBuffer = fs.readFileSync("C:/Users/jlw_4/OneDrive/Desktop/testing/audio test.mkv");

// Extract media information
extractMediaInfo(fileBuffer)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.error(error);
  });

I could very well be doing something wrong, if so I apologize for opening this up as a bug.

buzz commented 1 year ago

If you replace the first line, it will magically work:

const { default: MediaInfoFactory } = require("mediainfo.js");

This is necessary when you use it with a CommonJS module system because mediainfo.js is an ESM package. It boils down to JavaScript having multiple partially incompatible module systems. There are tons of blog posts about this topic. Just search the internet.

jessielw commented 1 year ago

I appreciate the quick response and I can confirm this works fine. I'm very experienced in python/python web frameworks but node is another beast I'm learning. Thanks man!