From 26c07809915f2de2be2e5acb87b510a381906bcb Mon Sep 17 00:00:00 2001 From: Mattia Date: Fri, 6 Nov 2020 22:46:47 +0100 Subject: [PATCH] Version 1.7.0 - Better captions --- CHANGELOG.md | 11 +++++--- example/video_download.dart | 4 +-- .../youtube_http_client.dart | 2 +- .../closed_caption_client.dart | 1 + .../closed_caption_format.dart | 25 +++++++++++++++++ .../closed_caption_manifest.dart | 1 + .../closed_caption_track_info.dart | 27 +------------------ .../closed_captions/closed_captions.dart | 1 + pubspec.yaml | 2 +- test/closed_caption_test.dart | 17 +++++++++++- 10 files changed, 56 insertions(+), 35 deletions(-) create mode 100644 lib/src/videos/closed_captions/closed_caption_format.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cd8dbb..14268e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ -## 1.x.x - WIP -- Implement `getSrt` a video closed captions in srt format. -- Only throw custom exceptions from the library. -- `getUploadsFromPage` no longer throws. + +## 1.7.0 +- BREAKING CHANGES: `ClosedCaptionManifest.getByLanguage` now returns a List. +- New Enum-Like class: `ClosedCaptionFormat`, which holds all the available YouTube subtiles format. +- `ClosedCaptionManifest.getByLanguage` now has a parameter named `format`. +- `ClosedCaptionClient.getManifest` now has a parameter named `autoGenerated` +- Fix: #82, #83 ## 1.6.2 - Bug fixes: #80 diff --git a/example/video_download.dart b/example/video_download.dart index c1dab83..3d1277c 100644 --- a/example/video_download.dart +++ b/example/video_download.dart @@ -30,10 +30,10 @@ Future download(String id) async { // Get the video manifest. var manifest = await yt.videos.streamsClient.getManifest(id); - var streams = manifest.audioOnly; + var streams = manifest.videoOnly; // Get the audio track with the highest bitrate. - var audio = streams.withHighestBitrate(); + var audio = streams.first; var audioStream = yt.videos.streamsClient.get(audio); // Compose the file name removing the unallowed characters in windows. diff --git a/lib/src/reverse_engineering/youtube_http_client.dart b/lib/src/reverse_engineering/youtube_http_client.dart index 997a7da..a90eebe 100644 --- a/lib/src/reverse_engineering/youtube_http_client.dart +++ b/lib/src/reverse_engineering/youtube_http_client.dart @@ -92,7 +92,7 @@ class YoutubeHttpClient extends http.BaseClient { int errorCount = 0}) async* { var url = streamInfo.url; - var query = Map.from(url.queryParameters); + var query = Map.from(url.queryParameters); query['ratebypass'] = 'yes'; url = url.replace(queryParameters: query); diff --git a/lib/src/videos/closed_captions/closed_caption_client.dart b/lib/src/videos/closed_captions/closed_caption_client.dart index 46cbcf0..7112771 100644 --- a/lib/src/videos/closed_captions/closed_caption_client.dart +++ b/lib/src/videos/closed_captions/closed_caption_client.dart @@ -6,6 +6,7 @@ import '../../reverse_engineering/responses/responses.dart' import '../../reverse_engineering/youtube_http_client.dart'; import '../videos.dart'; import 'closed_caption.dart'; +import 'closed_caption_format.dart'; import 'closed_caption_manifest.dart'; import 'closed_caption_part.dart'; import 'closed_caption_track.dart'; diff --git a/lib/src/videos/closed_captions/closed_caption_format.dart b/lib/src/videos/closed_captions/closed_caption_format.dart new file mode 100644 index 0000000..9734752 --- /dev/null +++ b/lib/src/videos/closed_captions/closed_caption_format.dart @@ -0,0 +1,25 @@ +/// SubTiles format. +class ClosedCaptionFormat { + /// .srv format(1). + static const ClosedCaptionFormat srv1 = ClosedCaptionFormat._('srv1'); + + /// .srv format(2). + static const ClosedCaptionFormat srv2 = ClosedCaptionFormat._('srv2'); + + /// .srv format(3). + static const ClosedCaptionFormat srv3 = ClosedCaptionFormat._('srv3'); + + /// .ttml format. + static const ClosedCaptionFormat ttml = ClosedCaptionFormat._('ttml'); + + /// .vtt format. + static const ClosedCaptionFormat vtt = ClosedCaptionFormat._('vtt'); + + /// List of all sub titles format. + static const List values = [srv1, srv2, srv3, ttml, vtt]; + + /// Format code as string. + final String formatCode; + + const ClosedCaptionFormat._(this.formatCode); +} \ No newline at end of file diff --git a/lib/src/videos/closed_captions/closed_caption_manifest.dart b/lib/src/videos/closed_captions/closed_caption_manifest.dart index 8d65085..08a0659 100644 --- a/lib/src/videos/closed_captions/closed_caption_manifest.dart +++ b/lib/src/videos/closed_captions/closed_caption_manifest.dart @@ -1,5 +1,6 @@ import 'dart:collection'; +import 'closed_caption_format.dart'; import 'closed_caption_track_info.dart'; /// Manifest that contains information about available closed caption tracks diff --git a/lib/src/videos/closed_captions/closed_caption_track_info.dart b/lib/src/videos/closed_captions/closed_caption_track_info.dart index 3b0f138..b09a942 100644 --- a/lib/src/videos/closed_captions/closed_caption_track_info.dart +++ b/lib/src/videos/closed_captions/closed_caption_track_info.dart @@ -1,6 +1,7 @@ import 'package:equatable/equatable.dart'; import '../../extensions/helpers_extension.dart'; +import 'closed_caption_format.dart'; import 'language.dart'; /// Metadata associated with a certain [ClosedCaptionTrack] @@ -39,29 +40,3 @@ class ClosedCaptionTrackInfo extends Equatable { @override List get props => [url, language, isAutoGenerated]; } - -/// SubTiles format. -class ClosedCaptionFormat { - /// .srv format(1). - static const ClosedCaptionFormat srv1 = ClosedCaptionFormat._('srv1'); - - /// .srv format(2). - static const ClosedCaptionFormat srv2 = ClosedCaptionFormat._('srv2'); - - /// .srv format(3). - static const ClosedCaptionFormat srv3 = ClosedCaptionFormat._('srv3'); - - /// .ttml format. - static const ClosedCaptionFormat ttml = ClosedCaptionFormat._('ttml'); - - /// .vtt format. - static const ClosedCaptionFormat vtt = ClosedCaptionFormat._('vtt'); - - /// List of all sub titles format. - static const List values = [srv1, srv2, srv3, ttml, vtt]; - - /// Format code as string. - final String formatCode; - - const ClosedCaptionFormat._(this.formatCode); -} diff --git a/lib/src/videos/closed_captions/closed_captions.dart b/lib/src/videos/closed_captions/closed_captions.dart index 28239af..1f6ad1e 100644 --- a/lib/src/videos/closed_captions/closed_captions.dart +++ b/lib/src/videos/closed_captions/closed_captions.dart @@ -1,5 +1,6 @@ export 'closed_caption.dart'; export 'closed_caption_client.dart'; +export 'closed_caption_format.dart'; export 'closed_caption_manifest.dart'; export 'closed_caption_part.dart'; export 'closed_caption_track.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 675f197..6081d51 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: youtube_explode_dart description: A port in dart of the youtube explode library. Supports several API functions without the need of Youtube API Key. -version: 1.6.2 +version: 1.7.0 homepage: https://github.com/Hexer10/youtube_explode_dart environment: diff --git a/test/closed_caption_test.dart b/test/closed_caption_test.dart index b4e0fea..99e8ed5 100644 --- a/test/closed_caption_test.dart +++ b/test/closed_caption_test.dart @@ -1,4 +1,5 @@ import 'package:test/test.dart'; +import 'package:youtube_explode_dart/src/videos/closed_captions/closed_caption_format.dart'; import 'package:youtube_explode_dart/youtube_explode_dart.dart'; void main() { @@ -22,9 +23,23 @@ void main() { expect(track.captions, isNotEmpty); }); + test('Get closed caption track at a specific time', () async { + var manifest = await yt.videos.closedCaptions + .getManifest('WOxr2dmLHLo', autoGenerated: false); + var trackInfo = manifest.getByLanguage('en'); + var track = await yt.videos.closedCaptions.get(trackInfo.first); + var caption = + track.getByTime(const Duration(hours: 0, minutes: 1, seconds: 48)); + + expect(caption, isNotNull); + expect(caption.parts, isEmpty); + expect(caption.text, + 'The second way to add subtitles is the one\nwe always use.'); + }); + 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', autoGenerated: true); + .getManifest('ppJy5uGZLi4', autoGenerated: true); var trackInfo = manifest.getByLanguage('en'); var track = await yt.videos.closedCaptions.get(trackInfo.first); var caption =