0
votes

I want to make an API get google drive video link to play on JW player and videojs for mysite like (api.getlinkdrive.com), but I just could not find the method to do it.

Recently, I have found some source code on github (https://github.com/marxvn/gdrive). I've modified it for my site. The code just work fine, but the link can't play (see example here: http://getlinkdrive.byethost7.com/examples/jwplayer.php).

The problem may come from the IP or maybe accessibility of the file. I just want to know if I will have to deal with IP or something like OAuth 2.0 to access to google API or not.

Here is my code: gdrive.php


namespace Marxvn;
/**
 * Google Drive library
 *
 * @author Marxvn
 * @copyright (c) 2016, Marxvn
 * @license https://spdx.org/licenses/BSD-3-Clause.html BSD-3-Clause
 */

class gdrive
{
    /**
     *
     * @var url
     */
    protected $url;

    /**
     *
     * @var title
     */
    protected $title = '';

    /**
     *
     * @var sources
     */
    public $sources;

    /**
     * 
     * @var itag
     */
    protected $itag = [
        37,
        22,
        59,
        18
    ];


    /**
     *
     * @var vidcode
     */
    protected $vidcode = [
        //2D Non-DASH
        '18'    => '360',
        '59'    => '480',
        '22'    => '720',
        '37'    => '1080',
        //3D Non-DASH
        '82'    => '360',
        '83'    => '240',
        '84'    => '720',
        '85'    => '1080'
    ];

    /**
     *
     * @param  array    $itags
     * @return void
     */

    public function setItag(array $itag)
    {
        $this->itag = $itag;
    }

    /**
     *
     * @param  array    $vidcode
     * @return void
     */

    public function setVidcode(array $vidcode)
    {
        $this->vidcode = $vidcode + $this->vidcode;
    }

    /**
     *
     * @param  array    $title
     * @return void
     */

    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     *
     * @param  string   $gurl
     * @return array
     */

    public function getLink($gurl)
    {
        $source = [];

        if( $this->getDriveId($gurl) ) {
            $body = $this->getByfopen();

            if($body && $this->getStr($body,'status=', '&') === 'ok') {

                $fmt = $this->getStr($body, 'fmt_stream_map=','&');

                $urls = explode(',', urldecode($fmt));

                foreach ($urls as $url) {
                    list($itag,$link) = explode('|', $url);
                    if(in_array($itag, $this->itag)){
                        $source[$this->vidcode[$itag]] = preg_replace("/[^\/]+\.google\.com/", "redirector.googlevideo.com",$link);
                    }
                }
            }
        }
        $this->sources = $source;

    }

    /**
     *
     * @param  string   $type
     * @return json
     */

    public function getSources($type = 'jwplayer')
    {
        $s = [];

        $url_tag = ($type == 'jwplayer') ? 'src' : 'file';

        foreach ($this->sources as $itag => $link) {
            $s[] = [
                'type'  => 'video/mp4',
                'label' => $itag,
                'file'  => $link.'&tile='.$itag,
                $url_tag => $link.'&tile='.$this->title.'-'.$itag
            ];
        }
        return json_encode($s);
    }

    /**
     *
     * @param  string   $url
     * @return string
     */
    public function getByfopen()
    {
        try {
            $handle = fopen($this->url, "r");

            if ( !$handle ) {
                throw new \Exception('Url open failed.');
            }  

            $contents = stream_get_contents($handle);
            fclose($handle);

            return $contents ? $contents : '';

        } catch( \Exception $e) {
            echo 'Message: ' .$e->getMessage();
        }
    }

    /**
     *
     * @param  string   $url
     * @return mixed
     */

    private function getDriveId($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 , $match);

        if(isset($match[1])) {
            $this->url = 'https://docs.google.com/get_video_info?docid='.$match[1];
            return true;
        }

        return false;
    }

    /**
     *
     * @param  string   $string
     * @param  string   $find_start
     * @param  string   $find_end
     * @return mixed
     */
    private function getStr($string, $find_start, $find_end)
    {
        $start = stripos($string, $find_start);

        if($start === false) return false;

        $length = strlen($find_start);

        $end = stripos(substr($string, $start+$length), $find_end);

        if($end !== false) {
            $rs = substr($string, $start+$length, $end);
        } else {
            $rs = substr($string, $start+$length);
        }

        return $rs ? $rs : false;
    }
}

Here is the code for the player: jwplayer.php

<?php
require __DIR__ . 'gdrive.php';
use \Marxvn\gdrive;

$gdrive = new gdrive;
$gdrive->getLink('https://drive.google.com/file/d/0B4EeKbDRC_36QzVNd2xnUEJfU28/view');

?>

<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Jwplayer</title>
</head>
<body>
    <div id='player'></div>

<script type="text/javascript" src="http://djrocker-anime.com/jwplayer/jwplayer.js"></script>
<script src="https://content.jwplatform.com/libraries/cJ0z4Ufh.js"></script>
<script type='text/javascript'>
    jwplayer.key='r1br2DJmUxLwrLgpi7D4IjgUHoHsDvGWHw2T7Q==';
    var playerInstance = jwplayer(player);
    playerInstance.setup({
        sources: <?php echo $gdrive->getSources('jwplayer');?>,
        width: '50%',
        height: '50%',
        aspectratio: '16:9',
        fullscreen: 'true', 
        autostart: 'true',  
    });
</script>
</body>
</html>

<pre>
    <?php print_r($gdrive->getSources('jwplayer')); ?>
</pre>

When I copy the direct link to the browser it shows

403. That’s an error.

Your client does not have permission to get URL /videoplayback?id=2f1b23544d05d291&itag=18&source=webdrive&requiressl=yes&ttl=transient&mm=30&mn=sn-aigllner&ms=nxu&mv=u&pl=22&ei=fSn2WMKwOpTmqgWLsS8&mime=video\/mp4&lmt=1428053688993343&mt=1492527374&ip=185.27.134.64&ipbits=0&expire=1492541885&cp=QVJOVEZfUFhWRlhNOnNmWVVyQ3NPR2Fl&sparams=ip%2Cipbits%2Cexpire%2Cid%2Citag%2Csource%2Crequiressl%2Cttl%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cei%2Cmime%2Clmt%2Ccp&signature=B682DF0E2471F5AB76BF14B2E4CF0C48CDF247BA.62EFECACDDA2D0B2374CC885C2F5804A5976E340&key=ck2&app=explorer&driveid=0B4EeKbDRC_36QzVNd2xnUEJfU28 from this server
1

1 Answers

0
votes

Error 403 means you don't have permission to access the file you're calling. What you can do is use the export request from Drive API with a demo from this SO thread

Use this link to stream on the browser or download:

https://drive.google.com/uc?export=download&id=ID_OF_YOUR_DRIVE_VIDEO

Up to you to implement in PHP.