video_file.auto_cache.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. class video_file_auto_cache extends auto_cache
  3. {
  4. private $key = "video:file:";
  5. public function load($video)
  6. {
  7. $this->key .= $video['id'];
  8. $key_bf = $this->key . '_bf';
  9. $play_info = $GLOBALS['cache']->get($this->key, true);
  10. if ($play_info === false) {
  11. $is_ok = $GLOBALS['cache']->set_lock($this->key);
  12. if (!$is_ok) {
  13. return $GLOBALS['cache']->get($key_bf, true);
  14. }
  15. fanwe_require(APP_ROOT_PATH . 'mapi/lib/core/video_factory.php');
  16. $video_factory = new VideoFactory();
  17. if ($video['video_type'] > 0 && $video['channelid'] && strpos($video['channelid'],'_')) {
  18. $root = c_get_vodset_by_video_id($video['id']);
  19. if(isset($root['vodset'])) {
  20. $play_list = array();
  21. $vodset = $root['vodset'];
  22. $urls = array();
  23. foreach ($vodset as $k => $v) {
  24. $playSet = $v['fileSet'];
  25. for ($i = sizeof($playSet) - 1; $i >= 0; $i--) {
  26. $play_list[] = $playSet[$i]['fileId'];
  27. $urls[$playSet[$i]['playSet'][$i]['definition']] = $playSet[$i]['playSet'][$i]['url'];
  28. }
  29. }
  30. ksort($urls);
  31. $play_info = array(
  32. 'file_id' => $play_list[0],
  33. 'urls' => $urls,
  34. 'play_url' => array_shift($urls),
  35. );
  36. }
  37. } else {
  38. $fileName = $video['id'] . '_' . to_date($video['begin_time'], 'Y-m-d-H');
  39. if($video['video_type'] == 1 && !empty($video['channelid'])){
  40. $fileName = 'live'.$video['id'] . '_' . to_date($video['begin_time'], 'Y-m-d-H');
  41. }
  42. $file_info = $video_factory->DescribeVodPlayInfo($fileName);
  43. if ($file_info['totalCount'] > 0) {
  44. if (count($file_info['fileSet'][0]['playSet']) == 1 && $file_info['fileSet'][0]['playSet'][0]['definition'] == 0) {
  45. $file_info['fileSet'][0]['playSet'][0]['definition'] = 20;
  46. }
  47. $urls = array();
  48. foreach ($file_info['fileSet'][0]['playSet'] as $play) {
  49. $urls[$play['definition']] = $play['url'];
  50. }
  51. ksort($urls);
  52. $play_info = array(
  53. 'file_id' => $file_info['fileSet'][0]['fileId'],
  54. 'urls' => $urls,
  55. 'play_url' => array_shift($urls),
  56. );
  57. }
  58. }
  59. //兼容再次查找视频
  60. if($play_info['play_url']==''){
  61. $channel_info = $video_factory->GetVodRecordFiles($video['channelid'], $video['create_time']);
  62. if ($channel_info['totalCount'] > 0) {
  63. if (empty($channel_info['urls'])) {
  64. $file_id = $channel_info['filesInfo'][0]['fileId'];
  65. $file_info = $video_factory->DescribeVodPlayUrls($file_id);
  66. $urls = $file_info['urls'];
  67. } else {
  68. $file_id = "";
  69. $urls = $channel_info['urls'];
  70. }
  71. ksort($urls);
  72. $play_info = array(
  73. 'file_id' => $file_id,
  74. 'urls' => $urls,
  75. 'play_url' => array_shift($urls),
  76. );
  77. }
  78. }
  79. if (!empty($play_info['urls'])) {
  80. $GLOBALS['cache']->set($this->key, $play_info, 3600 * 12, true);
  81. $GLOBALS['cache']->set($key_bf, $play_info, 86400, true);//备份
  82. }
  83. }
  84. return $play_info;
  85. }
  86. public function rm()
  87. {
  88. $GLOBALS['cache']->clear_by_name($this->key);
  89. }
  90. public function clear_all()
  91. {
  92. $GLOBALS['cache']->clear_by_name($this->key);
  93. }
  94. }