2 years ago

#62366

test-img

Leo

How to properly divide an mp3 file with Google Apps Scripts

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)

google-apps-script

mp3

audio-processing

0 Answers

Your Answer

Accepted video resources