-1

I got an example working great.

Now Im trying to modify that working example and come up with a way to extract specific data.

For example. Frame rate.

Im thinking the syntax should be something like this with result.frameRate

see below where I tried console.log("Frame: "+ result.frameRate) also tried the Buzz suggestion of result.media.track[0].FrameRate neither suggestion works.

<button class="btn btn-default" id="getframe" onclick="onClickMediaButton()">Get Frame Rate</button>
<script type="text/javascript" src="https://unpkg.com/mediainfo.js/dist/mediainfo.min.js"></script>


        const onClickMediaButton = (filename) => {
            //const file = fileinput.files[0]
            const file = "D:\VLCrecords\Poldark-Episode6.mp4"
            if (file) {
                output222.value = 'Working…'

                const getSize = () => file.size

                const readChunk = (chunkSize, offset) =>
                    new Promise((resolve, reject) => {
                        const reader = new FileReader()
                        reader.onload = (event) => {
                            if (event.target.error) {
                                reject(event.target.error)
                            }
                            resolve(new Uint8Array(event.target.result))
                        }
                        reader.readAsArrayBuffer(file.slice(offset, offset + chunkSize))
                    })

                mediainfo
                    .analyzeData(getSize, readChunk)
                    .then((result) => {
                        consoleLog("Frame: " + result.media.track[0].FrameRate);
                        output222.value = result;
                    })
                    .catch((error) => {
                        output222.value = `An error occured:\n${error.stack}`
                    })
            }
        }

but I cant figure out the exact syntax. Can you help point me in the right direction?

buzz
  • 939
  • 3
  • 15
H D Dang
  • 23
  • 2

1 Answers1

2

Short answer

Use result.media.track[0].FrameRate.

Long answer

The type of result depends on how you instantiated the library. Your example does not provide enough information on how you are using the library.

From the docs:

MediaInfo(opts, successCallback, errorCallback)

Where opts.format can be object, JSON, XML, HTML or text. So, assuming you used format: 'object' (the default), result will be a JavaScript object.

The structure of the result object depends on the data you provide to MediaInfoLib (or in this case mediainfo.js which is an Emscripten port of MediaInfoLib). The information about the framerate will only be available if you feed a file with at least one video track to the library.

Assuming this is the case, you can access the list of tracks using result.media.track. Given that the video track you are interested in has the index 0, the access to the desired property would be result.media.track[0].FrameRate. This is true for a large amount of video files that usually have at least one video track and have this track as the first available track. Note that this won't necessarily work for all video files and you must make sure your code is fault-tolerant in case these properties don't exist on the result object.

Unfortunately, it seems there is no detailed list of available fields in the MediaInfoLib documentation. You could look at the source code. This might get tedious and lengthy and requires you to understand a fair amount of C++. The most convenient way for me though is to just feed MediaInfoLib a file and look at the result.

PS: The question was already answered in this GitHub issue.


Disclaimer: I'm the author of the aforementioned Emscripten port mediainfo.js.

EDIT:

I don't think you are correct when saying my answer is "partial-incomplete" and "does not work."

As a proof here is a working snippet. Obviously you need to use a media file with a video track.

const fileinput = document.getElementById('fileinput');

const onChangeFile = (mediainfo) => {
  const file = fileinput.files[0];
  if (file) {
    const getSize = () => file.size;
    const readChunk = (chunkSize, offset) =>
      new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = (event) => {
          resolve(new Uint8Array(event.target.result));
        }
        reader.readAsArrayBuffer(file.slice(offset, offset + chunkSize));
      });

    console.log(`Processing ${file.name}`);

    mediainfo
      .analyzeData(getSize, readChunk)
      .then((result) => {
        const frameRate = result.media.track[0].FrameRate; // <- Here we read the framerate
        console.log(`Framerate: ${frameRate}`);
      })
  }
}

MediaInfo(null, (mediainfo) => {
  fileinput.addEventListener('change', () => onChangeFile(mediainfo));
  fileinput.disabled = false;
})
<script src="https://unpkg.com/mediainfo.js@0.1.4/dist/mediainfo.min.js"></script>
<input disabled id="fileinput" type="file" />
buzz
  • 939
  • 3
  • 15
  • Buzz - The answer was not satisfactorily supplied at github. Some guy over there (also named Buzz) gave a partial-incomplete answer that does not work. The code is implemented using the stock example. Ill post it again here for reference. (see post on top for an expanded explanation ) – H D Dang Feb 18 '21 at 23:16
  • @HongDongDang The answer I gave you in the [GitHub issue](https://github.com/buzz/mediainfo.js/issues/49#issuecomment-776875512) is the same one I gave you here. The code is tested and works. If you feel your question is not completely answered, please state what exactly has not been answered. Otherwise feel free to mark the answer as correct. – buzz Feb 18 '21 at 23:23
  • Same result. It does not work. Ill post some more details below in code brackets. – H D Dang Feb 19 '21 at 16:57
  • You are wrong. It does indeed work. I attached a working code snippet. Click "Run code snippet", the code snippet will start to run, Click on the "Choose file" button, a file chooser dialog will open, select a video file from your harddrive, click ok. Beneath it will show you the frame rate. It can't get any easier ;-) – buzz Feb 19 '21 at 19:27
  • Here is a [Screenshot proof](https://i.ibb.co/xGwMgTX/Screenshot.png). – buzz Feb 19 '21 at 19:38
  • Buzz answer does not work when applied to the specific goals as required. You are chasing a solution that differs drastically from the goal. If you follow the goal, I get an error Uncaught TypeError: Cannot read property '0' of undefined. The line that triggers this error message is; const file = fileinput.files[0]; (see code below) – H D Dang Feb 20 '21 at 01:23
  • Nope, your question was how to "query specific data", namely the frame rate. This question has been answered. Besides that you are completely right, the code below as well as all other code you ever posted here is faulty. – buzz Feb 20 '21 at 02:41