parseVideoId static method
Parses a video id from url or if given a valid id as url returns itself. Returns null if the id couldn't be extracted.
Implementation
static String? parseVideoId(String url) {
if (url.isNullOrWhiteSpace) {
return null;
}
if (validateVideoId(url)) {
return url;
}
// https://www.youtube.com/watch?v=yIVRs6YSbOM
final regMatch = _regMatchExp.firstMatch(url)?.group(1);
if (!regMatch.isNullOrWhiteSpace && validateVideoId(regMatch!)) {
return regMatch;
}
// https://youtu.be/yIVRs6YSbOM
final shortMatch = _shortMatchExp.firstMatch(url)?.group(1);
if (!shortMatch.isNullOrWhiteSpace && validateVideoId(shortMatch!)) {
return shortMatch;
}
// https://www.youtube.com/embed/yIVRs6YSbOM
final embedMatch = _embedMatchExp.firstMatch(url)?.group(1);
if (!embedMatch.isNullOrWhiteSpace && validateVideoId(embedMatch!)) {
return embedMatch;
}
// https://www.youtube.com/shorts/yIVRs6YSbOM
final shortsMatch = _shortsMatchExp.firstMatch(url)?.group(1);
if (!shortsMatch.isNullOrWhiteSpace && validateVideoId(shortsMatch!)) {
return shortsMatch;
}
return null;
}