Search code examples
javascriptaudiomp3

How do I get a mp3 files info duration, title, image etc in javascript


I understand how to get a mp3 files info in Java but I'd like to attempt to put less strain on the server by implementing it in javascript. I am new to javascript and don't understand the syntax well. I found this https://www.npmjs.com/package/jsmediatags and It looks like what I need but I tried to implement it and it didn't work basically I have a form like so

<form action="jsmediatags.read(document.getElementById("audiofile"));">
    <input type="file" name="audio" accept="audio/*">
    <input type="submit" name="submit" value="add music"/>
</form>

I would like for jsmediatags.read() to pull the necessary info from the file on submit then send it to the server. Am I implementing this API in the wrong way? Is there a way to do it without the API?


Solution

  • jsmediatags already has this browser part covered in the readme

    // From File
    $inputTypeFile.addEventListener("change", function(event) {
      var file = event.target.files[0];
      jsmediatags.read(file, {
        onSuccess: function(tag) {
          console.log(tag);
          // use ajax to upload tag info, or create some new form elements
        },
        onError: function(error) {
          console.log(error);
        }
      });              
    }, false);
    <script src="https://cdn.rawgit.com/aadsm/jsmediatags/master/dist/jsmediatags.min.js"></script>
    
    <form>
        <input id="$inputTypeFile" type="file" name="audio" accept="audio/*">
        <input type="submit" name="submit" value="add music"/>
    </form>