| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::Youtube - Fetch actual raw streamable URLs from YouTube and others. | ||
| 4 | |||
| 5 | =head1 AUTHOR | ||
| 6 | |||
| 7 | This module is Copyright (C) 2017-2026 by | ||
| 8 | |||
| 9 | Jim Turner, C<< <turnerjw784 at yahoo.com> >> | ||
| 10 | |||
| 11 | Email: turnerjw784@yahoo.com | ||
| 12 | |||
| 13 | All rights reserved. | ||
| 14 | |||
| 15 | You may distribute this module under the terms of either the GNU General | ||
| 16 | Public License or the Artistic License, as specified in the Perl README | ||
| 17 | file. | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | #!/usr/bin/perl | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | |||
| 25 | use StreamFinder::Youtube; | ||
| 26 | |||
| 27 | die "..usage: $0 ID|URL\n" unless ($ARGV[0]); | ||
| 28 | |||
| 29 | my $video = new StreamFinder::Youtube($ARGV[0]); | ||
| 30 | |||
| 31 | die "Invalid URL or no streams found!\n" unless ($video); | ||
| 32 | |||
| 33 | my $firstStream = $video->get(); | ||
| 34 | |||
| 35 | print "First Stream URL=$firstStream\n"; | ||
| 36 | |||
| 37 | my $url = $video->getURL(); | ||
| 38 | |||
| 39 | print "Stream URL=$url\n"; | ||
| 40 | |||
| 41 | my $videoTitle = $video->getTitle(); | ||
| 42 | |||
| 43 | print "Title=$videoTitle\n"; | ||
| 44 | |||
| 45 | my $videoDescription = $video->getTitle('desc'); | ||
| 46 | |||
| 47 | print "Description=$videoDescription\n"; | ||
| 48 | |||
| 49 | my $videoID = $video->getID(); | ||
| 50 | |||
| 51 | print "Video ID=$videoID\n"; | ||
| 52 | |||
| 53 | my $artist = $video->{'artist'}; | ||
| 54 | |||
| 55 | print "Artist (channel)=$artist\n" if ($artist); | ||
| 56 | |||
| 57 | my $albumartist = $video->{'albumartist'}; | ||
| 58 | |||
| 59 | print "Album Artist (Channel URL)=$albumartist\n" if ($albumartist); | ||
| 60 | |||
| 61 | my $icon_url = $video->getIconURL(); | ||
| 62 | |||
| 63 | if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE: | ||
| 64 | |||
| 65 | my ($image_ext, $icon_image) = $video->getIconData(); | ||
| 66 | |||
| 67 | if ($icon_image && open IMGOUT, ">/tmp/${videoID}.$image_ext") { | ||
| 68 | |||
| 69 | binmode IMGOUT; | ||
| 70 | |||
| 71 | print IMGOUT $icon_image; | ||
| 72 | |||
| 73 | close IMGOUT; | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | my $stream_count = $video->count(); | ||
| 80 | |||
| 81 | print "--Stream count=$stream_count=\n"; | ||
| 82 | |||
| 83 | my @streams = $video->get(); | ||
| 84 | |||
| 85 | foreach my $s (@streams) { | ||
| 86 | |||
| 87 | print "------ stream URL=$s=\n"; | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | =head1 DESCRIPTION | ||
| 92 | |||
| 93 | StreamFinder::Youtube accepts a valid full YouTube video ID, or page URL on | ||
| 94 | youtube, et. al. that the "yt-dlp" program supports, | ||
| 95 | and returns the actual stream URL, title, and cover art icon for that video. | ||
| 96 | The purpose is that one needs this URL in order to have the option to | ||
| 97 | stream the video in one's own choice of media player software rather | ||
| 98 | than using their web browser and accepting any / all flash, ads, | ||
| 99 | javascript, cookies, trackers, web-bugs, and other crapware that can | ||
| 100 | come with that method of play. The author uses his own custom all-purpose | ||
| 101 | media player called "fauxdacious" (his custom hacked version of the | ||
| 102 | open-source "audacious" audio player). "fauxdacious" incorporates this | ||
| 103 | module to decode and play youtube.com videos. This is a submodule of the | ||
| 104 | general StreamFinder module. | ||
| 105 | |||
| 106 | NOTE: This module may return either Youtube or non-Youtube videos and streams | ||
| 107 | for non-Youtube sites, including videos embedded in IFRAME tags and even | ||
| 108 | Rumble.com videos found in some non-Youtube sites (L<StreamFinder::Rumble> | ||
| 109 | required). See the I<-noiframes> and I<-youtubeonly> flags below for limiting | ||
| 110 | this feature. Also note: these videos, etc. are handled here and not | ||
| 111 | by L<StreamFinder::Anystream>. | ||
| 112 | |||
| 113 | NEW: StreamFinder::Youtube now supports return "DASH" video streams as pairs | ||
| 114 | of streams (an audio-only stream paired with a video-only stream) for media- | ||
| 115 | players such as Fauxdacious Mediaplayer that are capable of combining them | ||
| 116 | live while streaming in order to take advantage of Youtube's many modern | ||
| 117 | audio/video format options! See the new "-formatv" option below. | ||
| 118 | |||
| 119 | NOTE: Streamfinder now strongly recommends using yt-dlp over youtube-dl for | ||
| 120 | extracting streams, and it is now the default app. used. | ||
| 121 | |||
| 122 | Depends: | ||
| 123 | |||
| 124 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>, | ||
| 125 | and the separate application program: yt-dlp. | ||
| 126 | |||
| 127 | =head1 SUBROUTINES/METHODS | ||
| 128 | |||
| 129 | =over 4 | ||
| 130 | |||
| 131 | =item B<new>(I<ID>|I<url> [, I<-debug> [ => 0|1|2 ]] | ||
| 132 | [, I<-secure> [ => 0|1 ]] | ||
| 133 | [, I<-fast> [ => 0|1 ]] | ||
| 134 | [, I<-format> => "yt-dlp format specification" ] | ||
| 135 | [, I<-formatv> => "yt-dlp DASH video-only format specification" ] | ||
| 136 | [, I<-format-fallback> => "yt-dlp format specification (2nd try)" ] | ||
| 137 | [, I<-formatonly> [ => 0|1 ]] [, I<-noiframes> [ => 0|1 ]] | ||
| 138 | [, I<-youtubeonly> [ => 0|1 ]] | ||
| 139 | [, I<-user-agent> => "user-agent string"] | ||
| 140 | [, I<-userid> => "youtube-user-id", I<-userpw> => "password"] | ||
| 141 | [, I<-youtube-dl> => "yt-dlp"] | ||
| 142 | [, I<-youtube-dl-args> => "yt-dlp arguments"] | ||
| 143 | [, I<-youtube-dl-add-args> => "yt-dlp additional arguments"]) | ||
| 144 | |||
| 145 | Accepts a youtube.com video ID, or any full URL that yt-dlp | ||
| 146 | supports and creates and returns a new video object, or I<undef> if the URL | ||
| 147 | is not a youtube-supported video URL or no streams are found. The URL can | ||
| 148 | be the full URL, | ||
| 149 | ie. https://www.youtube.com/watch?v=B<video-id>, a user or channel URL, | ||
| 150 | ie. https://www.youtube.com/channel/B<channel-id> or | ||
| 151 | https://www.youtube.com/user/B<user-id>. or just B<video-id> | ||
| 152 | (if the site is www.youtube.com, since YouTube has multiple sites). | ||
| 153 | If a I<channel-id>, I<user-id>, or a Youtube channel/user URL is | ||
| 154 | given, then the first (latest) video uploaded to that channel will be | ||
| 155 | returned. Note: Some users and channels have a "featured" video (shown | ||
| 156 | with a larger thumbnail at the top) or multiple groupings of videos, but | ||
| 157 | the "first" video returned will normally be from the "Uploads" group. | ||
| 158 | Channels and users' urls must currently be specified as full URLs, as | ||
| 159 | just specifying an ID will be interpreted as a specific B<video-id>! | ||
| 160 | |||
| 161 | If I<-format> is specified, it should be a valid "I<yt-dlp -f>" format | ||
| 162 | string (see the yt-dlp manpage for details). Examples: | ||
| 163 | "I<mp4[height<=720]/best[height<=720]>" which limits videos to 720p, or | ||
| 164 | "bestaudio" to download only audio streams. | ||
| 165 | Default is "B<best>", but if no streams are found, it then tries all, | ||
| 166 | unless I<-formatonly> is specified. | ||
| 167 | |||
| 168 | If I<-formatv> is also specified, it should be a valid "I<yt-dlp -f>" | ||
| 169 | "DASH video-only format string (see the yt-dlp manpage for details) and | ||
| 170 | should be combined with an audio-only format string value for the | ||
| 171 | "I<-format>" option, ie: B<-format => "bestaudio", | ||
| 172 | -formatv => "bv[height<=720][vbr<=3500]">. There is no default value. | ||
| 173 | |||
| 174 | DASH streams will be returned as a pairs of streams separated by "|" in | ||
| 175 | the order: "[audio-stream]|[video-stream]", which one's media-player will | ||
| 176 | need to be able to parse. Fauxdacious Mediaplayer handles these and | ||
| 177 | combines them inline to produce higher-quality video playback! | ||
| 178 | |||
| 179 | If I<-format-fallback> is specified, it should be a valid | ||
| 180 | "I<yt-dlp -f>" format string (see the yt-dlp manpage for details), and | ||
| 181 | will be used if no streams matching the I<-format> are found. | ||
| 182 | Default is "B<bestaudio>". | ||
| 183 | |||
| 184 | If I<-formatonly> is specified (set to 1 (true)), then if no streams match | ||
| 185 | the specified I<-format> argument (default "I<best>"), then if | ||
| 186 | I<-format-fallback> is specified, that will be tried, otherwise, no streams | ||
| 187 | will be returned. Otherwise (if I<-formatonly> is unspecified or false, | ||
| 188 | yt-dlp is called again with either the -F I<-format-fallback> | ||
| 189 | (if specified) or else, no format (I<-f>) argument (match any stream we | ||
| 190 | can find). Default is 0 (false / unset). | ||
| 191 | |||
| 192 | If I<-formats_by_url> is specified, it should be a valid hash-ref. of url | ||
| 193 | patterns to match (keys) and valid "I<yt-dlp -f>" format strings. | ||
| 194 | This allows for overriding the I<-format> option for URLs (ie. certain | ||
| 195 | non-Youtube ones that provide different formats), particularly useful | ||
| 196 | when I<-formatonly> is also specified. | ||
| 197 | |||
| 198 | If I<-fast> is specified (set to 1 (true)), a separate probe of the | ||
| 199 | page to fetch the video's title and artist is skipped. This is useful | ||
| 200 | if you know the video is NOT a YouTube video or you don't care about | ||
| 201 | the artist (youtube channel's owner), artist icon, fields, etc. | ||
| 202 | Default is 0 (false / unset). | ||
| 203 | |||
| 204 | If I<-noiframes> is specified (set to 1 (true)), then only | ||
| 205 | process actual video URLs, not search the page for an iframe containing | ||
| 206 | a video URL (a new feature with v0.47). This is used primarily internally | ||
| 207 | to prevent possible recursion when StreamFinder::YouTube finds an iframe | ||
| 208 | containing a potential video stream URL and creates a new StreamFinder object | ||
| 209 | to find any streams in that URL (which can then call StreamFinder::Youtube | ||
| 210 | again on that URL to find the stream). Default is 0 (false / unset) - search | ||
| 211 | for StreamFinder-searchable URLs in an iframe, if the page is HTML and not an | ||
| 212 | actual video URL. | ||
| 213 | |||
| 214 | I<-youtubeonly> - Some non-Youtube pages have embedded Rumble (Rumble.com) | ||
| 215 | videos embedded in them and since StreamFinder::Youtube is somewhat of a | ||
| 216 | "catchall" (for videos), and we (the Author) prefer the less-woke Rumble to | ||
| 217 | Youtube (which has major censorship issues), we search for embedded Rumble | ||
| 218 | videos here, as opposed to L<StreamFinder::Rumble> or | ||
| 219 | L<StreamFinder::AnyStream>, and, upon finding one, we return that rather than | ||
| 220 | continuing the search for Youtube videos. To NOT do this (consider only | ||
| 221 | embedded Youtube videos), specify this / set it to 1 (true). | ||
| 222 | Default 0 (false) - accept Rumble (or other non-Youtube) videos, if found | ||
| 223 | first. NOTE: This option is effectively set (true) if I<-noiframes> is set! | ||
| 224 | |||
| 225 | The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>). | ||
| 226 | If 1 then only secure ("https://") streams will be returned. Default for | ||
| 227 | I<-secure> is 0 (false) - return all streams (http and https). | ||
| 228 | |||
| 229 | The optional I<-user-agent> argument can specify a specific user-agent string | ||
| 230 | to send to yt-dlp's optional "I<--user-agent>" argument. NOTE: This is | ||
| 231 | completely separate from the I<-agent> option used by some other StreamFinder | ||
| 232 | modules for fetching pages and streams from their respective sites, as that | ||
| 233 | argument is used by LWP::UserAgent (along with some other options), and is NOT | ||
| 234 | passed to yt-dlp, though they represent the same kind of user-agent string! | ||
| 235 | Default is I<-none-> (yt-dlp or the alternate program may use it's | ||
| 236 | own default). | ||
| 237 | |||
| 238 | The optional I<-userid> and I<-userpw> arguments allow specifying a Youtube | ||
| 239 | login (for fetching videos, ie. paid ones that require one). | ||
| 240 | Defaults are I<-none-> (no userid or password specified). | ||
| 241 | |||
| 242 | The optional I<-youtube-dl> argument allows specifying an alternate stream- | ||
| 243 | parser program in lieu of "yt-dlp", such as "youtube-dl" | ||
| 244 | (Default: "I<yt-dlp>"). Note: StreamFinder recommends sticking with yt-dlp, | ||
| 245 | as it has been known to work when youtube-dl fails, and is known for quicker | ||
| 246 | fixing of issues, as Youtube continues to throw up additional roadblocks to | ||
| 247 | alternate viewing methods. If the program is not in the user's | ||
| 248 | executable I<PATH>, the full path can be included with the program name here. | ||
| 249 | |||
| 250 | The optional I<-youtube-dl-args> argument allows you to change the arguments | ||
| 251 | to be passed to the external yt-dlp (youtube-dl or , etc.) program. NOTE: | ||
| 252 | Unless this program changes it's valid arguments or you select an alternate | ||
| 253 | program that requires slightly different arguments, you should NOT use this | ||
| 254 | argument, as the DEFAULT is: | ||
| 255 | "I<--get-url --get-format --get-thumbnail --get-title --get-description --get-id>", | ||
| 256 | which are the I<currently> required arguments for this module to function | ||
| 257 | properly! Instead, if you wish to include additional arguments, you should | ||
| 258 | use the I<-youtube-dl-add-args> option to append them to this required list, | ||
| 259 | see below: Also note that the I<-f format> argument should NOT be specified | ||
| 260 | either here or below as the I<-format> option provides this argument! | ||
| 261 | |||
| 262 | The optional I<-youtube-dl-add-args> argument allows you to add additional | ||
| 263 | arguments to be passed to the external yt-dlp (youtube-dl or , etc.) program. | ||
| 264 | See both the I<-youtube-dl-args> argument description and the manpage for | ||
| 265 | yt-dlp, youtube-dl or whatever alternative external program you use to extract | ||
| 266 | video streams for valid arguments for possible inclusion here. | ||
| 267 | |||
| 268 | DEFAULT I<-none-> (no additional arguments). | ||
| 269 | |||
| 270 | The optional I<-youtube-site> argument allows specifying a different default | ||
| 271 | Youtube site if only an video-ID is provided or an embedded video in an | ||
| 272 | iframe doesn't specify a specific Youtube site. | ||
| 273 | |||
| 274 | DEFAULT "I<https://www.youtube.com>". | ||
| 275 | |||
| 276 | Additional (general StreamFinder) options: | ||
| 277 | |||
| 278 | I<-log> => "I<logfile>" | ||
| 279 | |||
| 280 | Specify path to a log file. If a valid and writable file is specified, A | ||
| 281 | line will be appended to this file every time one or more streams is | ||
| 282 | successfully fetched for a url. | ||
| 283 | |||
| 284 | DEFAULT I<-none-> (no logging). | ||
| 285 | |||
| 286 | I<-logfmt> specifies a format string for lines written to the log file. | ||
| 287 | |||
| 288 | DEFAULT "I<[time] [url] - [site]: [title] ([total])>". | ||
| 289 | |||
| 290 | The valid field I<[variables]> are: [stream]: The url of the first/best | ||
| 291 | stream found. [site]: The site name (Youtube - OR the site name of the | ||
| 292 | embedded URL in the first iframe, if found - see I<-noiframes> option above | ||
| 293 | to prevent this feature). [url]: The url searched for streams. [time]: Perl | ||
| 294 | timestamp when the line was logged. [title], [artist], [album], | ||
| 295 | [description], [year], [genre], [total], [albumartist]: The corresponding | ||
| 296 | field data returned (or "I<-na->", if no value). | ||
| 297 | |||
| 298 | =item $video->B<get>() | ||
| 299 | |||
| 300 | Returns an array of strings representing all stream URLs found. | ||
| 301 | |||
| 302 | =item $video->B<getURL>([I<options>]) | ||
| 303 | |||
| 304 | Similar to B<get>() except it only returns a single stream representing | ||
| 305 | the first valid stream found. | ||
| 306 | |||
| 307 | Current options are: I<"random">, I<"nopls">, and I<"noplaylists">. | ||
| 308 | By default, the first ("best"?) stream is returned. If I<"random"> is | ||
| 309 | specified, then a random one is selected from the list of streams found. | ||
| 310 | If I<"nopls"> is specified, and the stream to be returned is a ".pls" playlist, | ||
| 311 | it is first fetched and the first entry (or a random entry if I<"random"> is | ||
| 312 | specified) is returned. This is needed by Fauxdacious Mediaplayer. | ||
| 313 | If I<"noplaylists"> is specified, and the stream to be returned is a | ||
| 314 | "playlist" (either .pls or .m3u? extension), it is first fetched and the first | ||
| 315 | entry (or a random entry if I<"random"> is specified) in the playlist | ||
| 316 | is returned. | ||
| 317 | |||
| 318 | =item $video->B<count>() | ||
| 319 | |||
| 320 | Returns the number of streams found for the video. | ||
| 321 | |||
| 322 | =item $video->B<getID>() | ||
| 323 | |||
| 324 | Returns the video's YouTube ID (alphanumeric+some special characters). | ||
| 325 | |||
| 326 | =item $video->B<getTitle>(['desc']) | ||
| 327 | |||
| 328 | Returns the station's title, or (long description). | ||
| 329 | |||
| 330 | =item $video->B<getIconURL>(['artist']) | ||
| 331 | |||
| 332 | Returns the URL for the video's "cover art" icon image, if any. | ||
| 333 | If B<'artist'> is specified, the channel artist's icon url is returned, | ||
| 334 | if any. NOTE: The B<'artist'> option will return an empty string if | ||
| 335 | the B<-fast> option is used. | ||
| 336 | |||
| 337 | =item $video->B<getIconData>(['artist']) | ||
| 338 | |||
| 339 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 340 | "gif", "jpeg", etc.) and the actual icon image (binary data), if any. | ||
| 341 | If B<'artist'> is specified, the channel artist's icon data is returned, | ||
| 342 | if any. NOTE: The B<'artist'> option will return an empty string if | ||
| 343 | the B<-fast> option is used. | ||
| 344 | |||
| 345 | =item $video->B<getImageURL>() | ||
| 346 | |||
| 347 | Returns the URL for the video's "cover art" banner image, which for | ||
| 348 | YouTube videos is always the icon image, as YouTube does not support | ||
| 349 | a separate banner image at this time. | ||
| 350 | |||
| 351 | =item $video->B<getImageData>() | ||
| 352 | |||
| 353 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 354 | "gif", "jpeg", etc.) and the actual video's banner image (binary data). | ||
| 355 | |||
| 356 | =item $video->B<getType>() | ||
| 357 | |||
| 358 | Returns the video's type ("Youtube"). | ||
| 359 | |||
| 360 | =back | ||
| 361 | |||
| 362 | =head1 CONFIGURATION FILES | ||
| 363 | |||
| 364 | The default root location directory for StreamFinder configuration files | ||
| 365 | is "~/.config/StreamFinder". To use an alternate location directory, | ||
| 366 | specify it in the "I<STREAMFINDER>" environment variable, ie.: | ||
| 367 | B<$ENV{STREAMFINDER} = "/etc/StreamFinder">. | ||
| 368 | |||
| 369 | =over 4 | ||
| 370 | |||
| 371 | =item ~/.config/StreamFinder/Youtube/config | ||
| 372 | |||
| 373 | Optional text file for specifying various configuration options | ||
| 374 | for a specific site (submodule). Each option is specified on a | ||
| 375 | separate line in the format below: | ||
| 376 | NOTE: Do not follow the lines with a semicolon, comma, or any other | ||
| 377 | separator. Non-numeric I<values> should be surrounded with quotes, either | ||
| 378 | single or double. Blank lines and lines beginning with a "#" sign as | ||
| 379 | their first non-blank character are ignored as comments. | ||
| 380 | |||
| 381 | 'option' => 'value' [,] | ||
| 382 | |||
| 383 | and the options are loaded into a hash used only by the specific | ||
| 384 | (submodule) specified. Valid options include | ||
| 385 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 386 | |||
| 387 | Options specified here override any specified in | ||
| 388 | I<~/.config/StreamFinder/config>. | ||
| 389 | |||
| 390 | =item ~/.config/StreamFinder/config | ||
| 391 | |||
| 392 | Optional text file for specifying various configuration options. | ||
| 393 | Each option is specified on a separate line in the format below: | ||
| 394 | |||
| 395 | 'option' => 'value' [,] | ||
| 396 | |||
| 397 | and the options are loaded into a hash used by all sites | ||
| 398 | (submodules) that support them. Valid options include | ||
| 399 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 400 | |||
| 401 | =back | ||
| 402 | |||
| 403 | NOTE: Options specified in the options parameter list of the I<new()> | ||
| 404 | function will override those corresponding options specified in these files. | ||
| 405 | |||
| 406 | =head1 KEYWORDS | ||
| 407 | |||
| 408 | youtube | ||
| 409 | |||
| 410 | =head1 DEPENDENCIES | ||
| 411 | |||
| 412 | yt-dlp (or youtube-dl, or other compatable program) | ||
| 413 | |||
| 414 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>, youtube-dl | ||
| 415 | |||
| 416 | =head1 RECCOMENDS | ||
| 417 | |||
| 418 | yt-dlp, wget | ||
| 419 | |||
| 420 | =head1 BUGS | ||
| 421 | |||
| 422 | Please report any bugs or feature requests to C<bug-streamFinder-youtube at rt.cpan.org>, or through | ||
| 423 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-Youtube>. I will be notified, and then you'll | ||
| 424 | automatically be notified of progress on your bug as I make changes. | ||
| 425 | |||
| 426 | =head1 SUPPORT | ||
| 427 | |||
| 428 | You can find documentation for this module with the perldoc command. | ||
| 429 | |||
| 430 | perldoc StreamFinder::Youtube | ||
| 431 | |||
| 432 | You can also look for information at: | ||
| 433 | |||
| 434 | =over 4 | ||
| 435 | |||
| 436 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 437 | |||
| 438 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-Youtube> | ||
| 439 | |||
| 440 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 441 | |||
| 442 | L<http://annocpan.org/dist/StreamFinder-Youtube> | ||
| 443 | |||
| 444 | =item * Search CPAN | ||
| 445 | |||
| 446 | L<http://search.cpan.org/dist/StreamFinder-Youtube/> | ||
| 447 | |||
| 448 | =back | ||
| 449 | |||
| 450 | =head1 LICENSE AND COPYRIGHT | ||
| 451 | |||
| 452 | Copyright 2017-2026 Jim Turner. | ||
| 453 | |||
| 454 | This program is free software; you can redistribute it and/or modify it | ||
| 455 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 456 | copy of the full license at: | ||
| 457 | |||
| 458 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 459 | |||
| 460 | Any use, modification, and distribution of the Standard or Modified | ||
| 461 | Versions is governed by this Artistic License. By using, modifying or | ||
| 462 | distributing the Package, you accept this license. Do not use, modify, | ||
| 463 | or distribute the Package, if you do not accept this license. | ||
| 464 | |||
| 465 | If your Modified Version has been derived from a Modified Version made | ||
| 466 | by someone other than you, you are nevertheless required to ensure that | ||
| 467 | your Modified Version complies with the requirements of this license. | ||
| 468 | |||
| 469 | This license does not grant you the right to use any trademark, service | ||
| 470 | mark, tradename, or logo of the Copyright Holder. | ||
| 471 | |||
| 472 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 473 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 474 | otherwise transfer the Package with respect to any patent claims | ||
| 475 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 476 | Package. If you institute patent litigation (including a cross-claim or | ||
| 477 | counterclaim) against any party alleging that the Package constitutes | ||
| 478 | direct or contributory patent infringement, then this Artistic License | ||
| 479 | to you shall terminate on the date that such litigation is filed. | ||
| 480 | |||
| 481 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 482 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 483 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 484 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 485 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 486 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 487 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 488 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 489 | |||
| 490 | =cut | ||
| 491 | |||
| 492 | package StreamFinder::Youtube; | ||
| 493 | |||
| 494 | use strict; | ||
| 495 | use warnings; | ||
| 496 | use URI::Escape; | ||
| 497 | use HTML::Entities (); | ||
| 498 | use LWP::UserAgent (); | ||
| 499 | use parent 'StreamFinder::_Class'; | ||
| 500 | |||
| 501 | my $DEBUG = 0; | ||
| 502 | my $DEFAULTYTSITE = 'https://www.youtube.com'; | ||
| 503 | my $DEFAULTFMT = 'best'; | ||
| 504 | my $DEFAULTFALLBACK = 'bestaudio'; | ||
| 505 | |||
| 506 | sub new | ||
| 507 | { | ||
| 508 | my $class = shift; | ||
| 509 | my $url = shift; | ||
| 510 | |||
| 511 | return undef unless ($url); | ||
| 512 | |||
| 513 | my $self = $class->SUPER::new('Youtube', @_); | ||
| 514 | |||
| 515 | #SET DEFAULTS FOR FLAGS: | ||
| 516 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 517 | $self->{'formatonly'} = 0 unless (defined $self->{'formatonly'}); | ||
| 518 | $self->{'noiframes'} = 0 unless (defined $self->{'noiframes'}); | ||
| 519 | $self->{'notrim'} = 0; | ||
| 520 | $self->{'youtubeonly'} = 0 unless (defined $self->{'youtubeonly'}); | ||
| 521 | $self->{'youtube-site'} = $DEFAULTYTSITE; | ||
| 522 | #DEFAULT YOUTUBE-DL ARGUMENTS: | ||
| 523 | $self->{'youtube-dl-args'} = '--get-url --get-format --get-thumbnail --get-title --get-description --get-id' | ||
| 524 | unless (defined $self->{'youtube-dl-args'}); | ||
| 525 | |||
| 526 | #FETCH ANY PARAMETERS PASSED TO THE new() FUNCTION (OVERRIDE ANY SET IN _class (GENERAL OR CONFIG FILES)): | ||
| 527 | while (@_) { | ||
| 528 | if ($_[0] =~ /^\-?fast$/o) { | ||
| 529 | shift; | ||
| 530 | $self->{'fast'} = (defined($_[0]) && $_[0] =~/^[0-9]$/) ? shift : 1; | ||
| 531 | } elsif ($_[0] =~ /^\-?noiframes$/o) { | ||
| 532 | shift; | ||
| 533 | $self->{'noiframes'} = (defined $_[0]) ? shift : 1; | ||
| 534 | } elsif ($_[0] =~ /^\-?youtubeonly$/o) { | ||
| 535 | shift; | ||
| 536 | $self->{'youtubeonly'} = (defined $_[0]) ? shift : 1; | ||
| 537 | } elsif ($_[0] =~ /^\-?formatonly$/o) { | ||
| 538 | shift; | ||
| 539 | $self->{'formatonly'} = (defined $_[0]) ? shift : 1; | ||
| 540 | } elsif ($_[0] =~ /^\-?notrim$/o) { | ||
| 541 | shift; | ||
| 542 | $self->{'notrim'} = (defined $_[0]) ? shift : 1; | ||
| 543 | } elsif ($_[0] =~ /^\-?youtube-site$/o) { | ||
| 544 | shift; | ||
| 545 | $self->{'youtube-site'} = (defined $_[0]) ? shift : $DEFAULTYTSITE; | ||
| 546 | $self->{'youtube-site'} = s#\/$##; | ||
| 547 | $self->{'youtube-site'} = 'https://' . $self->{'youtube-site'} | ||
| 548 | unless ($self->{'youtube-site'} =~ m#^https?\:\/\/#); | ||
| 549 | } elsif ($_[0] =~ /^\-?format$/o) { | ||
| 550 | shift; | ||
| 551 | $self->{'format'} = shift if (defined $_[0]); | ||
| 552 | } elsif ($_[0] =~ /^\-?formatv$/o) { | ||
| 553 | shift; | ||
| 554 | $self->{'formatv'} = shift if (defined $_[0]); | ||
| 555 | } elsif ($_[0] =~ /^\-?format\-fallback$/o) { | ||
| 556 | shift; | ||
| 557 | $self->{'format-fallback'} = shift if (defined $_[0]); | ||
| 558 | } elsif ($_[0] =~ /^\-?user-agent$/o) { | ||
| 559 | shift; | ||
| 560 | $self->{'user-agent'} = shift if (defined $_[0]); | ||
| 561 | } elsif ($_[0] =~ /^\-?youtube-dl$/o) { | ||
| 562 | shift; | ||
| 563 | $self->{'youtube-dl'} = shift if (defined $_[0]); | ||
| 564 | } elsif ($_[0] =~ /^\-?youtube-dl-args$/o) { | ||
| 565 | shift; | ||
| 566 | $self->{'youtube-dl-args'} = shift if (defined $_[0]); | ||
| 567 | } elsif ($_[0] =~ /^\-?youtube-dl-add-args$/o) { | ||
| 568 | shift; | ||
| 569 | $self->{'youtube-dl-add-args'} = shift if (defined $_[0]); | ||
| 570 | } else { | ||
| 571 | shift; #DISCARD ANY OTHERS. | ||
| 572 | } | ||
| 573 | } | ||
| 574 | $self->{'youtubeonly'} = 1 if ($self->{'noiframes'}); #NO EMBEDDED RUMBLE-SEARCH IF NO IFRAMES ALLOWED! | ||
| 575 | |||
| 576 | $self->{'youtube-dl'} = 'yt-dlp' unless (defined $self->{'youtube-dl'}); | ||
| 577 | |||
| 578 | print STDERR "-0(Youtube): URL=$url=\n" if ($DEBUG); | ||
| 579 | $url =~ s/[\?\&]autoplay\=true$//; #STRIP THIS OFF SO WE DON'T HAVE TO. | ||
| 580 | $url =~ s/[\?\&]list\=.*$//; #yt-dlp SEEMS TO JUST HANG ON "lists" (TRYING TO FETCH A LIST OF VIDEOS?!). | ||
| 581 | (my $url2fetch = $url); | ||
| 582 | $self->{'_isaYtPage'} = 1; | ||
| 583 | #DEPRECIATED (STATION-IDS NOW INCLUDE STUFF BEFORE THE DASH: ($self->{'id'} = $url) =~ s#^.*\-([a-z]\d+)\/?$#$1#; | ||
| 584 | if ($url2fetch =~ m#^https?\:#) { | ||
| 585 | $self->{'_isaYtPage'} = 0 unless ($url2fetch =~ /\b(?:youtube\.|youtu.be|ytimg\.)\b/); | ||
| 586 | #$url2fetch =~ s/www\.youtube\.com/youtube\.be/; #WWW.YOUTUBE.COM SEEMS TO NOW BE BLOCKING youtube-dl?! :/ | ||
| 587 | $self->{'id'} = $1 if ($url2fetch =~ m#\/([^\/]+)\/?$#); | ||
| 588 | $self->{'id'} =~ s/^watch\?v\=//; | ||
| 589 | $self->{'id'} =~ s/[\?\&].*$//; | ||
| 590 | $self->{'id'} = $1 if (!$self->{'_isaYtPage'} && $url2fetch =~ m#id[\=\:\#]?([^\/\s\=\:\#]+)#); | ||
| 591 | } else { | ||
| 592 | $self->{'id'} = $url; | ||
| 593 | $url2fetch = $self->{'youtube-site'} . '/watch?v=' . $url; | ||
| 594 | } | ||
| 595 | $url2fetch =~ s/[\?\&](?!v\=).*$// unless ($self->{'notrim'}); | ||
| 596 | print STDERR "-1 (isYT=$$self{'_isaYtPage'}) FETCHING URL=$url2fetch= VIA $$self{'youtube-dl'}: ID=$$self{'id'}=\n" if ($DEBUG); | ||
| 597 | $self->{'genre'} = 'Video'; | ||
| 598 | $self->{'albumartist'} = $url2fetch; | ||
| 599 | |||
| 600 | #FIRST, CHECK IF WE'RE A CHANNEL OR USER PAGE, IF SO, FETCH & RETURN LATEST UPLOADED VIDEO (EXCLUDE MARQUEE VIDEO AT TOP): | ||
| 601 | |||
| 602 | if ($self->{'_isaYtPage'} && !$self->{'noiframes'} && ($url2fetch =~ m#\/(?:channel|user|c)\/# | ||
| 603 | || $url2fetch =~ m#$self->{'youtube-site'}\/\@#)) { #WE'RE A CHANNEL PAGE, GRAB 1ST VIDEO!: | ||
| 604 | print STDERR "..1a:We're a channel or user page!...\n" if ($DEBUG); | ||
| 605 | my $html = ''; | ||
| 606 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 607 | $ua->timeout($self->{'timeout'}); | ||
| 608 | $ua->max_size(1024); #LIMIT FETCH-SIZE TO AVOID INFINITELY DOWNLOADING A STREAM! | ||
| 609 | $ua->cookie_jar({}); | ||
| 610 | $ua->env_proxy; | ||
| 611 | my $response = $ua->get($url2fetch); | ||
| 612 | $html = $response->decoded_content if ($response->is_success); | ||
| 613 | if ($html =~ /\<\!DOCTYPE\s+(?:html|text)/i) { #IF WE'RE AN HTML DOC. (NOT A STREAM!), THEN FETCH THE WHOLE THING: | ||
| 614 | $ua->max_size(undef); #(NOW OK TO FETCH THE WHOLE DOCUMENT) | ||
| 615 | my $response = $ua->get($url2fetch); | ||
| 616 | $html = $response->decoded_content if ($response->is_success); | ||
| 617 | return undef unless ($html); | ||
| 618 | |||
| 619 | $html =~ s#^.+\"description\"\:\{\"runs\"\:##s; #USER PAGES CAN HAVE A BANNER VIDEO, *TRY TO* SKIP THIS! | ||
| 620 | if ($html =~ m#\:\{\"url\"\:\"([^\"]+)\"\,\"webPageType\"\:\"WEB\_PAGE\_TYPE\_WATCH\"\,#s) { | ||
| 621 | $url2fetch = $1; | ||
| 622 | $url2fetch =~ s#^\/\/#https\:\/\/#; #URL STARTS WITH "//" (PREMPTIVE) | ||
| 623 | $url2fetch =~ s#^\/#$self->{'youtube-site'}\/#; #URL IS JUST "/video-id[?other-junk]" (COMMON) | ||
| 624 | $self->{'id'} = $1 if ($url2fetch =~ m#\/([^\/]+)\/?$#); | ||
| 625 | $self->{'id'} =~ s/^watch\?v\=//; | ||
| 626 | $self->{'id'} =~ s/[\?\&].*$//; | ||
| 627 | print STDERR "---FOUND 1ST EPISODE! FETCHING=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 628 | goto DO_YTDL; #SKIP NON-YOUTUBE PAGE CHECK (NEXT PARAGRAPH): | ||
| 629 | } | ||
| 630 | } | ||
| 631 | print STDERR "u:DID NOT FIND A VIDEO ON CHANNEL/USER PAGE, PUNT!" if ($DEBUG); | ||
| 632 | return undef; | ||
| 633 | } | ||
| 634 | |||
| 635 | #IF NON-YOUTUBE PAGE, LOOK FOR ANYTHING EMBEDDED IN AN IFRAME: | ||
| 636 | |||
| 637 | unless ($self->{'_isaYtPage'} || $self->{'noiframes'}) { | ||
| 638 | print STDERR "..1a:See if we have a StreamFinder-supported URL in 1st iframe?...\n" if ($DEBUG); | ||
| 639 | my $embedded_video = 0;; | ||
| 640 | my $html = ''; | ||
| 641 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 642 | $ua->timeout($self->{'timeout'}); | ||
| 643 | $ua->max_size(1024); #LIMIT FETCH-SIZE TO AVOID INFINITELY DOWNLOADING A STREAM! | ||
| 644 | $ua->cookie_jar({}); | ||
| 645 | $ua->env_proxy; | ||
| 646 | my $response = $ua->get($url2fetch); | ||
| 647 | $html = $response->decoded_content if ($response->is_success); | ||
| 648 | if ($html =~ /\<\!DOCTYPE\s+(?:html|text)/i) { #IF WE'RE AN HTML DOC. (NOT A STREAM!), THEN FETCH THE WHOLE THING: | ||
| 649 | $ua->max_size(undef); #(NOW OK TO FETCH THE WHOLE DOCUMENT) | ||
| 650 | my $response = $ua->get($url2fetch); | ||
| 651 | $html = $response->decoded_content if ($response->is_success); | ||
| 652 | ##FIRST, LOOK FOR FIRST YOUTUBE VIDEO EMBEDDED IN AN IFRAME: | ||
| 653 | while ($html && $html =~ s#\<iframe([^\>]+)\>##so) { | ||
| 654 | my $one = $1; | ||
| 655 | my $embeddedURL = ($one =~ m#\"(https?\:\/\/[^\"]+)#s) ? $1 : ''; | ||
| 656 | if ($embeddedURL) { | ||
| 657 | next unless ($embeddedURL =~ /\b(?:youtube\.|youtu.be|ytimg\.)\b/); | ||
| 658 | |||
| 659 | $embeddedURL =~ s/[\?\&](?!v\=).*$// unless ($self->{'notrim'}); | ||
| 660 | print STDERR "--embedded IFRAME url=$embeddedURL=\n" if ($DEBUG); | ||
| 661 | $url2fetch = $embeddedURL; | ||
| 662 | $self->{'_isaYtPage'} = 1; | ||
| 663 | $self->{'id'} = $1 if ($url2fetch =~ m#\/([^\/]+)\/?$#); | ||
| 664 | $self->{'id'} =~ s/^watch\?v\=//; | ||
| 665 | $self->{'id'} =~ s/[\?\&].*$//; | ||
| 666 | $embedded_video = 1; | ||
| 667 | last; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | ##NEXT, TRY FOR YOUTUBE URLs HIDDEN IN JSON: | ||
| 671 | unless ($embedded_video) { | ||
| 672 | while ($html && $html =~ s#\"url\"\:\"([^\"]+)\"\,##so) { | ||
| 673 | my $one = $1; | ||
| 674 | my $embeddedURL = ($one =~ m#(https?\:\/\/[^\"]+)#so) ? $1 : ''; | ||
| 675 | next unless ($embeddedURL && $embeddedURL =~ /\b(?:youtube\.|youtu.be|ytimg\.)\b/o); | ||
| 676 | $embeddedURL =~ s/[\?\&](?!v\=).*$// unless ($self->{'notrim'}); | ||
| 677 | $url2fetch = $embeddedURL; | ||
| 678 | print STDERR "--embedded YOUTUBE JSON url=$url2fetch=\n" if ($DEBUG); | ||
| 679 | $self->{'_isaYtPage'} = 1; | ||
| 680 | $self->{'id'} = $1 if ($url2fetch =~ m#\/([^\/]+)\/?$#); | ||
| 681 | $self->{'id'} =~ s/^watch\?v\=//; | ||
| 682 | $self->{'id'} =~ s/[\?\&].*$//; | ||
| 683 | $embedded_video = 1; | ||
| 684 | last; | ||
| 685 | } | ||
| 686 | } | ||
| 687 | ##LAST, TRY FOR EMBEDDED RUMBLE VIDEO-IDS (NOT RUMBLE-URLS, | ||
| 688 | ## WHICH WOULD BE CAUGHT BY StreamFinder::Rumble): | ||
| 689 | unless ($embedded_video) { | ||
| 690 | unless ($self->{'youtubeonly'}) { | ||
| 691 | if ($html =~ /\bRumble\s*\(\"play\"\,\s+\{\"video\"\:\"([a-z0-9\-\_]+)\"/si) { | ||
| 692 | #EXTRACT CERTAIN EMBEDDED RUMBLE VIDEOS NOT NECESSARILY IN AN IFRAME: | ||
| 693 | my $embeddedURL = 'https://rumble.com/embed/' . $1; | ||
| 694 | my $haveRumble = 0; | ||
| 695 | print STDERR "---FOUND AN EMBEDDED RUMBLE VIDEO ($embeddedURL), SEE IF WE CAN GO WITH THAT!\n" if ($DEBUG); | ||
| 696 | eval { require 'StreamFinder/Rumble.pm'; $haveRumble = 1; }; | ||
| 697 | if ($haveRumble) { | ||
| 698 | my %globalArgs = (-debug => $DEBUG); | ||
| 699 | foreach my $arg (qw(log logfmt)) { | ||
| 700 | $globalArgs{$arg} = $self->{$arg} if (defined($self->{$arg}) && $self->{$arg}); | ||
| 701 | } | ||
| 702 | $embedded_video = new StreamFinder::Rumble($embeddedURL, %globalArgs); | ||
| 703 | return $embedded_video if (defined($embedded_video) && $embedded_video->count() > 0); | ||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | } | ||
| 708 | } | ||
| 709 | } | ||
| 710 | |||
| 711 | #NEXT: GET STREAMS, THUMBNAIL, ETC. FROM youtube-dl: | ||
| 712 | |||
| 713 | DO_YTDL: | ||
| 714 | if (defined $self->{'formats_by_url'}) { | ||
| 715 | my %formats_by_url = %{$self->{'formats_by_url'}}; | ||
| 716 | foreach my $i (keys %formats_by_url) { | ||
| 717 | $self->{'format'} = $formats_by_url{$i} if ($url =~ m#$i#i); | ||
| 718 | } | ||
| 719 | } | ||
| 720 | $self->{'format-fallback'} = $DEFAULTFALLBACK | ||
| 721 | if (!defined($self->{'format-fallback'}) && $self->{'formatonly'}); | ||
| 722 | my $ytformat = (defined $self->{'format'}) ? $self->{'format'} : $DEFAULTFMT; | ||
| 723 | my $ua = (defined $self->{'user-agent'}) | ||
| 724 | ? (' --user-agent "'.$self->{'user-agent'}.'"') : ''; | ||
| 725 | my $ytdlArgs = $self->{'youtube-dl-args'}; | ||
| 726 | $ytdlArgs .= ' ' . $self->{'youtube-dl-add-args'} | ||
| 727 | if (defined $self->{'youtube-dl-add-args'}); | ||
| 728 | $ytdlArgs .= $ua; | ||
| 729 | $ytdlArgs .= ' -f "' . $ytformat . '" ' unless ($ytformat =~ /^a(?:ny|ll)$/i); | ||
| 730 | my $try = 0; | ||
| 731 | my (@ytdldata, @ytStreams); | ||
| 732 | my $wasdashfmt = ''; | ||
| 733 | |||
| 734 | RETRYIT: | ||
| 735 | $_ = ''; | ||
| 736 | my $cmd = ''; | ||
| 737 | print STDERR "====== (RE)TRYIT: TRY=$try= getdash=$wasdashfmt=\n" if ($DEBUG); | ||
| 738 | unless ($wasdashfmt eq '1') { #RESET STREAMS UNLESS LOOKING FOR 2ND PART OF DASH STREAM: | ||
| 739 | $self->{'streams'} = []; | ||
| 740 | $self->{'cnt'} = 0; | ||
| 741 | } | ||
| 742 | if (defined($self->{'userid'}) && defined($self->{'userpw'})) { #USER HAS A LOGIN CONFIGURED: | ||
| 743 | my $uid = $self->{'userid'}; | ||
| 744 | my $upw = $self->{'userpw'}; | ||
| 745 | $cmd = $self->{'youtube-dl'} . '--username "' . $uid . '" --password "' . $upw . '" ' | ||
| 746 | . $ytdlArgs. ' "' . $url2fetch .'"'; | ||
| 747 | } else { | ||
| 748 | $cmd = $self->{'youtube-dl'} . " $ytdlArgs " . '"' . $url2fetch . '"'; | ||
| 749 | } | ||
| 750 | print STDERR "--TRY($try of 1): youtube-dl: ARGS=$ytdlArgs= FMT=$ytformat=\nYT COMMAND==>$cmd<==\n" if ($DEBUG); | ||
| 751 | $_ = `$cmd`; | ||
| 752 | print STDERR "--YT RETURNED DATA===>$_<===\n" if ($DEBUG); | ||
| 753 | @ytdldata = split /\r?\n/s; | ||
| 754 | unless ($try || scalar(@ytdldata) > 1) { #IF NOTHING FOUND, RETRY WITHOUT THE SPECIFIC FILE-FORMAT: | ||
| 755 | $try++; | ||
| 756 | if (defined $self->{'format-fallback'} && $self->{'format-fallback'}) { | ||
| 757 | print STDERR "..1a:No ($ytformat) streams found, try again with ($$self{'format-fallback'})...\n" if ($DEBUG); | ||
| 758 | $wasdashfmt = shift (@ytStreams) if ($wasdashfmt eq '1' && scalar(@ytStreams) > 0); | ||
| 759 | @ytStreams = (); #RESET HERE CASE WE HAVE AUDIO STREAM FROM DASH FORMAT! | ||
| 760 | goto RETRYIT if ($ytdlArgs =~ s/\-f\s+\"[^\"]+\"/\-f \"$$self{'format-fallback'}\"/); | ||
| 761 | } | ||
| 762 | unless ($self->{'formatonly'}) { | ||
| 763 | print STDERR "..1b:No ($ytformat) streams found, try again for any (audio, etc.)...\n" if ($DEBUG); | ||
| 764 | $wasdashfmt = shift (@ytStreams) if ($wasdashfmt eq '1' && scalar(@ytStreams) > 0); | ||
| 765 | @ytStreams = (); #RESET HERE CASE WE HAVE AUDIO STREAM FROM DASH FORMAT! | ||
| 766 | goto RETRYIT if ($ytdlArgs =~ s/\-f\s+\"[^\"]+\"//); | ||
| 767 | } | ||
| 768 | } | ||
| 769 | @ytStreams = (); | ||
| 770 | my $urlcount = 0; | ||
| 771 | #ADDED FOLLOWING TO HANDLE CRAPPED UP BITCHUTE VIDEOS: | ||
| 772 | unless (scalar(@ytdldata) > 0) { #LAST DITCH EFFORT THAT SEEMS TO SOMETIMES WORK: | ||
| 773 | $cmd = $self->{'youtube-dl'} . ' -F "' . $url2fetch . '"'; | ||
| 774 | print STDERR "--TRY(last ditch): COMMAND==>$cmd<==\n" if ($DEBUG); | ||
| 775 | $_ = `$cmd`; | ||
| 776 | print STDERR "--YT RETURNED DATA===>$_<===\n" if ($DEBUG); | ||
| 777 | @ytdldata = split /\r?\n/s; | ||
| 778 | foreach my $line (@ytdldata) { | ||
| 779 | if ($line =~ /: Checking\s+(http\S+)$/o) { | ||
| 780 | push @ytStreams, $1 unless ($self->{'secure'} && $_ !~ /^https/o); | ||
| 781 | $urlcount++; | ||
| 782 | } | ||
| 783 | } | ||
| 784 | } | ||
| 785 | |||
| 786 | return undef unless ($self->{'cnt'} > 0 || scalar(@ytdldata) > 0); | ||
| 787 | |||
| 788 | #NOTE: ytdldata is ORDERED: TITLE?, ID, STREAM-URLS, THEN THE ICON URL, THEN DESCRIPTION, LASTLY FORMATS!: | ||
| 789 | unless ($ytdldata[0] =~ m#^https?\:\/\/#) { | ||
| 790 | $_ = shift(@ytdldata); | ||
| 791 | $self->{'title'} ||= $_; | ||
| 792 | } | ||
| 793 | $self->{'_ytID'} = ''; | ||
| 794 | if ($ytdlArgs =~ /\-\-get\-id\b/ && $ytdldata[0] !~ /^https?\:/) { #GET ID (IF YOUTUBE-ID): | ||
| 795 | my $get_id = shift(@ytdldata); | ||
| 796 | $self->{'_ytID'} = $get_id if ($get_id =~ /^[a-z0-9\-\_]{11}$/i); | ||
| 797 | } | ||
| 798 | my $fmtline = ($ytdldata[$#ytdldata] =~ m#^https?\:\/\/#) ? '-none' : pop(@ytdldata); #LAST LINE IS (USUALLY) THE LIST OF FORMATS RETURNED. | ||
| 799 | my @fmtsfound = split(/\+/, $fmtline); | ||
| 800 | for (my $i=0;$i<=$#fmtsfound;$i++) { #GET STREAM URLS: | ||
| 801 | $_ = shift @ytdldata; | ||
| 802 | next unless (/^https?/o); | ||
| 803 | push @ytStreams, $_ unless ($self->{'secure'} && $_ !~ /^https/o); | ||
| 804 | } | ||
| 805 | if ($ytdlArgs =~ /get-thumbnail/o) { #GET THUMBNAIL URL: | ||
| 806 | $_ = shift @ytdldata; | ||
| 807 | $self->{'iconurl'} = $_ if (defined($_) && m#^https?\:\/\/#); | ||
| 808 | } | ||
| 809 | #GET ANY YOUTUBE-URL (LAST LINE BEFORE FORMAT (OF DESCRIPTION)) IN MANY ODYSEE VIDEOS: | ||
| 810 | #(THIS CAN BE USED BY StreamFinder::Odysee!) | ||
| 811 | $self->{'_odysee_yturl'} = pop(@ytdldata) | ||
| 812 | if ($#ytdldata >= 0 && $ytdldata[$#ytdldata] =~ m#^https?\:\/\/#); | ||
| 813 | #GET DESCRIPTION (EVERYTHING ELSE): | ||
| 814 | $self->{'description'} = ''; | ||
| 815 | while (@ytdldata) { | ||
| 816 | $self->{'description'} .= shift(@ytdldata) . "\n"; | ||
| 817 | } | ||
| 818 | push @{$self->{'streams'}}, @ytStreams; | ||
| 819 | $self->{'cnt'} = scalar @{$self->{'streams'}}; | ||
| 820 | print STDERR "-STREAM COUNT=".$self->{'cnt'}."= FMTS=".join('|',@fmtsfound)."= ICON=".$self->{'iconurl'}."=\n" if ($DEBUG); | ||
| 821 | if ($try == 0 && $wasdashfmt eq '' && $self->{'cnt'} > 0 && defined($self->{'formatv'})) { | ||
| 822 | #NOW LOOK FOR A CORRESPONDING DASH VIDEO STREAM: | ||
| 823 | print STDERR "..1:Now try for dash video stream ($$self{'formatv'})...\n" if ($DEBUG); | ||
| 824 | $wasdashfmt = '1'; | ||
| 825 | $ytformat = $self->{'formatv'}; | ||
| 826 | goto RETRYIT if ($ytdlArgs =~ s/\-f\s+\"[^\"]+\"/\-f \"$ytformat\"/); | ||
| 827 | } | ||
| 828 | unless ($try || $self->{'cnt'} > 0) { #IF NO STREAMS FOUND, RETRY WITHOUT THE SPECIFIC FILE-FORMAT: | ||
| 829 | $try++; | ||
| 830 | $self->{'formatv'} = undef; #DON'T LOOK FOR DASH VIDEO IN FALLBACK TRIES! | ||
| 831 | if (defined $self->{'format-fallback'}) { | ||
| 832 | print STDERR "..2a:No ($ytformat) streams found, try again with ($$self{'format-fallback'})...\n" if ($DEBUG); | ||
| 833 | $wasdashfmt = shift (@ytStreams) if ($wasdashfmt eq '1' && scalar(@ytStreams) > 0); | ||
| 834 | @ytStreams = (); #RESET HERE CASE WE HAVE AUDIO STREAM FROM DASH FORMAT! | ||
| 835 | goto RETRYIT if ($ytdlArgs =~ s/\-f\s+\"[^\"]+\"/\-f \"$$self{'format-fallback'}\"/); | ||
| 836 | } | ||
| 837 | unless (defined($self->{'formatonly'}) && $self->{'formatonly'}) { | ||
| 838 | print STDERR "..2b:No ($ytformat) streams found, try again for any (audio, etc.)...\n" if ($DEBUG); | ||
| 839 | $wasdashfmt = shift (@ytStreams) if ($wasdashfmt eq '1' && scalar(@ytStreams) > 0); | ||
| 840 | @ytStreams = (); #RESET HERE CASE WE HAVE AUDIO STREAM FROM DASH FORMAT! | ||
| 841 | goto RETRYIT if ($ytdlArgs =~ s/\-f\s+\"[^\"]+\"//); | ||
| 842 | } | ||
| 843 | } | ||
| 844 | |||
| 845 | #NOW MANUALLY SCRAPE YOUTUBE PAGE TO TRY TO GET artist, description, year, ETC. DIRECTLY FROM PAGE (IF A YOUTUBE SITE): | ||
| 846 | |||
| 847 | unless ($self->{'fast'}) { #(FAST MEANS SKIP SCRAPING YOUTUBE PAGE FOR ADDTL. METADATA) | ||
| 848 | $try = 0; | ||
| 849 | RETRYPAGE: | ||
| 850 | print STDERR "----(try2=$try= FETCHURL=$url2fetch= isYT?=".$self->{'_isaYtPage'}."=\n" if ($DEBUG); | ||
| 851 | if ($self->{'_isaYtPage'}) { #WE'RE A YOUTUBE PAGE, FETCH METADATA: | ||
| 852 | #CONVERT "embedded" YT PAGES TO ACTUAL PAGE (EMBEDDED PAGES DON'T HAVE THE METADATA WE'RE SEEKING!: | ||
| 853 | if ($url2fetch =~ m#^(.+?)\/embed\/([a-z0-9\-\_]{11})#i) { #TRY FETCHING YOUTUBE SITE FROM THE EMBEDDED URL: | ||
| 854 | $url2fetch = $1.'/watch?v='.$2; | ||
| 855 | } elsif ($url2fetch =~ m#^\/embed\/([a-z0-9\-\_]{11})#i) { #IF FAIL, TRY www.youtube.com: | ||
| 856 | $url2fetch = $self->{'youtube-site'} . '/watch?v=' .$1; | ||
| 857 | } | ||
| 858 | print STDERR "-2 (TRY=$try) FETCHING SCREEN URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 859 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 860 | $ua->timeout($self->{'timeout'}); | ||
| 861 | $ua->cookie_jar({}); | ||
| 862 | $ua->env_proxy; | ||
| 863 | my $html = ''; | ||
| 864 | my $response = $ua->get($url2fetch); | ||
| 865 | if ($response->is_success) { | ||
| 866 | $html = $response->decoded_content; | ||
| 867 | } else { | ||
| 868 | print STDERR $response->status_line if ($DEBUG); | ||
| 869 | my $no_wget = system('wget','-V'); | ||
| 870 | unless ($no_wget) { | ||
| 871 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 872 | $html = `wget -t 2 -T 20 -O- -o /dev/null "$url2fetch" 2>/dev/null `; | ||
| 873 | } | ||
| 874 | } | ||
| 875 | $html =~ s/\\\"/\"\;/gs; | ||
| 876 | $self->{'genre'} = $1 if ($html =~ m#\"category\"\:\"([^\"]+)#s); | ||
| 877 | if ($html =~ s#\]\}\,\"title\"\:\{\"runs\"\:\[\{\"text\"\:\"([^\"]+)\"\,\"navigationEndpoint\"\:([^\}]+)##s) { | ||
| 878 | my $two = $2; | ||
| 879 | $self->{'artist'} = $1; | ||
| 880 | $self->{'albumartist'} = $self->{'youtube-site'} . $1 if ($two =~ m#\"url\"\:\"([^\"]+)#); | ||
| 881 | } elsif ($html =~ s#\<span\s+itemprop\=\"author\"[^\>]*\>(.+?)\<\/span\>##s) { | ||
| 882 | my $one = $1; | ||
| 883 | $self->{'artist'} = $1 if ($one =~ m#itemprop\=\"name\"\s+content\=\"([^\"]+)#); | ||
| 884 | $self->{'albumartist'} = $1 if ($one =~ m#itemprop\=\"url\"\s+href\=\"([^\"]+)#); | ||
| 885 | $self->{'albumartist'} = '/' . $self->{'albumartist'} | ||
| 886 | if ($self->{'albumartist'} =~ m#^\@#); | ||
| 887 | $self->{'albumartist'} = $self->{'youtube-site'} . $self->{'albumartist'} | ||
| 888 | if ($self->{'albumartist'} =~ m#^\/#); | ||
| 889 | } | ||
| 890 | if ($html =~ s#\"videoDetails\"\:\{\"videoId\"\:\"([^\"]+)\"([^\}]+)##s) { | ||
| 891 | my $two = $2; | ||
| 892 | $self->{'id'} = $1; | ||
| 893 | $self->{'title'} = $1 if ($two =~ m#\"title\"\:\"([^\"]+)#); | ||
| 894 | $self->{'iconurl'} = $1 if ($two =~ m#\"thumbnails\"\:\[\{\"url\"\:\"([^\"]+)#); | ||
| 895 | $self->{'iconurl'} =~ s/\?.*$//; | ||
| 896 | } | ||
| 897 | if ($html =~ m#\"dateText\"\:\{([^\}]+)\}#s) { | ||
| 898 | my $one = $1; | ||
| 899 | $self->{'year'} = $1 if ($one =~ /(\d\d\d\d)/); | ||
| 900 | } | ||
| 901 | $self->{'articonurl'} = $1 if ($html =~ m#(?:\"CHANNEL\"\,\"image\"|\"videoOwnerRenderer\")\:\{\"thumbnails"\:\[\{\"url\"\:\"([^\"]+)#s); | ||
| 902 | print "--YT:2 CHANNEL ICON URL1=".$self->{'articonurl'}."=\n" if ($DEBUG); | ||
| 903 | unless ($self->{'articonurl'}) { | ||
| 904 | my $ownerstuff = ($html =~ m#\"videoOwnerRenderer\"\:\{([^\}]+)#s) ? $1 : ''; | ||
| 905 | $self->{'articonurl'} = $1 if ($ownerstuff =~ /\"url\"\:\"([^\"]+)/); | ||
| 906 | } | ||
| 907 | print "--YT:2 CHANNEL ICON URL2=".$self->{'articonurl'}."=\n" if ($DEBUG); | ||
| 908 | } elsif (!$try && $self->{'_ytID'}) { #WE'RE NOT A YOUTUBE PAGE, BUT WE HAVE THE YT ID, SO TRY TO FETCH IT FOR METADATA: | ||
| 909 | print STDERR "--WE ARE NOT A YT PAGE, BUT ytID=".$self->{'_ytID'}."= SO WE WILL TRY AGAIN!\n" if ($DEBUG); | ||
| 910 | ++$try; | ||
| 911 | ++$self->{'_isaYtPage'}; | ||
| 912 | $url2fetch = $self->{'youtube-site'} . '/watch?v=' . $self->{'_ytID'}; | ||
| 913 | goto RETRYPAGE; | ||
| 914 | } | ||
| 915 | } | ||
| 916 | if ($self->{'cnt'} <= 0 && $wasdashfmt && $wasdashfmt ne '1') { | ||
| 917 | @{$self->{'streams'}} = ($wasdashfmt); #FALLBACK TO ANY RESIDUAL DASH AUDIO-STREAM (from formatv). | ||
| 918 | $self->{'cnt'} = 1; | ||
| 919 | } | ||
| 920 | $self->{'total'} = $self->{'cnt'}; | ||
| 921 | $self->{'imageurl'} = $self->{'iconurl'}; | ||
| 922 | $self->{'Url'} = ($self->{'cnt'} > 0) ? $self->{'streams'}->[0] : ''; | ||
| 923 | if (defined($self->{'formatv'}) && $self->{'cnt'} > 1) { | ||
| 924 | my $video_stream = $self->{'streams'}->[$self->{'cnt'}-1]; | ||
| 925 | $self->{'streams'}->[$self->{'cnt'}-1] = $self->{'Url'}; | ||
| 926 | $self->{'Url'} .= "|" . $video_stream; | ||
| 927 | $self->{'streams'}->[0] = $self->{'Url'}; | ||
| 928 | } | ||
| 929 | print STDERR "-count=".$self->{'cnt'}."= iconurl=".$self->{'iconurl'}."=\n" if ($DEBUG); | ||
| 930 | print STDERR "--SUCCESS: 1st stream=".$self->{'Url'}."= total=".$self->{'total'}."=\n" | ||
| 931 | if ($DEBUG && $self->{'cnt'} > 0); | ||
| 932 | if ($self->{'description'} =~ /\w/) { | ||
| 933 | $self->{'description'} =~ s/\s+$//; | ||
| 934 | } else { | ||
| 935 | $self->{'description'} = $self->{'title'}; | ||
| 936 | } | ||
| 937 | foreach my $i (qw(title artist albumartist description genre)) { | ||
| 938 | $self->{$i} = HTML::Entities::decode_entities($self->{$i}); | ||
| 939 | $self->{$i} = uri_unescape($self->{$i}); | ||
| 940 | $self->{$i} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/egso; | ||
| 941 | } | ||
| 942 | print STDERR "-2: title=".$self->{'title'}."= id=".$self->{'id'}."= artist=".$self->{'artist'}."= year(Published)=".$self->{'year'}."=\n" if ($DEBUG); | ||
| 943 | if ($DEBUG) { | ||
| 944 | foreach (my $i=0;$i<$self->{'cnt'};$i++) { | ||
| 945 | print STDERR "===== Stream.YT($i)=".$self->{'streams'}->[$i]."=\n"; | ||
| 946 | } | ||
| 947 | print STDERR "\n"; | ||
| 948 | foreach my $i (sort keys %{$self}) { | ||
| 949 | print STDERR "--KEY.YT=$i= VAL=".$self->{$i}."=\n"; | ||
| 950 | } | ||
| 951 | } | ||
| 952 | $self->_log($url2fetch); | ||
| 953 | |||
| 954 | bless $self, $class; #BLESS IT! | ||
| 955 | |||
| 956 | return $self; | ||
| 957 | } | ||
| 958 | |||
| 959 | sub getImageData | ||
| 960 | { | ||
| 961 | my $self = shift; | ||
| 962 | return $self->getIconData(); | ||
| 963 | } | ||
| 964 | |||
| 965 | 1 |