$i["height"], "id" => $i['format_id'] ]; }, $data["formats"]), function ($i) { return !is_null($i["quality"]); })); $hd = array_values(array_filter($qualities, function ($i) { return $i["quality"] >= 720; })); $selected = count($hd) > 0 ? $hd[0] : $qualities[count($qualities) - 1]; $stream = array_values(array_filter($data["formats"], function ($i) use ($selected) { return $i["height"] === $selected["quality"] && $i["acodec"] !== null && $i["acodec"] !== "none"; }))[0]; echo(json_encode([ "title" => $data["fulltitle"], "author" => $data["channel"], "duration" => $data["duration"], "stream" => $selected, "url" => $stream["url"], "duration_pretty" => $data["duration_string"], "poster" => $data["thumbnail"], ], JSON_PRETTY_PRINT)); }