Implement getSrt
This commit is contained in:
parent
ac7fe68906
commit
38c41e8f0b
|
@ -0,0 +1,2 @@
|
||||||
|
dartdoc:
|
||||||
|
showUndocumentedCategories: true
|
|
@ -1,3 +1,6 @@
|
||||||
|
/// APIs related to YouTube channels.
|
||||||
|
///
|
||||||
|
/// {@category Channels}
|
||||||
library youtube_explode.channels;
|
library youtube_explode.channels;
|
||||||
|
|
||||||
export 'channel.dart';
|
export 'channel.dart';
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
/// APIs related to YouTube playlists.
|
||||||
|
///
|
||||||
|
/// {@category Playlists}
|
||||||
library youtube_explode.playlists;
|
library youtube_explode.playlists;
|
||||||
|
|
||||||
export 'playlist.dart';
|
export 'playlist.dart';
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
/// APIs related to YouTube search queries.
|
||||||
|
///
|
||||||
|
/// {@category Search}
|
||||||
library youtube_explode.search;
|
library youtube_explode.search;
|
||||||
|
|
||||||
export 'related_query.dart';
|
export 'related_query.dart';
|
||||||
|
|
|
@ -33,7 +33,8 @@ class ClosedCaptionClient {
|
||||||
return ClosedCaptionManifest(tracks);
|
return ClosedCaptionManifest(tracks);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
/// Gets the actual closed caption track which is
|
||||||
|
/// 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.toString());
|
||||||
|
@ -44,4 +45,63 @@ class ClosedCaptionClient {
|
||||||
e.getParts().map((f) => ClosedCaptionPart(f.text, f.offset))));
|
e.getParts().map((f) => ClosedCaptionPart(f.text, f.offset))));
|
||||||
return ClosedCaptionTrack(captions);
|
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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
/// APIs related to YouTube videos.
|
||||||
|
///
|
||||||
|
/// {@category Videos}
|
||||||
library youtube_explode.videos;
|
library youtube_explode.videos;
|
||||||
|
|
||||||
export 'comments/comments.dart';
|
export 'comments/comments.dart';
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
/// Provides all the APIs implemented by this library.
|
||||||
library youtube_explode;
|
library youtube_explode;
|
||||||
|
|
||||||
export 'src/channels/channels.dart';
|
export 'src/channels/channels.dart';
|
||||||
|
|
Loading…
Reference in New Issue