youtube_explode/lib/src/reverse_engineering/dash_manifest.dart

97 lines
2.3 KiB
Dart
Raw Normal View History

2020-05-31 23:36:23 +02:00
import 'package:xml/xml.dart' as xml;
2020-06-03 13:18:37 +02:00
2021-07-21 02:06:02 +02:00
import '../retry.dart';
import 'models/stream_info_provider.dart';
2021-07-24 01:31:14 +02:00
import 'youtube_http_client.dart';
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-05-31 23:36:23 +02:00
class DashManifest {
static final _urlSignatureExp = RegExp(r'/s/(.*?)(?:/|$)');
final xml.XmlDocument _root;
2020-07-16 20:02:54 +02:00
///
2021-03-11 14:20:10 +01:00
late final Iterable<_StreamInfo> streams = _root
2020-05-31 23:36:23 +02:00
.findElements('Representation')
.where((e) => e
.findElements('Initialization')
.first
2021-03-11 14:20:10 +01:00
.getAttribute('sourceURL')!
2020-05-31 23:36:23 +02:00
.contains('sq/'))
.map((e) => _StreamInfo(e));
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
DashManifest(this._root);
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
// ignore: deprecated_member_use
2020-05-31 23:36:23 +02:00
DashManifest.parse(String raw) : _root = xml.parse(raw);
2020-07-16 20:02:54 +02:00
///
2020-06-03 13:18:37 +02:00
static Future<DashManifest> get(YoutubeHttpClient httpClient, dynamic url) {
2020-06-05 16:17:08 +02:00
return retry(() async {
2020-05-31 23:36:23 +02:00
var raw = await httpClient.getString(url);
return DashManifest.parse(raw);
});
}
2020-07-16 20:02:54 +02:00
///
2021-03-11 14:20:10 +01:00
static String? getSignatureFromUrl(String url) =>
2020-06-03 13:18:37 +02:00
_urlSignatureExp.firstMatch(url)?.group(1);
2020-05-31 23:36:23 +02:00
}
class _StreamInfo extends StreamInfoProvider {
2020-06-05 16:17:08 +02:00
static final _contentLenExp = RegExp(r'[/\?]clen[/=](\d+)');
2020-05-31 23:36:23 +02:00
2021-03-11 14:20:10 +01:00
final xml.XmlElement root;
2020-05-31 23:36:23 +02:00
2021-03-11 14:20:10 +01:00
_StreamInfo(this.root);
2020-05-31 23:36:23 +02:00
@override
StreamSource get source => StreamSource.dash;
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final int tag = int.parse(root.getAttribute('id')!);
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final String url = root.getAttribute('BaseURL')!;
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final int contentLength = int.parse(
(root.getAttribute('contentLength') ??
_contentLenExp.firstMatch(url)?.group(1))!);
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final int bitrate = int.parse(root.getAttribute('bandwidth')!);
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final String? container = '';
2021-07-24 01:31:14 +02:00
2021-03-11 14:20:10 +01:00
/*
Uri.decodeFull((_containerExp.firstMatch(url)?.group(1))!);*/
2020-05-31 23:36:23 +02:00
2021-03-11 14:20:10 +01:00
late final bool isAudioOnly =
root.findElements('AudioChannelConfiguration').isNotEmpty;
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final String? audioCodec =
isAudioOnly ? null : root.getAttribute('codecs');
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final String? videoCodec =
isAudioOnly ? root.getAttribute('codecs') : null;
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final int videoWidth = int.parse(root.getAttribute('width')!);
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final int videoHeight = int.parse(root.getAttribute('height')!);
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
@override
2021-03-11 14:20:10 +01:00
late final int framerate = int.parse(root.getAttribute('framerate')!);
// TODO: Implement this
@override
late final String? videoQualityLabel = null;
2020-05-31 23:36:23 +02:00
}