搜索插件、代码仓与 APP 分类

插件详情v3.1.0

youtube_explode_dart

YouTube Explode 库的 Dart 版本。无需 YouTube API 密钥即可支持多个 API 功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
5.9万
喜欢数
699
Pub 分数
140

安装插件

flutter pub add youtube_explode_dart

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

youtube_explode_dart 是一个 Dart 库,它复制了 YouTube Explode 项目的功能,使开发者能够在无需 YouTube API 密钥的情况下直接与 YouTube 内容交互。它通过解析 YouTube 的公开数据,支持获取视频详情、播放列表、搜索结果等。该库专为跨平台使用而设计,可在 Android、iOS、Web、macOS、Windows 和 Linux 上无缝运行。该包非常适合需要实时访问 YouTube 数据且设置简单的应用程序。

适合场景

  • 获取视频元数据
  • 搜索 YouTube 视频
  • 提取播放列表内容
  • 构建 YouTube 客户端
  • 创建具有动态内容的视频播放器

主要特点

  • 无需 API 密钥
  • 跨平台支持
  • 实时数据解析
  • 轻便且快速
  • 支持视频、播放列表和搜索操作

适合对象

  • 基于YouTube的应用程序
  • 媒体发现工具
  • 教育平台
  • 内容聚合服务
  • 离线视频探索者

使用建议

  • 依赖公开的 YouTube HTML 解析
  • 可能因 YouTube 界面更改而失效
  • 不适合高频率请求
  • 有限的官方支持
  • 由于直接抓取,可能存在速率限制

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

YoutubeExplodeDart

This is a port of the YoutubeExplode library from C#, most of the functions, doc comments, readme information, is taken from YoutubeExplode repository.

Pub Version License Lint

It used to build Youtube Downloader Flutter (A cross-platform application to download video streams from youtube using this library & flutter)


YoutubeExplode is a library that provides an interface to query metadata of YouTube videos, playlists and channels, as well as to resolve and download video streams and closed caption tracks. Behind a layer of abstraction, the library parses raw page content and uses reverse-engineered AJAX requests to retrieve information. As it doesn't use the official API, there's also no need for an API key and there are no usage quotas.

Features

  • Retrieve metadata on videos, playlists, channels, streams, and closed captions
  • Execute search queries and get resulting videos.
  • Get or download video streams.
  • Get closed captions.
  • Get video comments.

Usage

Install

Add the dependency to the pubspec.yaml (Check for the latest version)

youtube_explode_dart: ^2.3.4

Import the library

import 'package:youtube_explode_dart/youtube_explode_dart.dart';
Getting metadata of a video

The following example shows how you can extract various metadata from a YouTube video:

// You can provide either a video ID or URL as String or an instance of `VideoId`.
var video = yt.videos.get('https://youtube.com/watch?v=Dpp1sIL1m5Q'); // Returns a Video instance.

var title = video.title; // "Scamazon Prime"
var author = video.author; // "Jim Browning"
var duration = video.duration; // Instance of Duration - 0:19:48.00000
Downloading a video stream

Every YouTube video has a number of streams available. These streams may have different containers, video quality, bitrate, etc.

On top of that, depending on the content of the stream, the streams are further divided into 3 categories:

  • Muxed streams -- contain both video and audio (available only in 360p)
  • Audio-only streams -- contain only audio
  • Video-only streams -- contain only video

You can request the stream manifest to get available streams for a particular video:

var yt = YoutubeExplode();

var manifest = yt.videos.streams.getManifest('Dpp1sIL1m5Q');

print(manifest); // Prints the list of streams available for the video.

// If you want to specify which youtube clients to use to get the manifest you can do so:
var manifest = yt.videos.streams.getManifest(videoId, ytClients: [
  YoutubeApiClient.safari,
  YoutubeApiClient.androidVr  
]); // The streams provided by both clients will be merged.

Once you get the manifest, you can filter through the streams and choose the one you're interested in downloading:

// highest bitrate audio-only stream
var streamInfo = streamManifest.audioOnly.withHigestBitrate();

// MP4 video-only stream
var streamInfo.videoOnly.where((e) => e.container == Container);

// Muxed (audio + video) stream with the highest video quality. NOTE: Don't rely on this, muxed streams are limited to 360p30.
var streamInfo = streamManifest.muxed.withHigestVideoQuality();

// HLS (m3u8) streams are also supported
var streamInfo = streamManifest.hls;

Finally, you can get the actual Stream object represented by the metadata:

// Get the actual byte stream
var stream = yt.video.streams.get(streamInfo);

// Open a file for writing.
var file = File(filePath);
var fileStream = file.openWrite();

// Pipe all the content of the stream into the file.
await stream.pipe(fileStream);

// Close the file.
await fileStream.flush();
await fileStream.close();

While it may be tempting to just always use muxed streams, it's important to note that they are limited in quality. Muxed streams don't go beyond 360p30.

If you want to download the video in maximum quality, you need to download the audio-only and video-only streams separately and then mux them together on your own. There are tools like FFmpeg that let you do that.

Using a signature solver

Some youtube clients require you to complete a js challenge in order to download a video. This requires having a full js runtime like deno, nodejs or quickjs. Currently the only implemented runtime is deno, see the following snippet for how to use it:

import 'package:youtube_explode_dart/solvers.dart';

final solver = await DenoEJSSolver.init();
var yt = YoutubeExplode(jsSolver: solver);

Working with playlists

Among other things, YoutubeExplode also supports playlists:

var yt = YoutubeExplode();

// Get playlist metadata.
var playlist = await yt.playlists.get('xxxxx');

var title = playlist.title;
var author = playlist.author;

  await for (var video in yt.playlists.getVideos(playlist.id)) {
    var videoTitle = video.title;
    var videoAuthor = video.author;
  }

var playlistVideos = await yt.playlists.getVideos(playlist.id);

// Get first 20 playlist videos.
var somePlaylistVideos = await yt.playlists.getVideos(playlist.id).take(20);
Extracting closed captions

Similarly, to streams, you can extract closed captions by getting the manifest and choosing the track you're interested in:

  var yt = YoutubeExplode();

  var trackManifest = await yt.videos.closedCaptions.getManifest('_QdPW8JrYzQ')

  var trackInfo = manifest.getByLanguage('en'); // Get english caption.
  
  if (trackInfo != null)
  {
     // Get the actual closed caption track.
     var track = await youtube.videos.closedCaptions.get(trackInfo);
      
    // Get the caption displayed at 1:01
    var caption = track.getByTime(Duration(seconds: 61));
    var text = caption?.text; // "And the game was afoot."
  }
Get a list of related videos
var video = yt.videos.get('https://youtube.com/watch?v=Dpp1sIL1m5Q');
var relatedVideos = await yt.videos.getRelatedVideos(video); // video must be a Video instance.
print(relatedVideos); //prints the list of related videos

// to get the next page of related videos
relatedVideos = await relatedVideos.nextPage();

If no related video is found getRelatedVideos or nextPage will return null.

Get comments

You can easily get the video comments of a given video, the return value of comments.getComments(video) is a list-like object which behaves exactly like a List but has an additional method nextPage() which is used in order to get the next comments, it returns null when there are no comments to be fetched anymore.

var comments = await yt.videos.comments.getComments(video);

var replies = await yt.videos.comments.getReplies(comment); // Fetch the comment replies 
Cleanup

You need to close YoutubeExplode's http client, when done otherwise this could halt the dart process.

yt.close();
Troubleshooting

If you encounter any issues, please check that it has not been reported already in the issues section.

When reporting a new issue, make sure to follow the issue template and report the logs after having enabled the logging:

import 'package:logging/logging.dart';

...

// Before any YoutubeExplode code
Logger.root.level = Level.FINER;
Logger.root.onRecord.listen((e)  {
  print(e);
  if (e.error != null) {
   print(e.error);
   print(e.stackTrace);
  }
});

To the very minimum the video id causing the issue (if applicable) and the code snippet that is causing the issue must be provided.

Examples:

More examples available on GitHub.


Check the api documentation for additional information. You can find how most APIs can be used in the files inside the test/ folder.

Credits
  • Tyrrrz for creating YoutubeExplode in C#
  • Hexer10 (me) who ported the library over to Dart.
  • yt-dlp for documentation and reverse engineering about the YouTube apis, and implementation of the EJS modules.
  • All the Contributors of this repository.

所属分类

依赖

依赖清单 12 项

源码仓库

使用此插件的项目