Advertisement
phpface

Untitled

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