| 該項目的開發完全由社區資助。考慮捐款以支持! |

YouTubeexplode是一個庫,它為查詢YouTube視頻,播放列表和頻道的元數據提供了一個界面,以及解決和下載視頻流和封閉的標題軌道。在抽象層的後面,該庫是通過刮擦原始頁面數據並利用反向設計的內部端點來工作的。
對這個庫的內部運作興趣嗎?請參閱反向工程YouTube文章。
擴展程序包:
通過使用此項目或其源代碼,出於任何目的,以任何形式或形式,您可以授予以下所有聲明的隱性協議:
要了解有關戰爭以及如何提供幫助的更多信息,請單擊此處。榮耀烏克蘭! ?
dotnet add package YoutubeExplode 
YouTubeexplode通過單個入口點( YoutubeClient類)公開其功能。創建此類實例,並在Videos , Playlists , Channels和Search屬性上使用提供的操作來發送請求。
要檢索與YouTube視頻相關的元數據,請致電Videos.GetAsync(...) :
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
// You can specify either the video URL or its ID
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs" ;
var video = await youtube . Videos . GetAsync ( videoUrl ) ;
var title = video . Title ; // "Collections - Blender 2.80 Fundamentals"
var author = video . Author . ChannelTitle ; // "Blender"
var duration = video . Duration ; // 00:07:20 每個YouTube視頻都有許多可用的流,在容器,視頻質量,比特率,幀速率和其他參數方面有所不同。此外,根據其內容將流進一步分為三類:
警告:Muxed Streams既包含音頻和視頻,但是這些流的質量限制(高達720p30)。要以最高可用質量下載視頻,您將需要分別解決最佳的僅音頻和僅視頻流的流程,然後將它們一起介紹。可以在youtubeexplode.converter軟件包的幫助下使用FFMPEG執行Muxing過程。
警告:YouTube棄用Muxed Streams,並且不能保證每個視頻可用。如果可能的話,請避免過多地依靠它們,而是使用提供的只有音頻和僅視頻的流手動執行Muxing。
您可以通過調用Videos.Streams.GetManifestAsync(...)列出特定視頻的所有可用流的清單:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs" ;
var streamManifest = await youtube . Videos . Streams . GetManifestAsync ( videoUrl ) ;一旦獲得清單,您就可以通過流進行過濾並識別您感興趣的流程:
using YoutubeExplode ;
using YoutubeExplode . Videos . Streams ;
// ...
// Get the highest bitrate audio-only stream
var streamInfo = streamManifest . GetAudioOnlyStreams ( ) . GetWithHighestBitrate ( ) ;
// ...or the highest quality MP4 video-only stream
var streamInfo = streamManifest
. GetVideoOnlyStreams ( )
. Where ( s => s . Container == Container . Mp4 )
. GetWithHighestVideoQuality ( )最後,您可以使用Videos.Streams.GetAsync(...)解決指定元數據表示的實際流,或直接下載到帶有Videos.Streams.DownloadAsync(...)的文件中:
// ...
// Get the actual stream
var stream = await youtube . Videos . Streams . GetAsync ( streamInfo ) ;
// Download the stream to a file
await youtube . Videos . Streams . DownloadAsync ( streamInfo , $ "video. { streamInfo . Container } " ) ;警告:儘管可以使用流元數據中的
Url屬性來訪問基礎內容,但您需要一系列精心設計的HTTP請求才能這樣做。強烈建議使用Videos.Streams.GetAsync(...)或Videos.Streams.DownloadAsync(...),因為它們將為您執行所有繁重的工作。
可以以與媒體流相似的方式下載封閉字幕。要獲取可用的封閉字幕曲目的列表,請致電Videos.ClosedCaptions.GetManifestAsync(...) :
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs" ;
var trackManifest = await youtube . Videos . ClosedCaptions . GetManifestAsync ( videoUrl ) ;然後檢索特定曲目的元數據:
// ...
// Find closed caption track in English
var trackInfo = trackManifest . GetByLanguage ( "en" ) ;最後,使用Videos.ClosedCaptions.GetAsync(...)獲取曲目的實際內容:
// ...
var track = await youtube . Videos . ClosedCaptions . GetAsync ( trackInfo ) ;
// Get the caption displayed at 0:35
var caption = track . GetByTime ( TimeSpan . FromSeconds ( 35 ) ) ;
var text = caption . Text ; // "collection acts as the parent collection"您還可以使用Videos.ClosedCaptions.DownloadAsync(...)以SRT文件格式下載封閉的字幕曲目:
// ...
await youtube . Videos . ClosedCaptions . DownloadAsync ( trackInfo , "cc_track.srt" ) ;您可以通過調用Playlists.GetAsync(...)方法來獲取與YouTube播放列表相關聯的元數據:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6" ;
var playlist = await youtube . Playlists . GetAsync ( playlistUrl ) ;
var title = playlist . Title ; // "First Steps - Blender 2.80 Fundamentals"
var author = playlist . Author . ChannelTitle ; // "Blender" 要獲取播放列表中的視頻,請致電Playlists.GetVideosAsync(...) :
using YoutubeExplode ;
using YoutubeExplode . Common ;
var youtube = new YoutubeClient ( ) ;
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6" ;
// Get all playlist videos
var videos = await youtube . Playlists . GetVideosAsync ( playlistUrl ) ;
// Get only the first 20 playlist videos
var videosSubset = await youtube . Playlists . GetVideosAsync ( playlistUrl ) . CollectAsync ( 20 ) ;您還可以在不等待整個列表加載的情況下進行迭代列舉視頻:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6" ;
await foreach ( var video in youtube . Playlists . GetVideosAsync ( playlistUrl ) )
{
var title = video . Title ;
var author = video . Author ;
}如果您需要精確控制發送到YouTube的多少請求,請使用Playlists.GetVideoBatchesAsync(...)返回批量包裝的視頻:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6" ;
// Each batch corresponds to one request
await foreach ( var batch in youtube . Playlists . GetVideoBatchesAsync ( playlistUrl ) )
{
foreach ( var video in batch . Items )
{
var title = video . Title ;
var author = video . Author ;
}
}注意:您可以製作播放列表ID來獲取特殊的自動生成的播放列表,例如音樂混音,流行的頻道上傳,喜歡的視頻等。有關更多信息,請參見此參考。
您可以通過調用Channels.GetAsync(...)方法來獲取與YouTube頻道關聯的元數據:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var channelUrl = "https://youtube.com/channel/UCSMOQeBJ2RAnuFungnQOxLg" ;
var channel = await youtube . Channels . GetAsync ( channelUrl ) ;
var title = channel . Title ; // "Blender"您還可以使用Channels.GetByUserAsync(...)來獲取頻道元數據或配置文件。
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var channelUrl = "https://youtube.com/user/BlenderFoundation" ;
var channel = await youtube . Channels . GetByUserAsync ( channelUrl ) ;
var id = channel . Id ; // "UCSMOQeBJ2RAnuFungnQOxLg"要通過Slug或Legacy自定義URL獲取頻道元數據,請使用Channels.GetBySlugAsync(...) :
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var channelUrl = "https://youtube.com/c/BlenderFoundation" ;
var channel = await youtube . Channels . GetBySlugAsync ( channelUrl ) ;
var id = channel . Id ; // "UCSMOQeBJ2RAnuFungnQOxLg"要通過句柄或自定義URL獲取頻道元數據,請使用Channels.GetByHandleAsync(...) :
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
var channelUrl = "https://youtube.com/@BeauMiles" ;
var channel = await youtube . Channels . GetByHandleAsync ( channelUrl ) ;
var id = channel . Id ; // "UCm325cMiw9B15xl22_gr6Dw" 要獲取由頻道上傳的視頻列表,請致電Channels.GetUploadsAsync(...) :
using YoutubeExplode ;
using YoutubeExplode . Common ;
var youtube = new YoutubeClient ( ) ;
var channelUrl = "https://youtube.com/channel/UCSMOQeBJ2RAnuFungnQOxLg" ;
var videos = await youtube . Channels . GetUploadsAsync ( channelUrl ) ;您可以通過調用Search.GetResultsAsync(...)方法執行搜索查詢。每個搜索結果可能代表視頻,播放列表或頻道,因此您需要應用模式匹配以處理相應的情況:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
await foreach ( var result in youtube . Search . GetResultsAsync ( "blender tutorials" ) )
{
// Use pattern matching to handle different results (videos, playlists, channels)
switch ( result )
{
case VideoSearchResult video :
{
var id = video . Id ;
var title = video . Title ;
var duration = video . Duration ;
break ;
}
case PlaylistSearchResult playlist :
{
var id = playlist . Id ;
var title = playlist . Title ;
break ;
}
case ChannelSearchResult channel :
{
var id = channel . Id ;
var title = channel . Title ;
break ;
}
}
}要將結果限制為特定類型,請使用Search.GetVideosAsync(...) , Search.GetPlaylistsAsync(...)或Search.GetChannelsAsync(...) :
using YoutubeExplode ;
using YoutubeExplode . Common ;
var youtube = new YoutubeClient ( ) ;
var videos = await youtube . Search . GetVideosAsync ( "blender tutorials" ) ;
var playlists = await youtube . Search . GetPlaylistsAsync ( "blender tutorials" ) ;
var channels = await youtube . Search . GetChannelsAsync ( "blender tutorials" ) ;與播放列表類似,您還可以通過調用Search.GetResultBatchesAsync(...)來列舉結果:
using YoutubeExplode ;
var youtube = new YoutubeClient ( ) ;
// Each batch corresponds to one request
await foreach ( var batch in youtube . Search . GetResultBatchesAsync ( "blender tutorials" ) )
{
foreach ( var result in batch . Items )
{
switch ( result )
{
case VideoSearchResult videoResult :
{
// ...
}
case PlaylistSearchResult playlistResult :
{
// ...
}
case ChannelSearchResult channelResult :
{
// ...
}
}
}
}您可以通過提供對應於預先介紹的YouTube帳戶的cookie來訪問私人視頻和播放列表。為此,請使用接受IReadOnlyList<Cookie>構造函數創建YoutubeClient的實例:
using YoutubeExplode ;
// Perform authentication and extract cookies
var cookies = .. . ;
// Cookie collection must be of type IReadOnlyList<System.Net.Cookie>
var youtube = new YoutubeClient ( cookies ) ;為了實際執行身份驗證,您可以使用嵌入式瀏覽器(例如WebView)將用戶導航到YouTube登錄頁面,讓他們登錄,然後從瀏覽器中提取cookie。
YouTubeexplode中的“爆炸”來自拆分字符串的PHP函數的名稱, explode(...) 。當我啟動該庫的開發時,我閱讀的大多數參考源代碼都是用PHP編寫的,因此是該名稱的靈感。