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