Downloading YouTube Video List for Your Channel (using PHP and Google API)

Create API Key using Google Developer Console

https://console.developers.google.com

Create videolist_download.php File and Paste in this Code

Replace $api_key and $api_playlist_id with your own values.

#!/usr/bin/php
<?php

// List videos, fifty at a time

$api_key = "google_api_key";
$api_playlist_id = "google_playlist_id";
$api_url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=" . $api_playlist_id . "&key=" . $api_key;

$pageToken = '';
$pageNumber = 1;

// Create directory

mkdir("./videolist", 0700, true);

// Remove previous files

foreach (glob('./videolist/vidlist*.json') as $file) {
    if(is_file($file)) {
        unlink($file);
    }
}

function downloadVideos($api_url, $pageToken=null) {
    if ($pageToken != '') {
        $videos = file_get_contents($api_url . '&pageToken=' . $pageToken);
    } else {
        $videos = file_get_contents($api_url);
    }

    return $videos;
}

do {
    $videos = downloadVideos($api_url, $pageToken);
    file_put_contents("./videolist/vidlist" . $pageNumber . '.json', $videos);
    $pageNumber++;
    $videos_json = json_decode($videos);
    if (!empty($videos_json->nextPageToken)) {
        $pageToken = $videos_json->nextPageToken;
    } else {
        $pageToken = '';
    }
} while (!empty($pageToken));
Create videolist_to_html.php File and Paste in this Code
#!/usr/bin/php
<?php

ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>YouTube videos</title>
<style>
table {border-collapse: collapse; width: 100%}
table td {border: 1px solid black; padding: 12px}
.wrap {word-break: break-all;}
.nowrap {white-space: nowrap}
</style>
</head>
<table>

<?php
$file_number = 1;

// Convert URLs to Links
// https://stackoverflow.com/questions/1960461/convert-plain-text-urls-into-html-hyperlinks-in-php
// url regex
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';

while ( file_exists("videolist/vidlist" . $file_number . ".json")) {
    $contents_json = file_get_contents("videolist/vidlist" . $file_number . ".json");
    $contents = json_decode($contents_json);

    foreach ($contents->items as $item) {
        if ($item->kind == 'youtube#playlistItem') {
            $rowbuffer = '<tr>';
            $rowbuffer .= '<td class="wrap"><strong>';
            $rowbuffer .= $item->snippet->title . "</strong><br />";
            $description = nl2br($item->snippet->description);
            $description = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $description);

            $rowbuffer .= $description;
            $rowbuffer .= '<td class="nowrap">';
            $rowbuffer .= substr($item->snippet->publishedAt, 0, 10);
            $rowbuffer .= '<td class="nowrap">';
            $rowbuffer .= '<a href="https://studio.youtube.com/video/' . $item->contentDetails->videoId . '/edit" target="_blank">Edit</a>';
            $rowbuffer .= '<td class="nowrap">';
            $rowbuffer .= '<a href="https://youtu.be/' . $item->contentDetails->videoId . '" target="_blank">View</a>';
            echo $rowbuffer;
        }
    }
    $file_number++;
}
echo '</table>';
$output_buffer = ob_get_contents();
ob_end_clean();
file_put_contents('videos.html', $output_buffer);
Give Scripts Executable Permissions
chmod +x videolist*.php
Run Scripts
./videolist_download.php
./videolist_to_html.php

Open videos.html in a web browser to view your videos.

Leave a comment

Your email address will not be published. Required fields are marked *