summaryrefslogtreecommitdiff
path: root/iplayer.php
blob: 8522e28d5f13aac2118a0ee1e05f9d1279a2fabc (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
<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">
<title>BBC iPlayer video stream extractor</title>

</head>

<body>

<?php
define("CURLIFACE", "192.168.122.9");

// Grab the programme ID with GET, or with POST, or present a URL form
if (isset($_GET['id'])) {
  $id = $_GET['id'];
} else if (isset($_POST['url'])) {
  $idpos = strpos($_POST['url'], "/episode/") + 9;
  $idlen = strpos($_POST['url'], "/", $idpos) - $idpos;
  if ($idlen < 1) {
    $idlen = strlen($_POST['url']) - $idpos;
  }
  $id = substr($_POST['url'], $idpos, $idlen);
} else {
?>
<form action="./" method="post">
<p>Enter a non-live BBC iPlayer URL:<br>
e.g. http://www.bbc.co.uk/iplayer/episode/b06h9y66/<br>
<input name="url" id="url" size="40" type="text">
<input type="submit"><br>
<br>
You can also use ?id= to specify a programme ID directly to bypass this form<br>
e.g. <a href="http://iplayer.of.je/?id=b06h9y66">http://iplayer.of.je/?id=b06h9y66</a><br>
<br>
You can open the resulting file in <a href="https://www.videolan.org/">VLC</a>, or your preferred player.<br>
Alternatively, use the direct URL directly in your player.<br>
<br>
If you get a blank page, please try again as the request occasionally fails.</p>
</form>

<script type="text/javascript">
  document.getElementById("url").focus();
  document.getElementById("url").select();
</script>

<p><small><a href="./iplayer.phps">Source code?</a></small></p>

</body>

</html>
<?php
  exit();
}

// Grab iPlayer HTML page
$html = file_get_contents("http://www.bbc.co.uk/iplayer/episode/$id/");

// Extract the VPID from the page
$vpidpos = strpos($html, 'vpid":"') + 7;
$vpidlen = strpos($html, '"', $vpidpos) - $vpidpos;
$vpid = substr($html, $vpidpos, $vpidlen);

// Grab the JSON which appears to be within some JavaScript
$curl = curl_init();
curl_setopt($curl, CURLOPT_INTERFACE, CURLIFACE);
curl_setopt($curl, CURLOPT_URL, "http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/vpid/$vpid/format/json/mediaset/apple-ipad-hls/jsfunc/JS_callbacks39");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$notjson = curl_exec($curl);
curl_close($curl);

// Extract the actual JSON inside the JavaScript
$jsonpos = strpos($notjson, '{');
$json = json_decode(substr($notjson, $jsonpos, strlen($notjson) - $jsonpos - 3), true);

// Find an akamai_hls_open stream and HTTP redirect to it
foreach ($json['media']['0']['connection'] as $connection) {
  if ($connection['supplier'] == "akamai_hls_open") {
    header("Location: " . $connection['href']);
    exit();
  }
}
?>