如何下载 hls 视频并使其离线可用。我尝试了颤振下载器。但无法下载hls视频。
我尝试使用 hls 解析器,但不知道如何从中提取可下载的网址。
你可以使用https://pub.dev/packages/better_player更好的播放器,它可以播放和缓存hls视频。
下载你需要的hls视频3个包-
Future<void> downloadFile(BuildContext context, String url, String name, String folderName) async {
final dio = Dio();
//configure video file name
name = "$name.m3u8";
///get playList or get regulation form video
Response playListResponse = await dio.get(url);
log('get playlist ${playListResponse.data}');
///remove /playlist.m3u8 from main video url
String baseUrl = url.replaceAll('/playlist.m3u8', '');
///
///then will concatenation base to highest regulation from videoPlay playList response
///use regular expression
///Like us: $baseUrl/1280x720/video.m3u8
///
String playListDownloadUrl = '$baseUrl/${getHighestRegulation('${playListResponse.data}')}';
log('log main video - playListDownloadUrl $playListDownloadUrl');
final appStorage = await getApplicationDocumentsDirectory();
await Directory('${appStorage.path}/$folderName').create();
// Download the playlist file.
final playlistFile = File("${appStorage.path}/$folderName/$name");
await dio.download(playListDownloadUrl, playlistFile.path,
options: Options(
responseType: ResponseType.bytes,
));
log('download saved path: ${playlistFile.path}');
// Parse the playlist file.
final playList = await HlsPlaylistParser.create().parseString(playlistFile.uri, await playlistFile.readAsString());
playList as HlsMediaPlaylist;
///then need video base url
String videoBaseUrl = playListDownloadUrl.replaceAll('/video.m3u8', '');
log('log videoBaseUrl replace url $videoBaseUrl');
// Download the video segments or .ts file in parallel.
final futures = <Future<void>>[];
for (var segment in playList.segments) {
final segmentFile = File('${appStorage.path}/$folderName/${segment.url}');
log('log ts url $videoBaseUrl/${segment.url}');
futures.add(dio.download('$videoBaseUrl/${segment.url}', segmentFile.path,
options: Options(
responseType: ResponseType.bytes,
)));
}
// Wait for all of the downloads to complete.ß
await Future.wait(futures);
// Show a snack-bar message to indicate that the download is complete.
}
String getHighestRegulation(String text){
RegExp exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
Iterable<RegExpMatch> matches = exp.allMatches(text);
String lastMatch = text.substring(matches.last.start, matches.last.end);
return lastMatch;
}
但是此代码仅适用于 Android 平台