Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'streamtube/player/file/setup', function( $setup, $source ){
- if( ! array_key_exists( 'mediaid' , $setup ) ){
- return $setup;
- }
- $post_id = $setup['mediaid'];
- if( ! get_post_status( $post_id ) || array_key_exists( 'tracks' , $setup ) ){
- return $setup;
- }
- $default_subtitle = strtolower(get_option( 'player_default_subtitle' ));
- $_tracks = array();
- // Get the video URL meta value
- $video_url = get_post_meta( $post_id, 'video_url', true );
- if( ! $video_url ){
- return $setup;
- }
- // Extract the YouTube ID from the video URL
- preg_match('/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/', $video_url, $matches);
- if( ! $matches || ! isset( $matches[1] ) ){
- return $setup;
- }
- $youtube_id = $matches[1];
- // Directory where subtitle files are stored
- $subtitle_dir = WP_CONTENT_DIR . '/uploads/subtitles/' . $youtube_id;
- // Check if directory exists
- $subtitle_files = array();
- if( is_dir( $subtitle_dir ) ){
- // Scan the directory for .vtt files
- $subtitle_files = glob( $subtitle_dir . '/*.vtt' );
- }
- // Add local subtitle files if not already added
- if( $subtitle_files ){
- foreach ( $subtitle_files as $subtitle_file ) {
- $file_name = basename( $subtitle_file, '.vtt' );
- $file_parts = explode('.', $file_name);
- if( count( $file_parts ) >= 2 && $file_parts[0] === $youtube_id ){
- // Extract language code before the hyphen if present
- $language = explode('-', $file_parts[1])[0];
- $source = content_url( '/uploads/subtitles/' . $youtube_id . '/' . basename( $subtitle_file ) );
- // Check if this language is already added from meta
- $language_exists = false;
- foreach( $_tracks as $track ){
- if( $track['language'] === $language ){
- $language_exists = true;
- break;
- }
- }
- if( ! $language_exists ){
- $_tracks[] = array(
- 'language' => $language,
- 'source' => $source
- );
- }
- }
- }
- }
- if( $_tracks ){
- for ( $i = 0; $i < count( $_tracks ); $i++) {
- $language = streamtube_core_get_language_by_code( strtolower( $_tracks[$i]['language'] ) );
- $track = array(
- 'kind' => 'captions',
- 'srclang' => $_tracks[$i]['language'],
- 'label' => $language ? $language['name'] : '',
- 'src' => $_tracks[$i]['source']
- );
- if( ($default_subtitle == 'first' && $i == 0) || ( $default_subtitle == strtolower( $_tracks[$i]['language'] ) ) ){
- $track['default'] = true;
- }
- /**
- * Filter the track
- */
- $track = apply_filters( 'streamtube/core/post/player/track', $track, $setup, $source );
- $setup['tracks'][] = $track;
- }
- }
- return $setup;
- }, 30, 2 );
Advertisement
Comments
-
- Merge Subtitles:
- The manually added subtitles are retrieved and stored in $_tracks.
- Dynamically detected subtitles are then added to $_tracks only if the language is not already present (i.e., not manually added).
- #Toan Code ____ to move edits to childtheme on class-streamtube-core-post.php and Vido.min.js for TextTracks and Subtitile preload.
- add_filter('streamtube/player/file/setup', function ($setup, $source) {
- if (!array_key_exists('mediaid', $setup)) {
- return $setup;
- }
- $post_id = $setup['mediaid'];
- if (!get_post_status($post_id)) {
- return $setup;
- }
- $default_subtitle = strtolower(get_option('player_default_subtitle'));
- $_tracks = array();
- // Retrieve manually added subtitles
- if (array_key_exists('tracks', $setup)) {
- foreach ($setup['tracks'] as $manual_track) {
- $_tracks[$manual_track['srclang']] = $manual_track;
- }
- }
- // Get the video URL meta value
- $video_url = get_post_meta($post_id, 'video_url', true);
- if (!$video_url) {
- return $setup;
- }
- // Extract the YouTube ID from the video URL
- preg_match('/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/', $video_url, $matches);
- if (!$matches || !isset($matches[1])) {
- return $setup;
- }
- $youtube_id = $matches[1];
- // Directory where subtitle files are stored
- $subtitle_dir = WP_CONTENT_DIR . '/uploads/subtitles/' . $youtube_id;
- // Check if directory exists
- $subtitle_files = array();
- if (is_dir($subtitle_dir)) {
- // Scan the directory for .vtt files
- $subtitle_files = glob($subtitle_dir . '/*.vtt');
- }
- // Add local subtitle files if not already added
- if ($subtitle_files) {
- foreach ($subtitle_files as $subtitle_file) {
- $file_name = basename($subtitle_file, '.vtt');
- $file_parts = explode('.', $file_name);
- if (count($file_parts) >= 2 && $file_parts[0] === $youtube_id) {
- // Extract language code before the hyphen if present
- $language = explode('-', $file_parts[1])[0];
- $source = content_url('/uploads/subtitles/' . $youtube_id . '/' . basename($subtitle_file));
- // Check if this language is already added from meta
- if (!isset($_tracks[$language])) {
- $_tracks[$language] = array(
- 'kind' => 'captions',
- 'srclang' => $language,
- 'label' => streamtube_core_get_language_by_code(strtolower($language))['name'] ?? '',
- 'src' => $source,
- );
- }
- }
- }
- }
- // Prepare the tracks for the player setup
- $setup['tracks'] = array_values($_tracks);
- // Set the default subtitle
- if ($_tracks) {
- foreach ($setup['tracks'] as &$track) {
- $language_code = strtolower($track['srclang']);
- if (($default_subtitle == 'first' && $track === reset($setup['tracks'])) || ($default_subtitle == $language_code)) {
- $track['default'] = true;
- }
- }
- }
- return $setup;
- }, 30, 2);
Add Comment
Please, Sign In to add comment
Advertisement