Update closed captions test
This commit is contained in:
parent
a6f2dcf272
commit
9c514ceadc
|
@ -23,8 +23,8 @@ class ClosedCaptionTrackResponse {
|
|||
|
||||
///
|
||||
static Future<ClosedCaptionTrackResponse> get(
|
||||
YoutubeHttpClient httpClient, String url) {
|
||||
var formatUrl = Uri.parse(url).replaceQueryParameters({'fmt': 'srv3'});
|
||||
YoutubeHttpClient httpClient, Uri url) {
|
||||
var formatUrl = url.replaceQueryParameters({'fmt': 'srv3'});
|
||||
return retry(() async {
|
||||
var raw = await httpClient.getString(formatUrl);
|
||||
return ClosedCaptionTrackResponse.parse(raw);
|
||||
|
|
|
@ -79,7 +79,7 @@ class ClosedCaptionClient {
|
|||
/// identified by the specified metadata.
|
||||
Future<ClosedCaptionTrack> get(ClosedCaptionTrackInfo trackInfo) async {
|
||||
var response = await ClosedCaptionTrackResponse.get(
|
||||
_httpClient, trackInfo.url.toString());
|
||||
_httpClient, trackInfo.url);
|
||||
|
||||
var captions = response.closedCaptions
|
||||
.where((e) => !e.text.isNullOrWhiteSpace)
|
||||
|
@ -88,16 +88,6 @@ class ClosedCaptionClient {
|
|||
return ClosedCaptionTrack(captions);
|
||||
}
|
||||
|
||||
/// Auto translated a closed caption track.
|
||||
ClosedCaptionTrackInfo autoTranslate(
|
||||
ClosedCaptionTrackInfo trackInfo, String lang) {
|
||||
return ClosedCaptionTrackInfo(
|
||||
trackInfo.url.replaceQueryParameters({'tlang': lang}),
|
||||
Language(lang, ''),
|
||||
isAutoGenerated: trackInfo.isAutoGenerated,
|
||||
format: trackInfo.format);
|
||||
}
|
||||
|
||||
/// Returns the subtitles as a string.
|
||||
Future<String> getSubTitles(ClosedCaptionTrackInfo trackInfo) =>
|
||||
_httpClient.getString(trackInfo.url);
|
||||
|
|
|
@ -12,14 +12,16 @@ class ClosedCaptionManifest {
|
|||
ClosedCaptionManifest(Iterable<ClosedCaptionTrackInfo> tracks)
|
||||
: tracks = UnmodifiableListView(tracks);
|
||||
|
||||
/// Gets the closed caption track in the specified language.
|
||||
/// Returns null if not found.
|
||||
ClosedCaptionTrackInfo getByLanguage(String language) {
|
||||
/// Gets all the closed caption tracks in the specified language and format.
|
||||
/// Returns an empty list of no track is found.
|
||||
List<ClosedCaptionTrackInfo> getByLanguage(String language,
|
||||
{ClosedCaptionFormat format}) {
|
||||
language = language.toLowerCase();
|
||||
return tracks.firstWhere(
|
||||
(e) =>
|
||||
e.language.code.toLowerCase() == language ||
|
||||
e.language.name.toLowerCase() == language,
|
||||
orElse: () => null);
|
||||
return tracks
|
||||
.where((e) =>
|
||||
(e.language.code.toLowerCase() == language ||
|
||||
e.language.name.toLowerCase() == language) &&
|
||||
(format == null || e.format == format))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:equatable/equatable.dart';
|
||||
|
||||
import '../../extensions/helpers_extension.dart';
|
||||
import 'language.dart';
|
||||
|
||||
/// Metadata associated with a certain [ClosedCaptionTrack]
|
||||
|
@ -21,6 +22,17 @@ class ClosedCaptionTrackInfo extends Equatable {
|
|||
{this.isAutoGenerated = false, this.format})
|
||||
: assert(format != null);
|
||||
|
||||
/// Returns this auto-translated to another language.
|
||||
/// Keeping the same format.
|
||||
ClosedCaptionTrackInfo autoTranslate(
|
||||
ClosedCaptionTrackInfo trackInfo, String lang) {
|
||||
return ClosedCaptionTrackInfo(
|
||||
trackInfo.url.replaceQueryParameters({'tlang': lang}),
|
||||
Language(lang, ''),
|
||||
isAutoGenerated: trackInfo.isAutoGenerated,
|
||||
format: trackInfo.format);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => 'CC Track ($language)';
|
||||
|
||||
|
|
|
@ -22,11 +22,11 @@ void main() {
|
|||
|
||||
expect(track.captions, isNotEmpty);
|
||||
});
|
||||
test('Get closed caption track at a specific time', () async {
|
||||
test('Get auto-generated closed caption track at a specific time', () async {
|
||||
var manifest = await yt.videos.closedCaptions
|
||||
.getManifest('https://www.youtube.com/watch?v=ppJy5uGZLi4');
|
||||
.getManifest('https://www.youtube.com/watch?v=ppJy5uGZLi4', autoGenerated: true);
|
||||
var trackInfo = manifest.getByLanguage('en');
|
||||
var track = await yt.videos.closedCaptions.get(trackInfo);
|
||||
var track = await yt.videos.closedCaptions.get(trackInfo.first);
|
||||
var caption =
|
||||
track.getByTime(const Duration(hours: 0, minutes: 13, seconds: 22));
|
||||
var captionPart = caption.getPartByTime(const Duration(milliseconds: 200));
|
||||
|
|
Loading…
Reference in New Issue