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.
- Retrieve both manually created and auto-generated transcripts.
- Fetch transcripts in various languages.
- Translate transcripts to other available languages.
- PHP 7.4 or higher (including support for PHP 8.2)
- PSR-18 HTTP Client (
psr/http-client) and HTTP Factory (psr/http-factory)
To include this library in your project, use Composer:
composer require mrmysql/youtube-transcriptTo 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);You can fetch the transcript list for a given YouTube video ID:
$video_id = 'YOUR_YOUTUBE_VIDEO_ID';
$transcript_list = $fetcher->fetch($video_id);You can iterate over the available transcripts and display them:
foreach ($transcript_list as $transcript) {
echo $transcript . "\n";
}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);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.
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).
try {
$transcript = $transcript_list->findGeneratedTranscript(['en']);
print_r($transcript->fetch());
} catch (YouTubeRequestFailedException $e) {
echo "Failed to fetch the transcript: " . $e->getMessage();
}