<?php


$url = "type url on ?url=xasdasdasd";
$title = "Movie";
$thumbnail = "https://localhost/1.jpg";
$type = "video/mp4";
#$type = "application/vnd.apple.mpegurl";
#$type = "application/x-mpegURL";
$live = false;


if (!isset($_GET['url']) OR $_GET['url'] == "" ){

header('Content-Type: text/html; charset-UTF-8');

?>
<html>
<head>

</head>
<body>
<div>
  <form action="generator.json" method="get" autocomplete="on">
   <p>url: <br><textarea rows="4" cols="80" placeholder="ссылка на папку" name="url" ></textarea></p>
   <!--ea rows="3" cols="80" placeholder="может быть пустым" name="title" ></textarea></p>
   <p>thumbnail: <br><textarea rows="3" cols="80" placeholder="может быть пустым" name="thumbnail" ></textarea></p>
   -->
   <!--<p>content-type: <br><textarea rows="3" cols="80" placeholder="<?=$type?>" name="content" ></textarea></p>-->


    <p>content-type: <br>
      <label>
        <input type="radio" name="content" value="video/mp4" checked>
        video/mp4
      </label>

      <label>
        <input type="radio" name="content" value="application/vnd.apple.mpegurl">
        application/vnd.apple.mpegurl
      </label>

      <label>
        <input type="radio" name="content" value="application/vnd.apple.mpegurl">
        application/x-mpegURL (m3u8)
      </label>
    </p>

    <p>live: <br>
      <label>
        <input type="radio" name="live" value="false" checked>
        false
      </label>

      <label>
        <input type="radio" name="live" value="true">
        true
      </label>

    </p>

   <p>subs: <br><textarea rows="3" cols="80" placeholder="может быть пустым" name="sub" ></textarea></p>
   <p><input type="submit" /></p>
  </form>
</div>
FisHlaBsoMAN 2021(c)
</body>
</html>
<?php
  exit;
}



if (isset($_GET['title']) && $_GET['title'])
    $title = $_GET['title'];

if (isset($_GET['thumbnail']) && $_GET['thumbnail'])
    $thumbnail = $_GET['thumbnail'];
    #$json['sources'][0]['title'] = $_GET['title'];

if (isset($_GET['url']) && $_GET['url'])
    $url = $_GET['url'];

if (isset($_GET['content']) && $_GET['content'])
    $type = $_GET['content'];


if (isset($_GET['live']) && $_GET['live'])
    $live = $_GET['live'];

$urls = explode("\n", $url);


$sub_template = "";

if(isset($_GET['sub']) && $_GET['sub']){
  $sub_url = $_GET['sub'];
  $sub_template = <<<SUB
  ,

    "textTracks": [

      {

        "url": "$sub_url",

        "contentType": "text/vtt",

        "name": "subs"

      }
    ]
  SUB;
}

$sources = "";
$first = "";
$i = 0;
$q = [360,240,480,720,1080];
foreach ($urls as $key => $value) {
  $value = trim($value);
  $quality = $q[$i];
  $source_template = <<<SOURCES
      $first
      {

        "url": "$value",

        "contentType": "$type",
            "quality": $quality,
            "bitrate": 5000
      }
  SOURCES;

  $sources.=$source_template;
  $first = ",";
  $i++;
}




$template = <<<JSON
{

  "title": "$title",

  "duration": 1000,

  "live": $live,

  "thumbnail": "$thumbnail",

  "sources": [

    $sources
  ]
  $sub_template

}
JSON;

header('Content-Type: application/json');
echo $template;
