Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/videos/video_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class VideoId with _$VideoId {
static final _embedMatchExp = RegExp(r'youtube\..+?/embed/(.*?)(?:\?|&|/|$)');
static final _shortsMatchExp =
RegExp(r'youtube\..+/shorts/([A-Za-z0-9-_]+$)');
static final _liveMatchExp = RegExp(r'youtube\..+?/live/(.*?)(?:\?|&|/|$)');

/// Initializes an instance of [VideoId] with a url or video id.
factory VideoId(String idOrUrl) {
Expand Down Expand Up @@ -97,6 +98,13 @@ abstract class VideoId with _$VideoId {
if (!shortsMatch.isNullOrWhiteSpace && validateVideoId(shortsMatch!)) {
return shortsMatch;
}

// https://www.youtube.com/live/yIVRs6YSbOM
final liveMatch = _liveMatchExp.firstMatch(url)?.group(1);
if (!liveMatch.isNullOrWhiteSpace && validateVideoId(liveMatch!)) {
return liveMatch;
}

return null;
}
}
2 changes: 2 additions & 0 deletions test/video_id_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void main() {
['youtube.com/watch?v=yIVRs6YSbOM', 'yIVRs6YSbOM'],
['youtu.be/yIVRs6YSbOM', 'yIVRs6YSbOM'],
['youtube.com/embed/yIVRs6YSbOM', 'yIVRs6YSbOM'],
['youtube.com/live/yIVRs6YSbOM', 'yIVRs6YSbOM'],
}) {
test('Video - $val', () {
expect(VideoId(val[0]).value, val[1]);
Expand All @@ -34,6 +35,7 @@ void main() {
'youtube.com/xxx?v=pI2I2zqzeKg',
'youtu.be/watch?v=xxx',
'youtube.com/embed',
'youtube.com/live'
}) {
test('VideoURL - $val', () {
expect(() => VideoId(val), throwsArgumentError);
Expand Down