/youtube-video-id

Extract the YouTube video ID from string or url.

Primary LanguageJavaScriptMIT LicenseMIT

youtube-video-id

NPM

NPM version Build Status Coverage Status

Extracts the YouTube video ID from a url or string:

YouTubeVideoId(string)

Installation

NPM:

$ npm install youtube-video-id

Yarn:

$ yarn add youtube-video-id

CDN:

<script src="https://unpkg.com/youtube-video-id@latest/dist/youtube-video-id.min.js"></script>

Examples

Server:

var YouTubeVideoId = require('youtube-video-id');

// returns video ID
YouTubeVideoId('https://www.youtube.com/watch?v=X3pTXG9a1oQ'); // 'X3pTXG9a1oQ'
YouTubeVideoId('https://youtu.be/X3pTXG9a1oQ'); // 'X3pTXG9a1oQ'
YouTubeVideoId('X3pTXG9a1oQ'); // 'X3pTXG9a1oQ'

Client:

<script src="https://unpkg.com/youtube-video-id@latest/dist/youtube-video-id.min.js"></script>
<script>
  var videoId = YouTubeVideoId('https://www.youtube.com/watch?v=F2uovvU-dLA')
  console.log(videoId);
</script>

Also see JSFiddle and other examples.

Testing

$ npm test
$ npm run lint

License

MIT