mediainfo.js query specific data

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?

27 thoughts on “mediainfo.js query specific data”

  1. 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" />
    Reply

Leave a Comment