youtube_explode/lib/src/videos/closed_captions/closed_caption_client.dart

70 lines
2.6 KiB
Dart
Raw Normal View History

2020-06-03 23:02:21 +02:00
import '../../extensions/helpers_extension.dart';
2020-11-01 15:05:19 +01:00
import '../../reverse_engineering/responses/responses.dart'
hide ClosedCaption, ClosedCaptionPart, ClosedCaptionTrack;
2020-06-03 23:02:21 +02:00
import '../../reverse_engineering/youtube_http_client.dart';
import '../videos.dart';
import 'closed_caption.dart';
2020-11-06 22:46:47 +01:00
import 'closed_caption_format.dart';
2020-06-03 23:02:21 +02:00
import 'closed_caption_manifest.dart';
import 'closed_caption_part.dart';
import 'closed_caption_track.dart';
import 'closed_caption_track_info.dart';
import 'language.dart';
/// Queries related to closed captions of YouTube videos.
class ClosedCaptionClient {
final YoutubeHttpClient _httpClient;
/// Initializes an instance of [ClosedCaptionClient]
ClosedCaptionClient(this._httpClient);
/// Gets the manifest that contains information
/// about available closed caption tracks in the specified video.
2020-12-30 15:00:11 +01:00
Future<ClosedCaptionManifest> getManifest(
dynamic videoId,
{@Deprecated('Not used anymore, use track.isAutoGenerated to see if a track is autogenerated or not.') // ignore: lines_longer_than_80_chars
bool autoGenerated = false,
List<ClosedCaptionFormat> formats = const [
ClosedCaptionFormat.srv1,
ClosedCaptionFormat.srv2,
ClosedCaptionFormat.srv3,
ClosedCaptionFormat.ttml,
ClosedCaptionFormat.vtt
]}) async {
2020-06-05 20:08:04 +02:00
videoId = VideoId.fromString(videoId);
2020-12-30 15:00:11 +01:00
var tracks = <ClosedCaptionTrackInfo>{};
var videoInfoResponse =
await VideoInfoResponse.get(_httpClient, videoId.value);
var playerResponse = videoInfoResponse.playerResponse;
2020-11-01 15:05:19 +01:00
2021-03-11 14:20:10 +01:00
for (final track in playerResponse.closedCaptionTrack) {
for (final ext in formats) {
2020-12-30 15:00:11 +01:00
tracks.add(ClosedCaptionTrackInfo(
Uri.parse(track.url)
.replaceQueryParameters({'fmt': ext.formatCode}),
Language(track.languageCode, track.languageName),
isAutoGenerated: track.autoGenerated,
format: ext));
2020-11-01 15:05:19 +01:00
}
}
2020-06-03 23:02:21 +02:00
return ClosedCaptionManifest(tracks);
}
2020-07-16 18:52:27 +02:00
/// Gets the actual closed caption track which is
/// identified by the specified metadata.
2020-06-03 23:02:21 +02:00
Future<ClosedCaptionTrack> get(ClosedCaptionTrackInfo trackInfo) async {
2020-11-16 11:52:26 +01:00
var response =
await ClosedCaptionTrackResponse.get(_httpClient, trackInfo.url);
2020-06-03 23:02:21 +02:00
var captions = response.closedCaptions
.where((e) => !e.text.isNullOrWhiteSpace)
.map((e) => ClosedCaption(e.text, e.offset, e.duration,
2021-03-11 14:20:10 +01:00
e.parts.map((f) => ClosedCaptionPart(f.text, f.offset))));
2020-06-03 23:02:21 +02:00
return ClosedCaptionTrack(captions);
}
2020-07-16 18:52:27 +02:00
2020-11-01 15:05:19 +01:00
/// Returns the subtitles as a string.
Future<String> getSubTitles(ClosedCaptionTrackInfo trackInfo) =>
_httpClient.getString(trackInfo.url);
2020-06-03 23:02:21 +02:00
}