AyKarsi
AyKarsi

Reputation: 9685

Google Cloud Storage: Download partial audio file

I need to process large audio files in segments. Instead of downloading the full file and the extracting a timeframe, I'd like to only download the required segment (e.g. seconds 25 to 35).

As the API does not support this out of the box, what is the best way of only download segments of an audio file?

Upvotes: 0

Views: 249

Answers (1)

Juancki
Juancki

Reputation: 1882

Reading from seconds its not supported, as Google Storage does not read the file and interpret it.

You can do segments of bytes. This is called reading ranges of bytes, which is supported in Cloud Storage.

You have to add a header Range: bytes=20-2000 as documented [1].

In nodejs you would do something like this [2]

   const logFile = myBucket.file('access_log');
   logFile.createReadStream({
     start: 20,
     end: 2000
   })

[1] https://cloud.google.com/storage/docs/xml-api/reference-headers#range

[2] https://github.com/googleapis/nodejs-storage/blob/607f6c1c8b9ae5414513957f54a5de2490c454b1/src/file.ts#L1178

Upvotes: 1

Related Questions