I’m trying to build a service that extracts direct video links from Google Drive files so I can embed them in web players like JWPlayer and VideoJS on my website. I want something similar to existing API services but I can’t figure out the right approach.
I found some code on GitHub and modified it for my needs. The extraction part works and I get video URLs, but when I try to play them I get 403 permission errors. The videos won’t load in the player.
I think the issue might be related to IP restrictions or authentication requirements. Do I need to implement OAuth 2.0 or handle IP whitelisting to access Google’s video APIs properly?
Here’s my main class for handling the drive links:
namespace VideoExtractor;
class DriveParser
{
protected $endpoint;
protected $filename = '';
public $videoSources;
protected $qualityTags = [
37, 22, 59, 18
];
protected $formatMap = [
'18' => '360p',
'59' => '480p',
'22' => '720p',
'37' => '1080p',
'82' => '360p_3d',
'83' => '240p_3d',
'84' => '720p_3d',
'85' => '1080p_3d'
];
public function setQualityTags(array $tags)
{
$this->qualityTags = $tags;
}
public function setFormatMap(array $formats)
{
$this->formatMap = $formats + $this->formatMap;
}
public function setFilename($name)
{
$this->filename = $name;
}
public function extractUrls($driveUrl)
{
$results = [];
if($this->parseFileId($driveUrl)) {
$response = $this->fetchVideoInfo();
if($response && $this->extractValue($response, 'status=', '&') === 'ok') {
$streamData = $this->extractValue($response, 'fmt_stream_map=', '&');
$urlList = explode(',', urldecode($streamData));
foreach($urlList as $urlData) {
list($tag, $videoUrl) = explode('|', $urlData);
if(in_array($tag, $this->qualityTags)) {
$results[$this->formatMap[$tag]] = preg_replace("/[^\/]+\.google\.com/", "redirector.googlevideo.com", $videoUrl);
}
}
}
}
$this->videoSources = $results;
}
public function getPlayerConfig($playerType = 'jwplayer')
{
$config = [];
$urlKey = ($playerType == 'jwplayer') ? 'src' : 'file';
foreach($this->videoSources as $quality => $url) {
$config[] = [
'type' => 'video/mp4',
'label' => $quality,
'file' => $url . '&title=' . $quality,
$urlKey => $url . '&title=' . $this->filename . '-' . $quality
];
}
return json_encode($config);
}
private function fetchVideoInfo()
{
try {
$stream = fopen($this->endpoint, "r");
if(!$stream) {
throw new \Exception('Failed to open URL.');
}
$data = stream_get_contents($stream);
fclose($stream);
return $data ?: '';
} catch(\Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
private function parseFileId($url)
{
preg_match('/(?:https?:\/\/)?(?:[\w\-]+\.)*(?:drive|docs)\.google\.com\/(?:(?:folderview|open|uc)\?(?:[\w\-\%]+=[\w\-\%]*&)*id=|(?:folder|file|document|presentation)\/d\/|spreadsheet\/ccc\?(?:[\w\-\%]+=[\w\-\%]*&)*key=)([\w\-]{28,})/i', $url, $matches);
if(isset($matches[1])) {
$this->endpoint = 'https://docs.google.com/get_video_info?docid=' . $matches[1];
return true;
}
return false;
}
private function extractValue($text, $startMarker, $endMarker)
{
$startPos = stripos($text, $startMarker);
if($startPos === false) return false;
$markerLength = strlen($startMarker);
$endPos = stripos(substr($text, $startPos + $markerLength), $endMarker);
if($endPos !== false) {
$result = substr($text, $startPos + $markerLength, $endPos);
} else {
$result = substr($text, $startPos + $markerLength);
}
return $result ?: false;
}
}
And here’s my player implementation:
<?php
require __DIR__ . 'driveparser.php';
use \VideoExtractor\DriveParser;
$parser = new DriveParser;
$parser->extractUrls('https://drive.google.com/file/d/0B4EeKbDRC_36QzVNd2xnUEJfU28/view');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Video Player Test</title>
</head>
<body>
<div id='videoPlayer'></div>
<script src="https://content.jwplatform.com/libraries/cJ0z4Ufh.js"></script>
<script>
jwplayer.key='r1br2DJmUxLwrLgpi7D4IjgUHoHsDvGWHw2T7Q==';
var player = jwplayer('videoPlayer');
player.setup({
sources: <?php echo $parser->getPlayerConfig('jwplayer');?>,
width: '50%',
height: '50%',
aspectratio: '16:9',
fullscreen: true,
autostart: true
});
</script>
<pre><?php print_r($parser->getPlayerConfig('jwplayer')); ?></pre>
</body>
</html>
When I test the extracted URLs directly in browser I get a 403 error saying my client doesn’t have permission to access the videoplayback endpoint. The error mentions IP and expiration parameters in the URL. Is this a common issue with Google Drive video extraction?