@@ -479,7 +479,7 @@ class PlayerYoutubeSource extends PlayerMediaSource {
479479 /// Get cached item.
480480 List <String >? getSubtitleItems (String videoId) {
481481 return getPreference <List <String >?>(
482- key: 'subtitles_cache /$videoId ' ,
482+ key: 'subs_cache /$videoId ' ,
483483 defaultValue: null ,
484484 );
485485 }
@@ -488,15 +488,15 @@ class PlayerYoutubeSource extends PlayerMediaSource {
488488 void setSubtitleItems (
489489 {required String videoId, required List <String > subtitles}) async {
490490 await setPreference <List <String >?>(
491- key: 'subtitles_cache /$videoId ' ,
491+ key: 'subs_cache /$videoId ' ,
492492 value: subtitles,
493493 );
494494 }
495495
496496 /// Get cached item.
497497 List <String >? getSubtitleMetadata (String videoId) {
498498 return getPreference <List <String >?>(
499- key: 'subtitle_metadata_cache /$videoId ' ,
499+ key: 'subs_meta_cache /$videoId ' ,
500500 defaultValue: null ,
501501 );
502502 }
@@ -505,7 +505,7 @@ class PlayerYoutubeSource extends PlayerMediaSource {
505505 void setSubtitleMetadata (
506506 {required String videoId, required List <String > metadata}) async {
507507 await setPreference <List <String >?>(
508- key: 'subtitle_metadata_cache /$videoId ' ,
508+ key: 'subs_meta_cache /$videoId ' ,
509509 value: metadata,
510510 );
511511 }
@@ -559,20 +559,21 @@ class PlayerYoutubeSource extends PlayerMediaSource {
559559 Future <String > getAudioUrl (MediaItem item, String dataSource) async {
560560 StreamManifest manifest = getStreamManifest (item);
561561
562- if (manifest.muxed
563- .where ((e) => e.videoCodec.contains ('avc1' ))
564- .map ((e) => e.url.toString ())
565- .contains (dataSource)) {
562+ if (manifest.muxed.map ((e) => e.url.toString ()).contains (dataSource)) {
566563 return dataSource;
567564 } else {
568565 AudioStreamInfo streamAudioInfo =
569- manifest.audioOnly.sortByBitrate ().lastWhere ((info) {
570- return info.audioCodec.contains ('mp4a' );
571- });
566+ manifest.audioOnly.sortByBitrate ().first;
572567 return streamAudioInfo.url.toString ();
573568 }
574569 }
575570
571+ /// Used to get the audio source for a video.
572+ Future <String > getAudioExportUrl (MediaItem item, String dataSource) async {
573+ StreamManifest manifest = getStreamManifest (item);
574+ return manifest.audio.first.url.toString ();
575+ }
576+
576577 /// Gets the video qualities available for a [StreamManifest] .
577578 List <VideoQuality > getVideoQualities (
578579 StreamManifest manifest,
@@ -997,33 +998,21 @@ Future<VideoManifest> computeManifests(ComputeManifestParams params) async {
997998 if (! params.subtitlesCached) {
998999 entries.addAll (
9991000 await Future .wait (
1000- tracksByLanguage.values.map (
1001+ tracksByLanguage.values.whereNot ((e) => e.isAutoGenerated). map (
10011002 (trackInfo) async {
10021003 String shortCode = trackInfo.language.code.substring (0 , 2 );
1003- String subtitles =
1004- await yt.videos.closedCaptions.getSubTitles (trackInfo);
1005-
1006- if (trackInfo.isAutoGenerated) {
1007- subtitles = subtitles.replaceAllMapped (vttRegex, (match) {
1008- String text = match.group (11 ) ?? '' ;
1009- List <String > lines = text.split ('\n ' );
1010-
1011- String currentSubtitle =
1012- subtitles.substring (match.start, match.end);
1013- int position = currentSubtitle
1014- .lastIndexOf (text)
1015- .clamp (0 , currentSubtitle.length);
1016-
1017- String newSubtitle = currentSubtitle.replaceFirst (
1018- text, lines.last.trim (), position);
1019-
1020- return newSubtitle;
1021- });
1004+ String xml = await YouTubeTranscriptFetcher ().fetchCaptions (
1005+ params.videoId,
1006+ languageCode: trackInfo.language.code);
1007+ final captions = CaptionParser .parseXml (xml);
1008+ final sb = StringBuffer ('WEBVTT\n ' );
1009+
1010+ for (final caption in captions) {
1011+ sb.write (caption.vtt);
10221012 }
10231013
1024- String metadata = trackInfo.isAutoGenerated
1025- ? 'YouTube - Auto - [$shortCode ]'
1026- : 'YouTube - CC - [$shortCode ]' ;
1014+ final metadata = 'YouTube - CC - [$shortCode ]' ;
1015+ final subtitles = sb.toString ().trim ();
10271016
10281017 return MapEntry (metadata, subtitles);
10291018 },
0 commit comments