alexeichhorn/YouTubeKit

QUESTION: Downloading is really slow

Closed this issue · 5 comments

I noticed that loading the URL retrieved in a WKWebView's AUDIO element seems to load the audio instantly.
We can also skip to different times.

However, performing the download in Swift using for example the Alamofire library is super slow, as if the server was throttling our request.

Any idea on how we could improve the situation?

The way I solved it is by downloading chunks of 2-10 MB in parallel with separate requests and then combining them again. Depending on the type of stream you are downloading, there are different options to request the specific range of bytes.

Thank you!
I am only interested in an audio stream (to transcribe them).
Do you have code samples or links showing how to download in chunks?

Basically, you can only download a certain chunk of a file by setting the HTTP headers like this:

var request = URLRequest(url: url)
request.addValue("bytes=\(contentStart)-\(contentEnd)", forHTTPHeaderField: "Range")

Depends a bit on the stream type if this works without throttling or not. For dash audio I personally use this as the previous method is sometimes still throttling (no guarantees that it always works):

var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.queryItems?.append(URLQueryItem(name: "range", value: "\(contentStart)-\(contentEnd)"))
          
var request = URLRequest(url: components.url!)
request.httpMethod = "post"

Thanks a lot, I managed to make it work using range requests.
Downloading a 30 minutes audio now takes about 5 seconds with chunks of 2MB.

@alexeichhorn Hey Alex, thank you for creating this amazing library! It’s been incredibly helpful.

I was wondering if you could provide a more detailed code example for implementing the chunked download approach you mentioned.

Thank you !