Advertisement
JSW3722

Untitled

Jun 29th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #Toan Code ____ to move edits to childtheme on class-streamtube-core-post.php and Vido.min.js for TextTracks and Subtitile preload.
  2. add_filter('streamtube/player/file/setup', function ($setup, $source) {
  3.  
  4. if (!array_key_exists('mediaid', $setup)) {
  5. return $setup;
  6. }
  7.  
  8. $post_id = $setup['mediaid'];
  9.  
  10. if (!get_post_status($post_id)) {
  11. return $setup;
  12. }
  13.  
  14. $default_subtitle = strtolower(get_option('player_default_subtitle'));
  15.  
  16. $_tracks = array();
  17.  
  18. // Retrieve manually added subtitles
  19. if (array_key_exists('tracks', $setup)) {
  20. foreach ($setup['tracks'] as $manual_track) {
  21. $_tracks[$manual_track['srclang']] = $manual_track;
  22. }
  23. }
  24.  
  25. // Get the video URL meta value
  26. $video_url = get_post_meta($post_id, 'video_url', true);
  27.  
  28. if (!$video_url) {
  29. return $setup;
  30. }
  31.  
  32. // Extract the YouTube ID from the video URL
  33. preg_match('/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/', $video_url, $matches);
  34.  
  35. if (!$matches || !isset($matches[1])) {
  36. return $setup;
  37. }
  38.  
  39. $youtube_id = $matches[1];
  40.  
  41. // Directory where subtitle files are stored
  42. $subtitle_dir = WP_CONTENT_DIR . '/uploads/subtitles/' . $youtube_id;
  43.  
  44. // Check if directory exists
  45. $subtitle_files = array();
  46. if (is_dir($subtitle_dir)) {
  47. // Scan the directory for .vtt files
  48. $subtitle_files = glob($subtitle_dir . '/*.vtt');
  49. }
  50.  
  51. // Add local subtitle files if not already added
  52. if ($subtitle_files) {
  53. foreach ($subtitle_files as $subtitle_file) {
  54. $file_name = basename($subtitle_file, '.vtt');
  55. $file_parts = explode('.', $file_name);
  56.  
  57. if (count($file_parts) >= 2 && $file_parts[0] === $youtube_id) {
  58. // Extract language code before the hyphen if present
  59. $language = explode('-', $file_parts[1])[0];
  60. $source = content_url('/uploads/subtitles/' . $youtube_id . '/' . basename($subtitle_file));
  61.  
  62. // Check if this language is already added from meta
  63. if (!isset($_tracks[$language])) {
  64. $_tracks[$language] = array(
  65. 'kind' => 'captions',
  66. 'srclang' => $language,
  67. 'label' => streamtube_core_get_language_by_code(strtolower($language))['name'] ?? '',
  68. 'src' => $source,
  69. );
  70. }
  71. }
  72. }
  73. }
  74.  
  75. // Prepare the tracks for the player setup
  76. $setup['tracks'] = array_values($_tracks);
  77.  
  78. // Set the default subtitle
  79. if ($_tracks) {
  80. foreach ($setup['tracks'] as &$track) {
  81. $language_code = strtolower($track['srclang']);
  82. if (($default_subtitle == 'first' && $track === reset($setup['tracks'])) || ($default_subtitle == $language_code)) {
  83. $track['default'] = true;
  84. }
  85. }
  86. }
  87.  
  88. return $setup;
  89. }, 30, 2);
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement