aboutsummaryrefslogtreecommitdiffstats
path: root/tools/init.php
blob: fb8e6bffedf958f49ee5189a35c44670abaf7f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

function get($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function secToDuration($seconds){
    $minutes  = intval($seconds/60);
    $seconds = ($seconds - ($minutes * 60));
    return $minutes . ' minutes ' . $seconds . ' seconds';
}

function parseStream($stream){
    $available_formats = [];
    foreach ($stream as $format) {
        parse_str($format, $format_info);
        if (isset($format_info['bitrate'])) $quality = isset($format_info['quality_label'])?$format_info['quality_label']:round($format_info['bitrate']/1000). 'k';
        else $quality = isset($format_info['quality'])?$format_info['quality']:'';

        switch ($quality){
            case 'hd720':
                $quality = "720p";
                break;
            case 'medium':
                $quality = "360p";
                break;
            case 'small':
                $quality = "240p"; // May Less
                break;
        }
        $type = explode(";", $format_info['type']);
        $type= $type[0];
        switch ($type) {
            case 'video/3gpp':
                $type = "3GP";
                break;
            case 'video/mp4':
                $type = "MP4";
                break;
            case 'video/webm':
                $type = "WEBM";
                break;
        }

        $libretype = explode(";", $format_info['type']);
        $libretype= $libretype[0];
        switch ($libretype) {
            case 'video/webm':
                $libretype = "video/webm";
                break;
        }

        $available_formats[] = [
            'itag' =>  $format_info['itag'],
            'quality' => $quality,
            'type' => $type,
            'libretype' => $libretype,
            'url' => $format_info['url'],
            'size' => getSize($format_info['url']),

        ];
    }

    return $available_formats;
}

function getSize($url)
{

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $r = curl_exec($ch);

    foreach (explode("\n", $r) as $header) {
        if (strpos($header, 'Content-Length:') === 0) {
            return intval(intval(trim(substr($header, 16)))/ (1024*1024)) . " MB";
        }
    }
}