Skip to content

Commit

Permalink
Implement getSrt
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexer10 committed Jul 16, 2020
1 parent ac7fe68 commit 38c41e8
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dartdoc_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dartdoc:
showUndocumentedCategories: true
3 changes: 3 additions & 0 deletions lib/src/channels/channels.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// APIs related to YouTube channels.
///
/// {@category Channels}
library youtube_explode.channels;

export 'channel.dart';
Expand Down
3 changes: 3 additions & 0 deletions lib/src/playlists/playlists.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// APIs related to YouTube playlists.
///
/// {@category Playlists}
library youtube_explode.playlists;

export 'playlist.dart';
Expand Down
3 changes: 3 additions & 0 deletions lib/src/search/search.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// APIs related to YouTube search queries.
///
/// {@category Search}
library youtube_explode.search;

export 'related_query.dart';
Expand Down
62 changes: 61 additions & 1 deletion lib/src/videos/closed_captions/closed_caption_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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";
}
}
3 changes: 3 additions & 0 deletions lib/src/videos/videos.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// APIs related to YouTube videos.
///
/// {@category Videos}
library youtube_explode.videos;

export 'comments/comments.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/youtube_explode_dart.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Provides all the APIs implemented by this library.
library youtube_explode;

export 'src/channels/channels.dart';
Expand Down

0 comments on commit 38c41e8

Please sign in to comment.