Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Youtube WatchLater to Playlist Script
* Youtube Watch Later List To Playlist Script
* This script fetches your watch later list and outputs it as an M3U playlist
* Requirements:
* PHP enabled webserver
* Google PHP API (
* A YouTube API key (Oauth2, can be registered at
* Some kittens to sacrifice
* Initialization
* First ensure your webserver can write a file called "key.php" to the directory
* you put this file in. E.g. "touch key.php ; chmod ugo+w key.php"
* Next open the appropriate URL in your browser. You will be asked to pair
* this script with your YouTube-Account. If you configured everything correctly
* it will output the playlist and save your access codes in the key.php mentioned
* above. Successive requests do not require additional authentication. You may now
* put the URL in your YT-enabled media player like VLC.
* Based on examples by DomSammut (
* 2015 Florian Knodt <>
// Set this to the directory of google api php client
$REDIRECT = 'http://localhost/yt/ytwll2m3u.php';
$APPNAME = 'YouTube WLL Import Test';
require_once 'Google/autoload.php';
$client = new Google_Client();
if(file_exists('key.php')) {
require_once 'key.php';
if(isset($key)) {
$_SESSION['token'] = $key;
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
$_SESSION['token'] = $client->getAccessToken();
if (isset($_SESSION['token'])) {
$keydata = '<?php $key=\''.$_SESSION['token'].'\'; ?>';
//echo '<code>' . $_SESSION['token'] . '</code>';
if(!is_writable('key.php')) {
die('key.php can not be written - please put this code into key.php: '.$keydata);
file_put_contents('key.php', $keydata);
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
try {
// Call the channels.list method to retrieve information about the
// currently authenticated user's channel.
$channelsResponse = $youtube->channels->listChannels('contentDetails', array(
'mine' => 'true',
$playlist = '#EXTM3U'."\n";
foreach ($channelsResponse['items'] as $channel) {
// Extract the unique playlist ID that identifies the list of videos
// uploaded to the channel, and then call the playlistItems.list method
// to retrieve that list.
$uploadsListId = $channel['contentDetails']['relatedPlaylists']['watchLater'];
$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => $uploadsListId,
'maxResults' => 50
foreach ($playlistItemsResponse['items'] as $playlistItem) {
$playlist .= '#EXTINF:-1,'.$playlistItem['snippet']['title']."\n".''.$playlistItem['snippet']['resourceId']['videoId']."\n";
while(isset($playlistItemsResponse['nextPageToken'])) {
$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => $uploadsListId,
'maxResults' => 50,
'nextPageToken' => $playlistItemsResponse['nextPageToken'],
foreach ($playlistItemsResponse['items'] as $playlistItem) {
//$htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
$playlist .= '#EXTINF:-1,'.$playlistItem['snippet']['title']."\n".''.$playlistItem['snippet']['resourceId']['videoId']."\n";
} catch (Google_ServiceException $e) {
echo sprintf('<p>A service error occurred: <code>%s</code></p>',
} catch (Google_Exception $e) {
echo sprintf('<p>An client error occurred: <code>%s</code></p>',
$_SESSION['token'] = $client->getAccessToken();
} else {
$state = mt_rand();
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorise access</a> before proceeding.<p>
if(isset($playlist)) die($playlist);
<!doctype html>
<title>My Uploads</title>
<?php echo $htmlBody?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.