mrmysql/youtube-transcript
Composer 安装命令:
composer require mrmysql/youtube-transcript
包简介
Retrieving transcripts for youtube videos.
README 文档
README
YouTube Transcript is a PHP library designed to fetch transcripts for YouTube videos. It allows you to retrieve captions in multiple languages, including auto-generated captions, and provides functionality to translate transcripts into other available languages. Inspired by jdepoix/youtube-transcript-api. I've generated this readme using one of these fancy [A]utocompletion [I]nstruments, so it might be incorrect somewhere, but overall looks good.
Features
- Retrieve both manually created and auto-generated transcripts.
- Fetch transcripts in various languages.
- Translate transcripts to other available languages.
Requirements
- PHP 7.4 or higher (including support for PHP 8.2)
- PSR-18 HTTP Client (
psr/http-client) and HTTP Factory (psr/http-factory)
Installation
To include this library in your project, use Composer:
composer require mrmysql/youtube-transcript
Usage
1. Initialize the Transcript Fetcher
To begin, you need to set up an HTTP client and a request factory that implements the PSR-18 and PSR-17 interfaces, respectively. These dependencies are required by the TranscriptListFetcher class.
use MrMySQL\YoutubeTranscript\TranscriptListFetcher; use GuzzleHttp\Client; use GuzzleHttp\Psr7\HttpFactory; $http_client = new Client(); $request_factory = new HttpFactory(); $stream_factory = new HttpFactory(); // GuzzleHttp\Psr7\HttpFactory implements StreamFactoryInterface $fetcher = new TranscriptListFetcher($http_client, $request_factory, $stream_factory);
2. Fetch the Transcript List
You can fetch the transcript list for a given YouTube video ID:
$video_id = 'YOUR_YOUTUBE_VIDEO_ID'; $transcript_list = $fetcher->fetch($video_id);
3. Retrieve and Display Transcripts
You can iterate over the available transcripts and display them:
foreach ($transcript_list as $transcript) { echo $transcript . "\n"; }
4. Fetch a Specific Transcript
To fetch a transcript in a specific language:
$language_codes = ['en', 'es']; // Prioritized language codes $transcript = $transcript_list->findTranscript($language_codes); $transcript_text = $transcript->fetch(); print_r($transcript_text);
You can also take all available locale codes directly from the list:
$language_codes = $transcript_list->getAvailableLanguageCodes(); $transcript = $transcript_list->findTranscript($language_codes); $transcript_text = $transcript->fetch(); print_r($transcript_text);
5. Translate a Transcript
If translation is available, you can translate the transcript to another language:
$translated_transcript = $transcript->translate('fr'); $translated_text = $translated_transcript->fetch(); print_r($translated_text);
Note: Cookie-based authentication is currently not supported due to recent changes in the YouTube API. Age-restricted videos and some protected content cannot be accessed until a new authentication method is implemented.
Exception Handling
This library comes with a set of custom exceptions to handle different scenarios:
YouTubeRequestFailedException: Thrown when the YouTube request fails.NoTranscriptFoundException: Thrown when no transcript is found for the given language codes.NotTranslatableException: Thrown when attempting to translate a transcript that is not translatable.TranscriptsDisabledException: Thrown when transcripts are disabled for the video.TooManyRequestsException: Thrown when YouTube imposes a rate limit.PoTokenRequiredException: Thrown when a PO Token is required to retrieve the transcript (please open a GitHub issue if you see this).
Example:
try { $transcript = $transcript_list->findGeneratedTranscript(['en']); print_r($transcript->fetch()); } catch (YouTubeRequestFailedException $e) { echo "Failed to fetch the transcript: " . $e->getMessage(); }
mrmysql/youtube-transcript 适用场景与选型建议
mrmysql/youtube-transcript 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 234.65k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2024 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mrmysql/youtube-transcript 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mrmysql/youtube-transcript 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 234.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 13
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: WTFPL
- 更新时间: 2024-08-20