-2

How to crop the duration of an audio file from only 30 seconds before uploading it using JavaScript. I have to select and upload audio file from my local storage, but I want to upload any audio file duration is maximum 30 seconds as like process of video upload on whatsapp status. Provide me a solution to sort this problem.

MANISH SHARMA
  • 33
  • 1
  • 8
  • Read the file into an Array (using `reader.readasBufferArray`). Note that MP3 is just "frames" or pieces joined up. Each piece has its own MP3 header and so is playable as a file, each piece represents X amount of milliseconds so get enough pieces to make 30 seconds into your output array and save as file. The MP3 header usually starts with `[FF] [FB]` in the loaded array then from next 2 bytes you extract certain information and use it to check **frame length in MS** using [this formula](https://stackoverflow.com/a/12650386/2057709) then divide 30 secs by frame length to know `how many == 30` – VC.One Jan 30 '22 at 13:59
  • PS: If you can show code where you get an MP3 loaded into a buffer array and also share same test MP3 file, then I can try help you with making the rest of code to trim the file down to 30 seconds. – VC.One Jan 30 '22 at 14:41

1 Answers1

0

It's easier to use an audio cutter online, just like this: https://clideo.com/cut-audio, I've tried to do it in JS too but I could not.