getIdFromUrl function

String? getIdFromUrl(
  1. String url
)

Gets the video ID from any valid YouTube video link.

Returns null if the video link is not valid.

Example:

getIdFromUrl('https://www.youtube.com/watch?v=5KlnlCq2M5Q') -> '5KlnlCq2M5Q'

Implementation

String? getIdFromUrl(String url) {
  const youtubeComHosts = ['youtube.com', 'www.youtube.com', 'm.youtube.com'];

  if (url.contains(' ')) {
    return null;
  }

  late final Uri uri;
  try {
    uri = Uri.parse(url);
  } catch (e) {
    return null;
  }

  if (!['https', 'http'].contains(uri.scheme)) {
    return null;
  }

  // youtube.com/watch?v=xxxxxxxxxxx
  if (youtubeComHosts.contains(uri.host) &&
      uri.pathSegments.isNotEmpty &&
      uri.pathSegments.first == 'watch' &&
      uri.queryParameters.containsKey('v')) {
    final videoId = uri.queryParameters['v']!;
    return _isValidId(videoId) ? videoId : null;
  }

  // youtu.be/xxxxxxxxxxx
  if (uri.host == 'youtu.be' && uri.pathSegments.isNotEmpty) {
    final videoId = uri.pathSegments.first;
    return _isValidId(videoId) ? videoId : null;
  }

  // youtube.com/shorts/xxxxxxxxxxx
  // youtube.com/embed/xxxxxxxxxxx
  // youtube.com/live/xxxxxxxxxxx
  if (youtubeComHosts.contains(uri.host) &&
      uri.pathSegments.length == 2 &&
      ['shorts', 'embed', 'live'].contains(uri.pathSegments.first)) {
    final videoId = uri.pathSegments[1];
    return _isValidId(videoId) ? videoId : null;
  }

  return null;
}