A robust utility function to reliably extract the 11-character YouTube video ID from all official URL formats. Most solutions break on edge cases, but this logic handles variations including shorts, live streams, and mobile URLs.
The extractor supports the following formats, including those with extra parameters (like ?t= or &si=):
- Standard:
youtube.com/watch?v=VIDEO_ID - Shortened:
youtu.be/VIDEO_ID - Shorts:
youtube.com/shorts/VIDEO_ID - Embeds:
youtube.com/embed/VIDEO_ID