Extracts YouTube video, channel, and playlist IDs from various URLs
npm install opex-yt-id



---
A robust Node.js library (ESM) to extract YouTube Video, Channel (UC-prefixed), and Playlist (PL-prefixed) IDs from a wide variety of URL formats.
* Video IDs:
* Supports standard youtube.com URLs (/watch, /v/, /embed/, /shorts/, /live/, etc.)
* Supports mobile m.youtube.com URLs.
* Supports official shortener youtu.be and alternative y2u.be.
* Supports privacy-enhanced youtube-nocookie.com embeds.
* Supports music.youtube.com and kids.youtube.com.
* Supports image links from i.ytimg.com.
* Supports legacy formats (/e/, /vi/, user channel fragments #p/u/...).
* Handles embed code strings.
Provides a generic fallback for /watch?v={ID} on any* domain.
* Channel IDs (UC-prefixed):
* Supports standard youtube.com/channel/UC... URLs.
Limitation:* Does not resolve custom URLs (/c/Name), legacy usernames (/user/Name), or handles (/@Handle) as this requires external lookups.
* Playlist IDs (PL-prefixed):
* Supports standard youtube.com/playlist?list=PL... URLs.
* Supports URLs where a video is watched within a playlist (/watch?v=...&list=PL...).
* Supports music.youtube.com/playlist?list=PL....
* General:
* Supports youtube:// mobile app schemes (common video patterns).
* Includes support for known third-party frontend domains (like Invidious, Piped - based on static list) for relevant ID types.
* Written in modern ECMAScript (ESM) with type definitions included.
``bash`
npm install opex-yt-idor
yarn add opex-yt-id
`javascript
import {
getYouTubeVideoId,
getYouTubeChannelId,
getYouTubePlaylistId
} from 'opex-yt-id';
// Video Examples
const urlV1 = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
const urlV2 = 'https://youtu.be/jNQXAC9IVRw?t=15';
const urlV3 = '';
const urlV4 = 'https://yewtu.be/watch?v=lmnopqrs_tu'; // Invidious example
const urlV5 = 'https://some-random-proxy.net/watch?v=123456789_A'; // Generic fallback
const urlV6 = 'not a valid url string';
console.log("Video IDs:");
console.log(getYouTubeVideoId(urlV1)); // Output: dQw4w9WgXcQ
console.log(getYouTubeVideoId(urlV2)); // Output: jNQXAC9IVRw
console.log(getYouTubeVideoId(urlV3)); // Output: abcdefghijk
console.log(getYouTubeVideoId(urlV4)); // Output: lmnopqrs_tu
console.log(getYouTubeVideoId(urlV5)); // Output: 123456789_A
console.log(getYouTubeVideoId(urlV6)); // Output: null
console.log(getYouTubeVideoId(null)); // Output: null
// Channel Examples
const urlC1 = 'https://www.youtube.com/channel/UCBR8-60-B28hp2BmDPdntcQ';
const urlC2 = 'https://m.youtube.com/channel/UC_Channel_ID_Chars';
const urlC3 = 'https://youtube.com/c/SomeCustomName'; // Will return null
const urlC4 = 'https://www.youtube.com/@handle'; // Will return null
const urlC5 = 'https://vid.puffyan.us/channel/UCBR8-60-B28hp2BmDPdntcQ'; // 3rd party
console.log("\nChannel IDs (UC-prefixed only):");
console.log(getYouTubeChannelId(urlC1)); // Output: UCBR8-60-B28hp2BmDPdntcQ
console.log(getYouTubeChannelId(urlC2)); // Output: UC_Channel_ID_Chars
console.log(getYouTubeChannelId(urlC3)); // Output: null (custom URL not resolved)
console.log(getYouTubeChannelId(urlC4)); // Output: null (handle not resolved)
console.log(getYouTubeChannelId(urlC5)); // Output: UCBR8-60-B28hp2BmDPdntcQ
// Playlist Examples
const urlP1 = 'https://www.youtube.com/playlist?list=PLBCF2DAC6FFB574DE';
const urlP2 = 'https://www.youtube.com/watch?v=VIDEOID&list=PLIivdWyY5sqL9mXChOLu_U4Q431r9fIGN&index=2';
const urlP3 = 'https://music.youtube.com/playlist?list=PL_Playlist_ID_Chars';
console.log("\nPlaylist IDs (PL-prefixed only):");
console.log(getYouTubePlaylistId(urlP1)); // Output: PLBCF2DAC6FFB574DE
console.log(getYouTubePlaylistId(urlP2)); // Output: PLIivdWyY5sqL9mXChOLu_U4Q431r9fIGN
console.log(getYouTubePlaylistId(urlP3)); // Output: PL_Playlist_ID_Chars
`
#### getYouTubeVideoId(urlString)
* urlString: string | null | undefined - The URL or string potentially containing a YouTube video link or ID.string | null
* Returns: - The 11-character YouTube Video ID if found and valid, otherwise null.
#### getYouTubeChannelId(urlString)
* urlString: string | null | undefined - The URL or string potentially containing a YouTube channel link.string | null
* Returns: - The 24-character YouTube Channel ID (starting with UC) if found and valid, otherwise null./c/
Note: Does not* resolve custom URLs (), legacy usernames (/user/), or handles (/@/).
#### getYouTubePlaylistId(urlString)
* urlString: string | null | undefined - The URL or string potentially containing a YouTube playlist link.string | null
* Returns: - The YouTube Playlist ID (starting with PL) if found and valid, otherwise null.
MIT
---
Надежная Node.js библиотека (ESM) для извлечения ID Видео, Каналов (с префиксом UC) и Плейлистов (с префиксом PL) YouTube из множества различных форматов URL.
* ID Видео:
* Поддерживает стандартные URL youtube.com (/watch, /v/, /embed/, /shorts/, /live/ и т.д.)m.youtube.com
* Поддерживает мобильные URL .youtu.be
* Поддерживает официальный сокращатель и альтернативный y2u.be.youtube-nocookie.com
* Поддерживает встраивания с повышенной приватностью.music.youtube.com
* Поддерживает и kids.youtube.com.i.ytimg.com
* Поддерживает ссылки на изображения с ./e/
* Поддерживает устаревшие форматы (, /vi/, фрагменты каналов пользователей #p/u/...).
* Обрабатывает строки с кодом встраивания
`bash`
npm install opex-yt-idили
yarn add opex-yt-id
`javascript
import {
getYouTubeVideoId,
getYouTubeChannelId,
getYouTubePlaylistId
} from 'opex-yt-id';
// Примеры для Видео
const urlV1 = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
const urlV2 = 'https://youtu.be/jNQXAC9IVRw?t=15';
const urlV3 = '';
const urlV4 = 'https://yewtu.be/watch?v=lmnopqrs_tu'; // Пример Invidious
const urlV5 = 'https://some-random-proxy.net/watch?v=123456789_A'; // Общий запасной вариант
const urlV6 = 'невалидная строка url';
console.log("ID Видео:");
console.log(getYouTubeVideoId(urlV1)); // Вывод: dQw4w9WgXcQ
console.log(getYouTubeVideoId(urlV2)); // Вывод: jNQXAC9IVRw
console.log(getYouTubeVideoId(urlV3)); // Вывод: abcdefghijk
console.log(getYouTubeVideoId(urlV4)); // Вывод: lmnopqrs_tu
console.log(getYouTubeVideoId(urlV5)); // Вывод: 123456789_A
console.log(getYouTubeVideoId(urlV6)); // Вывод: null
console.log(getYouTubeVideoId(null)); // Вывод: null
// Примеры для Каналов
const urlC1 = 'https://www.youtube.com/channel/UCBR8-60-B28hp2BmDPdntcQ';
const urlC2 = 'https://m.youtube.com/channel/UC_Channel_ID_Chars';
const urlC3 = 'https://youtube.com/c/SomeCustomName'; // Вернет null
const urlC4 = 'https://www.youtube.com/@handle'; // Вернет null
const urlC5 = 'https://vid.puffyan.us/channel/UCBR8-60-B28hp2BmDPdntcQ'; // Сторонний фронтенд
console.log("\nID Каналов (только с префиксом UC):");
console.log(getYouTubeChannelId(urlC1)); // Вывод: UCBR8-60-B28hp2BmDPdntcQ
console.log(getYouTubeChannelId(urlC2)); // Вывод: UC_Channel_ID_Chars
console.log(getYouTubeChannelId(urlC3)); // Вывод: null (пользовательский URL не распознан)
console.log(getYouTubeChannelId(urlC4)); // Вывод: null (дескриптор не распознан)
console.log(getYouTubeChannelId(urlC5)); // Вывод: UCBR8-60-B28hp2BmDPdntcQ
// Примеры для Плейлистов
const urlP1 = 'https://www.youtube.com/playlist?list=PLBCF2DAC6FFB574DE';
const urlP2 = 'https://www.youtube.com/watch?v=VIDEOID&list=PLIivdWyY5sqL9mXChOLu_U4Q431r9fIGN&index=2';
const urlP3 = 'https://music.youtube.com/playlist?list=PL_Playlist_ID_Chars';
console.log("\nID Плейлистов (только с префиксом PL):");
console.log(getYouTubePlaylistId(urlP1)); // Вывод: PLBCF2DAC6FFB574DE
console.log(getYouTubePlaylistId(urlP2)); // Вывод: PLIivdWyY5sqL9mXChOLu_U4Q431r9fIGN
console.log(getYouTubePlaylistId(urlP3)); // Вывод: PL_Playlist_ID_Chars
`
#### getYouTubeVideoId(urlString)
* urlString: string | null | undefined - URL или строка, потенциально содержащая ссылку или ID видео YouTube.string | null
* Возвращает: - 11-значный ID видео YouTube, если найден и действителен, иначе null.
#### getYouTubeChannelId(urlString)
* urlString: string | null | undefined - URL или строка, потенциально содержащая ссылку на канал YouTube.string | null
* Возвращает: - 24-значный ID канала YouTube (начинающийся с UC), если найден и действителен, иначе null./c/
* Примечание: Не распознает пользовательские URL (), устаревшие имена пользователей (/user/) или дескрипторы (/@/).
#### getYouTubePlaylistId(urlString)
* urlString: string | null | undefined - URL или строка, потенциально содержащая ссылку на плейлист YouTube.string | null
* Возвращает: - ID плейлиста YouTube (начинающийся с PL), если найден и действителен, иначе null`.
MIT