Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add implementation to handle custom vimeo url #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions lib/src/controllers/pod_getx_video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ class PodGetXVideoController extends _PodGesturesController {
playingVideoUrl = _url;

break;
case PodVideoPlayerType.vimeoCustomUrl:
await getQualityUrlsFromVimeoUrl(playVideoFrom.dataSource!);
final _url = await getUrlFromVideoQualityUrls(
qualityList: podPlayerConfig.videoQualityPriority,
videoUrls: vimeoOrVideoUrls,
);

_videoCtr = VideoPlayerController.network(
_url,
closedCaptionFile: playVideoFrom.closedCaptionFile,
formatHint: playVideoFrom.formatHint,
videoPlayerOptions: playVideoFrom.videoPlayerOptions,
httpHeaders: playVideoFrom.httpHeaders,
);
playingVideoUrl = _url;

break;

case PodVideoPlayerType.asset:

///
Expand Down
12 changes: 12 additions & 0 deletions lib/src/controllers/pod_video_quality_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class _PodVideoQualityController extends _PodVideoController {
}
}

Future<void> getQualityUrlsFromVimeoUrl(String videoUrl) async {
try {
podVideoStateChanger(PodVideoState.loading);
final _vimeoVideoUrls = await VideoApis.getVimeoVideoQualityUrlsFromCustomLink(videoUrl);

///
vimeoOrVideoUrls = _vimeoVideoUrls ?? [];
} catch (e) {
rethrow;
}
}

Future<void> getQualityUrlsFromVimeoPrivateId(
String videoId,
Map<String, String> httpHeader,
Expand Down
18 changes: 18 additions & 0 deletions lib/src/models/play_video_from.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ class PlayVideoFrom {
httpHeaders: httpHeaders,
);
}

factory PlayVideoFrom.vimeoCustomUrl(
String dataSource, {
VideoFormat? formatHint,
Future<ClosedCaptionFile>? closedCaptionFile,
VideoPlayerOptions? videoPlayerOptions,
Map<String, String> httpHeaders = const {},
}) {
return PlayVideoFrom._(
playerType: PodVideoPlayerType.vimeoCustomUrl,
dataSource: dataSource,
formatHint: formatHint,
closedCaptionFile: closedCaptionFile,
videoPlayerOptions: videoPlayerOptions,
httpHeaders: httpHeaders,
);
}

factory PlayVideoFrom.youtube(
String dataSource, {
bool live = false,
Expand Down
1 change: 1 addition & 0 deletions lib/src/utils/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ enum PodVideoPlayerType {
vimeo,
youtube,
vimeoPrivateVideos,
vimeoCustomUrl,
}
54 changes: 40 additions & 14 deletions lib/src/utils/video_apis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,39 @@ String podErrorString(String val) {
}

class VideoApis {
static Future<List<VideoQalityUrls>?> getVimeoVideoQualityUrlsFromCustomLink(
String videoLink, {
Map<String, String>? headers,
}) async {
try {
final response = await http.get(
Uri.parse(videoLink),
headers: {'Accept': 'application/json', 'Content-type': 'application/json', ...headers ?? {}},
);
final jsonData = jsonDecode(response.body)['files'];

return _parseVimeoPrivateUrlResponse(jsonData);
} catch (error) {
if (error.toString().contains('XMLHttpRequest')) {
log(
podErrorString(
'(INFO) To play vimeo video in WEB, Please enable CORS in your browser',
),
);
}
debugPrint('===== VIMEO API ERROR: $error ==========');
rethrow;
}
}

static Future<List<VideoQalityUrls>?> getVimeoVideoQualityUrls(
String videoId,
) async {
try {
final response = await http.get(
Uri.parse('https://player.vimeo.com/video/$videoId/config'),
);
final jsonData =
jsonDecode(response.body)['request']['files']['progressive'];
final jsonData = jsonDecode(response.body)['request']['files']['progressive'];
return List.generate(
jsonData.length,
(index) => VideoQalityUrls(
Expand Down Expand Up @@ -53,16 +77,7 @@ class VideoApis {
);
final jsonData = jsonDecode(response.body)['files'];

final List<VideoQalityUrls> list = [];
for (int i = 0; i < jsonData.length; i++) {
final String quality =
(jsonData[i]['rendition'] as String?)?.split('p').first ?? '0';
final int? number = int.tryParse(quality);
if (number != null && number != 0) {
list.add(VideoQalityUrls(quality: number, url: jsonData[i]['link']));
}
}
return list;
return _parseVimeoPrivateUrlResponse(jsonData);
} catch (error) {
if (error.toString().contains('XMLHttpRequest')) {
log(
Expand Down Expand Up @@ -94,8 +109,7 @@ class VideoApis {
),
);
} else {
final manifest =
await yt.videos.streamsClient.getManifest(youtubeIdOrUrl);
final manifest = await yt.videos.streamsClient.getManifest(youtubeIdOrUrl);
urls.addAll(
manifest.muxed.map(
(element) => VideoQalityUrls(
Expand All @@ -120,4 +134,16 @@ class VideoApis {
rethrow;
}
}

static List<VideoQalityUrls> _parseVimeoPrivateUrlResponse(jsonData) {
final List<VideoQalityUrls> list = [];
for (int i = 0; i < jsonData.length; i++) {
final String quality = (jsonData[i]['rendition'] as String?)?.split('p').first ?? '0';
final int? number = int.tryParse(quality);
if (number != null && number != 0) {
list.add(VideoQalityUrls(quality: number, url: jsonData[i]['link']));
}
}
return list;
}
}