0

I'm trying to get the properties (e.g. its duration) of a MP3 file at a URL in a Google Apps Script. The following code gets an object, but as best I can tell, the resulting object is not an audio object.

function loadMusic(url) {
  if (url == undefined) {
    url = "http://traffic.libsyn.com/socialmediasec/Weekly_Blaze_E43.mp3";
  };
  var audio = UrlFetchApp.fetch(url).getAs("audio/mpeg");

  return audio;
};

Doing this kind of thing in a webpage is simple using the Audio object (see W3Schools' documentation (link). Ideally, I would like to do something similar to this (link) except by referencing a URL instead of a DOM element. Is this possible?

Rubén
  • 34,714
  • 9
  • 70
  • 166
tlewis3348
  • 424
  • 2
  • 7
  • 21
  • This may be an issue with the fact that Apps Script is still very underdeveloped and incapable of *a lot* of standard JavaScript functionality. I would suggest not using Apps Script; just use the Google APIs with Node.js or Python or something. – glotchimo Nov 20 '18 at 02:55
  • @e.maguire "Audio object" and the duration property aren't part of the JavaScript specification but they are part of the web apis. Ref. https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/duration – Rubén Nov 20 '18 at 03:11
  • @e.maguire The thing is that I am doing this in a custom function in Google Sheets. Is it possible to use the Google APIs with Node.J's or Python in Google Sheets? – tlewis3348 Nov 20 '18 at 10:41

1 Answers1

0

UrlFetchApp.fetch(url).getAs("audio/mpeg") returns a blob object. Google Apps Script doesn't has an specific class for "audio objects". If you save the file to Google Drive, then you could use the Google Apps Script advanced service for Google Drive to get the metadata from the file but depending on the specific details of what you are looking for, it could be easier to use client-side code to get the DOM audio object and the related properties like the audio duration.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I'm trying to get this information in a custom function in Google Sheets. Is it possible to use client-side code to get the DOM audio object from inside a custom function? I could certainly do as you suggest and add the file to Google Drive, but because I'm trying to get this information for all the episodes from several different podcasts over the past year, my suspicion would be that it would be pretty slow and eat up my Drive storage space pretty quickly. If there's no other option, I may be able to make it work, but I was hoping for a simpler solution. – tlewis3348 Nov 20 '18 at 10:49
  • 3
    Custom function can't access client-side code but client-side code can update cells. Please checkout https://developers.google.com/apps-script/guides/sheets/functions and https://developers.google.com/apps-script/guides/html/. If you need further help, post a new question. – Rubén Nov 20 '18 at 17:29
  • Anything new here. I am a teacher with education games in Google Sheets. I really want to add short audio from the script based on actions of the user. – aNewb Nov 19 '20 at 15:57
  • @aNewb Perhaps the must relevant novelty is that nowadays you can choose between two runtimes, the new and default for new projects is Chrome V8, besides that I don't think that there is anything else worthy to mention as the OP didn't included details of what they want to do. Please consider to post a new question. – Rubén Nov 19 '20 at 16:03
  • @aNewb You might find this Q/A interesting [How to reduce the latency between two script calls in Google Apps Script](https://stackoverflow.com/q/64847239/1595451) (it's related to playing audio files listed in a spreasheet) – Rubén Nov 19 '20 at 16:08