Implement getSrt

This commit is contained in:
Mattia 2020-07-16 18:52:27 +02:00
parent ac7fe68906
commit 38c41e8f0b
7 changed files with 76 additions and 1 deletions

2
dartdoc_options.yaml Normal file
View File

@ -0,0 +1,2 @@
dartdoc:
showUndocumentedCategories: true

View File

@ -1,3 +1,6 @@
/// APIs related to YouTube channels.
///
/// {@category Channels}
library youtube_explode.channels;
export 'channel.dart';

View File

@ -1,3 +1,6 @@
/// APIs related to YouTube playlists.
///
/// {@category Playlists}
library youtube_explode.playlists;
export 'playlist.dart';

View File

@ -1,3 +1,6 @@
/// APIs related to YouTube search queries.
///
/// {@category Search}
library youtube_explode.search;
export 'related_query.dart';

View File

@ -33,7 +33,8 @@ class ClosedCaptionClient {
return ClosedCaptionManifest(tracks);
}
///
/// Gets the actual closed caption track which is
/// identified by the specified metadata.
Future<ClosedCaptionTrack> get(ClosedCaptionTrackInfo trackInfo) async {
var response = await ClosedCaptionTrackResponse.get(
_httpClient, trackInfo.url.toString());
@ -44,4 +45,63 @@ class ClosedCaptionClient {
e.getParts().map((f) => ClosedCaptionPart(f.text, f.offset))));
return ClosedCaptionTrack(captions);
}
Future<String> getSrt(ClosedCaptionTrackInfo trackInfo) async {
var track = await get(trackInfo);
var buffer = StringBuffer();
for (var i = 0; i < track.captions.length; i++) {
var caption = track.captions[i];
// Line number
buffer.writeln('${i + 1}');
// Time start --> time end
buffer.write(caption.offset.toSrtFormat());
buffer.write(' --> ');
buffer.write(caption.end.toSrtFormat());
buffer.writeln();
// Actual text
buffer.writeln(caption.text);
buffer.writeln();
}
return buffer.toString();
}
}
extension on Duration {
String toSrtFormat() {
String threeDigits(int n) {
if (n >= 1000) {
return n.toString().substring(0, 3);
}
if (n >= 100) {
return '$n';
}
if (n >= 10) {
return '0$n';
}
return '00$n';
}
String twoDigits(int n) {
if (n >= 10) {
return '$n';
}
return '0$n';
}
if (inMicroseconds < 0) {
return "-${-this}";
}
var twoDigitHours = twoDigits(inHours);
var twoDigitMinutes =
twoDigits(inMinutes.remainder(Duration.minutesPerHour));
var twoDigitSeconds =
twoDigits(inSeconds.remainder(Duration.secondsPerMinute));
var fourDigitsUs =
threeDigits(inMilliseconds.remainder(1000));
return "$twoDigitHours:$twoDigitMinutes:$twoDigitSeconds,$fourDigitsUs";
}
}

View File

@ -1,3 +1,6 @@
/// APIs related to YouTube videos.
///
/// {@category Videos}
library youtube_explode.videos;
export 'comments/comments.dart';

View File

@ -1,3 +1,4 @@
/// Provides all the APIs implemented by this library.
library youtube_explode;
export 'src/channels/channels.dart';