getVideoQualityUrlsFromYoutube method

Future<List<YoutubeVideoQualityUrl>> getVideoQualityUrlsFromYoutube(
  1. String youtubeIdOrUrl
)

Get actual video quality urls from YouTube.

youtubeIdOrUrl - YouTube video url or id.

Implementation

Future<List<YoutubeVideoQualityUrl>> getVideoQualityUrlsFromYoutube(
  String youtubeIdOrUrl,
) async {
  try {
    final yt = YoutubeExplode();
    final muxed =
        (await yt.videos.streamsClient.getManifest(youtubeIdOrUrl)).muxed;
    final urls = muxed
        .map((element) => YoutubeVideoQualityUrl(
              quality: int.parse(element.qualityLabel.split('p')[0]),
              url: element.url.toString(),
            ))
        .toList();

    // Close the YoutubeExplode's http client.
    yt.close();

    return YtHelper.sortQualityVideoUrls(urls);
  } catch (error) {
    if (error.toString().contains('XMLHttpRequest')) {
      Logger.i(
        'YtHelper',
        '(INFO) To play youtube video in WEB, Please enable CORS in your browser',
      );
    }
    Logger.e(
      'YtHelper',
      '===== YOUTUBE API ERROR: $error ==========',
    );
    rethrow;
  }
}