I have created a Library to do that. Please read this blog.
https://codeformat.dev/blog/how-youtube-download-videos-and-play-video-offline