Created
October 22, 2022 16:47
-
-
Save duythien0912/11865b48be097fc5300217ed4d722bd7 to your computer and use it in GitHub Desktop.
get first media from mux.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert' as convert; | |
import 'package:http/http.dart' as http; | |
void main(List<String> arguments) async { | |
// This example uses the Google Books API to search | |
// for books about HTTP. For details, see | |
// https://developers.google.com/books/docs/overview | |
final url = Uri.https('stream.mux.com', | |
'/onxADFeJBbrs6tdocM800ezUau00TinjF7tfrmo9WQCQY.m3u8', {}); | |
// Await the HTTP GET response, then decode the | |
// JSON data it contains. | |
final response = await http.get(url); | |
if (response.statusCode != 200) { | |
print('Request failed with status: ${response.statusCode}.'); | |
return; | |
} | |
print('response.body: ${response.body}.'); | |
if (!response.body.toString().contains('.m3u8')) return; | |
List<String> links = response.body.toString().split('\n'); | |
List<String> linksM3u8 = <String>[]; | |
for (String link in links) { | |
if (link.contains('.m3u8')) { | |
print('link: ${link}.'); | |
linksM3u8.add(link); | |
} | |
} | |
if (linksM3u8.isEmpty) return; | |
final cacheSubLink = Uri.tryParse(linksM3u8.first); | |
if (cacheSubLink == null) return; | |
final responseSublink = await http.get(cacheSubLink); | |
if (responseSublink.statusCode != 200) { | |
print( | |
'Request responseSublink failed with status: ${responseSublink.statusCode}.'); | |
return; | |
} | |
print('responseSublink.body: ${responseSublink.body}.'); | |
final bodySubM3u8 = responseSublink.body.toString(); | |
print("bodySubM3u8: ${bodySubM3u8} ${bodySubM3u8.contains('.ts')}"); | |
if (!bodySubM3u8.contains('.ts')) return; | |
List<String> linksSubM3u8 = bodySubM3u8.split('\n'); | |
List<String> linksTs = <String>[]; | |
for (String link in linksSubM3u8) { | |
if (link.contains('.ts')) { | |
print('link: ${link}.'); | |
linksTs.add(link); | |
} | |
} | |
print("linksTs: ${linksTs}"); | |
if (linksTs.isEmpty) return; | |
final cacheTsLink = Uri.tryParse(linksTs.first); | |
if (cacheTsLink == null) return; | |
print("cacheTsLink: ${cacheTsLink}"); | |
final responseTslink = await http.get(cacheTsLink); | |
if (responseTslink.statusCode != 200) { | |
print('Request responseTslink failed with status: ${responseTslink.statusCode}.'); | |
return; | |
} | |
print('responseTslink.body: ${responseTslink.statusCode}.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment