video.class.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Vioscope;
  3. if ( ! class_exists( '\Vioscope\Video' ) ) :
  4. class Video {
  5. // YT video id
  6. public $id;
  7. // Raw json parsed data
  8. public $data;
  9. // Processed data
  10. public $info;
  11. // Video formats available
  12. public $formats;
  13. function __construct( $video_id = null ) {
  14. if ( $video_id ) {
  15. $this->set_video_id( $video_id );
  16. }
  17. }
  18. public function set_video_id( $video_id = null ) {
  19. $this->id = $video_id;
  20. // Clear previous data (if any)
  21. unset($this->data);
  22. unset($this->info);
  23. unset($this->formats);
  24. // Update data
  25. $this->fetch_data($this-id);
  26. }
  27. public function fetch_data( $video_id = null ) {
  28. if ( ! $video_id ) {
  29. $video_id = $this->id;
  30. }
  31. parse_str( file_get_contents( "https://youtube.com/get_video_info?html5=1&gl=US&hl=en&video_id={$this->id}" ), $info );
  32. $this->data = json_decode( $info['player_response'], true );
  33. // Other info
  34. $this->info['id'] = $this->id;
  35. $this->info['title'] = $this->data['videoDetails']['title'];
  36. $this->info['length'] = $this->data['videoDetails']['lengthSeconds'];
  37. $this->info['keywords'] = $this->data['videoDetails']['keywords'];
  38. $this->info['channelId'] = $this->data['videoDetails']['channelId'];
  39. $this->info['channel_url'] = get_local_url( $this->data['videoDetails']['channelId'], 'channel' );
  40. $this->info['short_description'] = urlify_string( replace_urls_with_local_urls( $this->data['videoDetails']['shortDescription'] ) );
  41. $this->info['thumbnails'] = $this->data['videoDetails']['thumbnail']['thumbnails'];
  42. $this->info['averageRating'] = $this->data['videoDetails']['averageRating'];
  43. $this->info['views'] = $this->data['videoDetails']['viewCount'];
  44. $this->info['author'] = $this->data['videoDetails']['author'];
  45. $this->info['isPrivate'] = ( $this->data['videoDetails']['isPrivate'] == '' ) ? false : true;
  46. $this->info['isLive'] = $this->data['videoDetails']['isLiveContent'];
  47. $this->info['isUnlisted'] = ( $this->data['videoDetails']['isUnlisted'] == '' ) ? false : true;
  48. $this->info['category'] = $this->data['microformat']['playerMicroformatRenderer']['category'];
  49. $this->info['publish_date'] = $this->data['microformat']['playerMicroformatRenderer']['publishDate'];
  50. $this->info['publish_date_friendly'] = time_ago( $this->data['microformat']['playerMicroformatRenderer']['publishDate'] );
  51. // useful...??
  52. $this->info['ownerChannelName'] = $this->data['microformat']['playerMicroformatRenderer']['ownerChannelName'];
  53. $this->info['uploadDate'] = $this->data['microformat']['playerMicroformatRenderer']['uploadDate'];
  54. // Video formats available
  55. $this->info['formats'] = $this->get_formats();
  56. }
  57. private function get_formats() {
  58. if ( ! isset( $this->data ) ) return;
  59. $formats = array();
  60. foreach( array_merge( $this->data['streamingData']['formats'], $this->data['streamingData']['adaptiveFormats'] ) as $format ) {
  61. $formats[] = array(
  62. 'url' => $format['url'],
  63. 'mimetype' => $format['mimeType'],
  64. 'mimetype_type' => explode( ";", $format['mimeType'] )[0],
  65. 'bitrate' => $format['bitrate'],
  66. 'width' => $format['width'],
  67. 'height' => $format['height'],
  68. 'is_audio_only' => ( strpos( $format['mimeType'], 'audio/' ) ==! false && ! empty( $format['audioChannels'] ) ) ? true : false,
  69. 'is_video_only' => ( strpos( $format['mimeType'], 'video/' ) ==! false && empty( $format['audioChannels'] ) ) ? true : false,
  70. 'bitrate' => $format['bitrate'],
  71. 'quality' => $format['quality'],
  72. 'fps' => $format['fps'],
  73. 'quality_label' => $format['qualityLabel'],
  74. 'approx_duration_ms' => $format['approxDurationMs'],
  75. 'audio_sample_rate' => $format['audioSampleRate'],
  76. 'audio_channels' => $format['audioChannels'],
  77. );
  78. }
  79. return $formats;
  80. }
  81. }
  82. endif;