How does YouTube make it play the video and audio separately and work in Safari

I’m getting the data from the YouTube video with ytdl-core, however with videos above 1080p the video and audio files are separate, I would like to make it work without having to do any processing in the backend to join the video and audio or do something directly in the client side.

I can make it work normally on Android/Windows, but on Apple devices (Safari) there is a crash when I play both media files at the same time.

Remembering that I want to play the video in specific sizes, for example, 480p, 720p and 1080p and synchronize with the audio, the customer will manually select the video quality.

Is there any solution for this case?

I tried to place the video and audio elements directly in the html and synchronizing via javascript didn’t work in safari

I tried leaving just the video in the html and the audio using the new Audio(url) api, but I didn’t have a good result either.

I hope to be able to play the two synchronized media files directly on the client