youtube_explode/test/closed_caption_test.dart

66 lines
2.4 KiB
Dart

import 'package:test/test.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
void main() {
YoutubeExplode? yt;
setUpAll(() {
yt = YoutubeExplode();
});
tearDownAll(() {
yt?.close();
});
test('Get closed captions of a video', () async {
var manifest = await yt!.videos.closedCaptions.getManifest('WOxr2dmLHLo');
expect(manifest.tracks, isNotEmpty);
});
test('Get closed caption track of a video', () async {
var manifest = await yt!.videos.closedCaptions.getManifest('WOxr2dmLHLo');
var trackInfo = manifest.tracks.first;
var track = await yt!.videos.closedCaptions.get(trackInfo);
expect(track.captions, isNotEmpty);
});
test('Get closed auto translated caption track file of a video', () async {
var manifest = await yt!.videos.closedCaptions.getManifest('WOxr2dmLHLo');
var trackInfo = manifest.tracks.first;
var subtitles = await yt!.videos.closedCaptions
.getSubTitles(trackInfo.autoTranslate('it'));
expect(subtitles, isNotEmpty);
});
test('Get closed caption track at a specific time', () async {
var manifest = await yt!.videos.closedCaptions.getManifest('qfJthDvcZ08');
var trackInfo = manifest.getByLanguage('en',
autoGenerated: false); // ignore: avoid_redundant_argument_values
var track = await yt!.videos.closedCaptions.get(trackInfo.first);
var caption = track.getByTime(const Duration(
hours: 0, // ignore: avoid_redundant_argument_values
minutes: 1,
seconds: 48)); // ignore: avoid_redundant_argument_values
expect(caption, isNotNull);
expect(caption?.parts, isEmpty);
expect(caption?.text, 'But what if you don\'t have a captions file');
});
test('Get auto-generated closed caption track at a specific time', () async {
var manifest = await yt!.videos.closedCaptions.getManifest('ppJy5uGZLi4');
var trackInfo = manifest.getByLanguage('en', autoGenerated: true);
var track = await yt!.videos.closedCaptions.get(trackInfo.first);
var caption = track.getByTime(const Duration(
hours: 0, // ignore: avoid_redundant_argument_values
minutes: 13,
seconds: 22)); // ignore: avoid_redundant_argument_values
var captionPart = caption!.getPartByTime(const Duration(milliseconds: 200));
expect(caption, isNotNull);
expect(captionPart, isNotNull);
expect(caption.text, 'how about this black there are some');
expect(captionPart?.text, ' about');
});
}