BBC iPlayer video download

This script allows to save videos from BBC iPlayer.

目前為 2015-07-23 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name BBC iPlayer video download
  3. // @namespace http://andrealazzarotto.com/
  4. // @include http://www.bbc.co.uk/iplayer/episode/*
  5. // @include http://www.bbc.co.uk/programmes/*
  6. // @version 2.1
  7. // @description This script allows to save videos from BBC iPlayer.
  8. // @copyright 2015+, Andrea Lazzarotto - GPLv3 License
  9. // @require http://code.jquery.com/jquery-latest.min.js
  10. // @grant GM_xmlhttpRequest
  11. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  12. // ==/UserScript==
  13.  
  14. get_title = function() {
  15. var title = $('meta[property="og:title"]').attr('content') || 'output';
  16. return title.replace(/\W+/g, '_');
  17. }
  18.  
  19. appendURL = function(element, url) {
  20. element.after('<div id="direct-link"></div>');
  21. $('#direct-link').css({
  22. 'padding': '.75em',
  23. 'margin': '25px auto',
  24. 'width': $('#player-outer-outer').width(),
  25. 'border': '1px solid #444',
  26. 'color': 'white',
  27. 'font-family': 'sans-serif',
  28. 'box-sizing': 'border-box'
  29. }).append('<p>To record the video, use <code>avconv</code> with the following command line:</p>' +
  30. '<pre>avconv -i "' + url + '" -codec copy -qscale 0 ' + get_title() + '.mp4</pre>' +
  31. '<p>Alternatively, you may also try to record the M3U8 stream URL with VLC.</p>');
  32. $('#direct-link pre, #direct-link code').css({
  33. 'white-space': 'normal',
  34. 'word-break': 'break-word',
  35. 'font-size': $('#direct-link p').css('font-size'),
  36. 'margin': '.75em 0',
  37. 'padding': '.75em',
  38. 'background-color': '#444'
  39. });
  40. $('#direct-link code').css('padding','.25em');
  41. $('#direct-link p:last-child').css('margin-bottom', '0');
  42. }
  43.  
  44. get_biggest = function(dict) {
  45. var s = 0;
  46. var o = null;
  47. for(var key in dict) {
  48. key = parseInt(key) || 0;
  49. if (key > s) {
  50. s = key;
  51. o = dict[key];
  52. }
  53. }
  54. return {'size': s, 'object': o};
  55. }
  56.  
  57. handle_pid = function(vpid, selector){
  58. var config_url = 'http://www.bbc.co.uk/iplayer/config/windows-phone';
  59. // figure out the mediaselector URL
  60. $.getJSON(config_url, function(data) {
  61. var selector_mobile = data['mediaselector'].replace('{vpid}', vpid);
  62. var selector_pc = selector_mobile.replace(/mobile-.*vpid/, 'pc/vpid');
  63. // get mobile data
  64. GM_xmlhttpRequest({
  65. method: 'GET',
  66. url: selector_mobile,
  67. onload: function(responseDetails) {
  68. var r = responseDetails.responseText;
  69. var doc = $.parseXML(r);
  70. var $xml = $(doc);
  71. var media = {};
  72. $xml.find('media[kind^="video"]').each(function() {
  73. var bitrate = $(this).attr('bitrate');
  74. var href = $(this).find('connection').attr('href');
  75. media[bitrate] = href;
  76. });
  77. var m3u8_url = get_biggest(media);
  78. console.log("M3U8_URL: " + m3u8_url['object']);
  79. // get desktop data for higher quality
  80. GM_xmlhttpRequest({
  81. method: 'GET',
  82. url: selector_pc,
  83. onload: function(responseDetails) {
  84. var r = responseDetails.responseText;
  85. var doc = $.parseXML(r);
  86. var $xml = $(doc);
  87. var media = {};
  88. $xml.find('media[kind^="video"]').each(function() {
  89. var bitrate = $(this).attr('bitrate');
  90. var identifier = $(this).find('connection[application="ondemand"], ' +
  91. 'connection[application*="/e3"]').attr('identifier');
  92. if(identifier)
  93. media[bitrate] = identifier;
  94. });
  95. var high_quality = get_biggest(media);
  96. console.log("HIGH_QUALITY: " + high_quality['object']);
  97. // compose the M3U8 stream URL
  98. GM_xmlhttpRequest({
  99. method: 'GET',
  100. url: m3u8_url['object'],
  101. onload: function(responseDetails) {
  102. var r = responseDetails.responseText;
  103. var urls = r.split('\n').slice(1);
  104. var final_url = urls[1];
  105. // fix the final url
  106. var old_pieces = final_url.split(',');
  107. var pieces = [old_pieces[0], old_pieces[1], old_pieces[old_pieces.length-1]];
  108. var p = 1;
  109. var high_quality_piece = high_quality['object'].replace('.mp4','')
  110. .replace('secure_auth/','').split('/').slice(1).join('/');
  111. var new_p = pieces[p].split('/').slice(0,2).join('/') + '/' + high_quality_piece;
  112. pieces[p] = new_p;
  113. final_url = pieces.join(',');
  114. // output the M3U8 URL
  115. appendURL($(selector), final_url);
  116. }
  117. });
  118. }
  119. });
  120. }
  121. });
  122. }); // getJSON
  123. }
  124.  
  125. $(document).ready(function(){
  126. var isProgramme = !!unsafeWindow.bbcProgrammes;
  127. if (isProgramme) {
  128. var clipid = location.href.split("/")[4];
  129. $.getJSON('http://www.bbc.co.uk/programmes/' + clipid + '/playlist.json', function(data) {
  130. var vpid = data.defaultAvailableVersion.pid;
  131. console.log("VPID: " + vpid.toString());
  132. handle_pid(vpid, '.island .cf.component');
  133. });
  134. }
  135. else {
  136. var spid = $('script:contains("mediator.bind")').html();
  137. var vpid = spid.split('vpid')[1].split('"')[2];
  138. handle_pid(vpid, '#player-outer-outer');
  139. }
  140. }); // $(document).ready.ready