b5i / YouTubeKit

A way to interact with YouTube's API in Swift (without any API key!)
https://swiftpackageindex.com/b5i/YouTubeKit
MIT License
64 stars 7 forks source link

VideoInfosWithDownloadFormatsResponse response took too long #24

Open hoangnam714 opened 1 month ago

hoangnam714 commented 1 month ago

When I use VideoInfosWithDownloadFormatsResponse to get information, the function works well but the response time is very long, it takes 10 to 15 seconds, how can it be optimized, my network is very good, I can watch 4k videos smoothly

let formatsResponse = try await VideoInfosWithDownloadFormatsResponse.sendThrowingRequest(
 youtubeModel: YTM,
 data: [.query:"abPmZCZZrFA"])
b5i commented 1 month ago

Could you look at Xcode's network tracker and give me the time it takes to do the network requests of YouTubeKit please?

The likely reason why you encounter such delays is because VideoInfosWithDownloadFormatsResponse also needs to download the YouTube Player engine before being able to get you the Download formats. It needs to do that every time the player gets updated because YouTubeKit keeps in memory the important data so it doesn't have to download a second time the same player if it hasn't changed. However, as you have a good connection, you shouldn't have any issue with that as the test takes GitHub only 5 seconds to complete.

hoangnam714 commented 1 month ago

Hello B5I, After a period of testing and upgrading to the latest version, I was able to get information faster, perhaps a fast network is not enough.

But I have a question VideoInfosWithDownloadFormatsResponse.sendThrowingRequest defaultFormats sometimes returns 1 video with 360 quality, sometimes returns 2 quality 360 and 720, is there any way to optimize this and return 2 urls with quality or than not, thank you

b5i commented 1 month ago

Hello

But I have a question VideoInfosWithDownloadFormatsResponse.sendThrowingRequest defaultFormats sometimes returns 1 video with 360 quality, sometimes returns 2 quality 360 and 720, is there any way to optimize this and return 2 urls with quality or than not, thank you

Are you getting sometimes 1 or 2 results for the same video or for different ones?

If it's for the same video, then it's a bit odd and I would like to know the videoId to check on my side. If it's not, then it's likely that YouTube hasn't converted the format that the YouTuber gave it into 720p and there's no solutions for it to work. You can still use the downloadFormats (there's plenty of them normally), but they may be a bit slower.

hoangnam714 commented 1 month ago

i am using this video id : MKw2iF6hrI8 I really want to use defaultFormats because the url that returns audio video is available, and what quality does defaultFormats currently return? I am limiting the use of downloadFormats for videos with no available audio. It will take me time to check if the url works or not and check the audio url too.

hoangnam714 commented 1 month ago

I discovered one more problem, the video url returned in downloadFormats is very slow to load, while default defaultFormats can load immediately. I am checking in the above ID video. I tried using the video URL and running on safari and got the same results

b5i commented 1 month ago

I really want to use defaultFormats because the url that returns audio video is available, and what quality does defaultFormats currently return?

It usually returns 360p and 720p (sometimes 1080p too if I remember correctly).

I am limiting the use of downloadFormats for videos with no available audio. It will take me time to check if the url works or not and check the audio url too.

You can use an AVMutableComposition and put one audio format with a video one, but if you directly feed an AVPlayer with it it's likely to load very slowly (it has to download the whole tracks before playing) as it doesn't support DASH.

b5i commented 1 month ago

I discovered one more problem, the video url returned in downloadFormats is very slow to load, while default defaultFormats can load immediately. I am checking in the above ID video. I tried using the video URL and running on safari and got the same results

Yes that's expected, you can use the range=0- HTTP header to speed the thing up (that's what Chrome does if you try).

hoangnam714 commented 1 month ago

You can use an AVMutableComposition and put one audio format with a video one, but if you directly feed an AVPlayer with it it's likely to load very slowly (it has to download the whole tracks before playing) as it doesn't support DASH.

I'm thinking of playing video url and audio url separately, it's quite ok instead of using AVMutableComposition. But how can I speed up video playback using http header like you said. I'm sorry but I still found a way to load video urls that don't support DASH more smoothly :(

b5i commented 1 month ago

You can try to use the private AVURLAssetHTTPHeaderFieldsKey, I tried it for Atwy but I don't remember it having a big effect.