1

I am trying to make a script that emails an audio file from a website as an attachment (the website sends out an email with a link to the file when a new podcast comes out). My problem is Google doesn't allow attachments larger than 25MB. My idea was to convert the file into a byte array, and then split the file up into 25MB chunks (see code):

function emailFile(file, body, counter) {
  console.log("entered EF");
  console.log("file.getBytes().length: ", file.getBytes().length);
const maxFileSize = 26214400; //25MB Max File Size Limit by Gmail
if (counter <= 1) {
    var subject = "(File) - " + PropertiesService.getScriptProperties().getProperty('orgSubject'); 
  }
  else {
    var subject = "(File - Part " + counter + ") - " + PropertiesService.getScriptProperties().getProperty('orgSubject');
    file.setName(PropertiesService.getScriptProperties().getProperty('orgTitle').replaceAll(".mp3", "") + "-part-" + counter + ".mp3");
  }
if (file.getBytes().length <= maxFileSize) {
  MailApp.sendEmail({
    to: Session.getActiveUser().getEmail(),
    subject: subject,
    htmlBody: body,
    attachments: [file],
    name: 'Automatic Emailer Script'
  });
  } else {
    var byteArray = file.getBytes();
    var part1 = byteArray.splice(0, 25000000);
    var part2 = byteArray.splice(-25000000);
    part1 = Utilities.newBlob(part1, 'audio/mp3', PropertiesService.getScriptProperties().getProperty('orgTitle').replaceAll(".mp3", "") + "-part-" + counter + ".mp3");
    part2 = Utilities.newBlob(part2, 'audio/mp3', PropertiesService.getScriptProperties().getProperty('orgTitle').replaceAll(".mp3", "") + "-part-" + (counter + 1) + ".mp3");
    MailApp.sendEmail({
     to: Session.getActiveUser().getEmail(),
     subject: "(File - Part " + counter + ") - " + PropertiesService.getScriptProperties().getProperty('orgSubject'),
     htmlBody: body,
     attachments: [part1],
     name: 'Automatic Emailer Script'
  });
    emailFile(part2, body, counter++);        
  }
}

My question is that this works, but the metadata doesn't reflect the new size, (I think the problem is the audio metadata, I could be wrong), because the audio player I use shows the old time (for example, if the entire podcast was an hour long, and now 25MB of if is only 45 minutes long, it'll still say 1:00:00, and once it gets to the 45 minute mark it jumps to the end. Part 2 of the file "slips" when I pause it, meaning, it'll will continue from either a few seconds before of after where I paused it from). Does anyone have a way to do this more properly using Google Apps Scripts? Thanks! (Please don't just tell me to use a different audio player)

VC.One
  • 14,790
  • 4
  • 25
  • 57
Leo
  • 11
  • 3
  • Doesn't your podcast provider have an rss feed? If so why don't you just read the rss feed and look for the most recent item and you will know right a way whenever they have one. You don't really have to download it. You can just listen to it from there. I know it get's download anyway but it will also get erase after a while as well. – Cooper Jan 17 '22 at 20:36
  • That was my first idea when I wanted to set this up, but they don't have an RSS feed. – Leo Jan 17 '22 at 20:41
  • Look into [design > file structure](https://wikipedia.org/wiki/MP3). You need to modify the bytes accordingly. – TheMaster Jan 17 '22 at 20:41
  • That's surprising that they don't have an rss feed – Cooper Jan 17 '22 at 20:43
  • 1
    Have you ever tried to assign your audio elements src to the url of the file? – Cooper Jan 17 '22 at 20:45
  • 1
    Search [here](https://stackoverflow.com/search?q=%5Bmp3%5D%5Bjs%5D+split) isn't very promising, but basically you'd have to do what [tag:ffmpeg] does with binary data. Other than that, if any of Cooper's suggestion works, that's the best way to go. – TheMaster Jan 17 '22 at 20:55
  • 1
    According to [this related post](https://stackoverflow.com/a/53747530), **"They (MP3 files) don't contain any timestamps. It's just a series of MPEG frames, one after the other."**, which could most likely be related to your issue. Also, on the [MPEG Layer III](http://www.mpgedit.org/mpgedit/mpeg_format/MP3Format.html) online docs, it says `"you can cut any part of MPEG file and play it correctly. For Layer III, this is not 100% correct. Due to internal data organization in MPEG version 1 Layer III files, frames are often dependent of each other and they cannot be cut off just like that"` – SputnikDrunk2 Jan 17 '22 at 22:46
  • @Cooper 1) when you say "audio elements source", what exactly are you referring to? 2) What do you mean by "url of the file"? Basically, since I'm sort of new to this, I don't understand what you're saying – Leo Jan 18 '22 at 00:20
  • I have my own Podcast player built into one of my spreadsheets and when you push the play button it just takes the audio Url recovered from the rss feed for that channel and set the audio elements src attribute equal to it `document.getElementById(id).src=obj.aurl;` – Cooper Jan 18 '22 at 01:36
  • You can get the audio url from the enclosure tag of a Podcasts RSS feed. Here's an example `` – Cooper Jan 18 '22 at 01:42
  • [Split MP3 files](https://softwarerecs.stackexchange.com/questions/14786/split-mp3-files-automatically) – Cooper Jan 18 '22 at 02:52

0 Answers0