Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * Get Twitch m3u8 file
- *
- * @author EchtkPvL <dev@echtkpvl.de>
- * @version 0.1.0
- * @copyright 18.02.2018 EchtkPvL
- */
- $streamer = '<streamer>';
- $clid = '<client id>'; // https://dev.twitch.tv/docs/v5#getting-a-client-id
- $opts = [
- "http" => [
- "method" => "GET",
- "header" => "Accept: application/vnd.twitchtv.v5+json\r\n".
- "Client-ID: ".$clid."\r\n"
- ]
- ];
- $context = stream_context_create($opts);
- $access_token = file_get_contents('http://api.twitch.tv/api/channels/'.$streamer.'/access_token', false, $context);
- $decoded = json_decode($access_token, true);
- $url = 'http://usher.twitch.tv/api/channel/hls/'.$streamer.'.m3u8?player=twitchweb&token='.$decoded['token'].'&sig='.$decoded['sig'].'&allow_audio_only=true&allow_source=true&type=any&p='.rand(100000,999999);
- $m3u8 = file_get_contents($url);
- $regex_video = '/#[^,]*,GROUP-ID="([^"]*)",[^,]*,[^#]*\n#[^,]*,[^,]*,RESOLUTION=([^,]*),CODECS="[^"]*",VIDEO="(\1)"\n([^\n]*)/';
- $regex_audio = '/#[^,]*,GROUP-ID="([^"]*)",[^,]*,[^#]*\n#[^,]*,[^,]*,CODECS="[^"]*",VIDEO="(\1)"\n([^\n]*)/';
- preg_match_all($regex_video, $m3u8, $array_video, PREG_SET_ORDER);
- preg_match_all($regex_audio, $m3u8, $array_audio, PREG_SET_ORDER);
- echo $array_audio[0][3].PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement