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