• Resolved dreyg9

    (@dreyg9)


    Hello!

    Plugin is very good!

    Is it possible to know the length of the video and display next to the date of publication, term, author?

    Thank you

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author tubegtld

    (@tubegtld)

    Alright, long story short, the YouTube API doesn’t return duration during the “normal” API calls the plugin makes. This saves on API usage points but has the downside of not having duration.

    This has been something that’s been “on the drawing board” to add to the plugin for some time.

    I got things started a bit in light of this request. It’s not “integrated” into the plugin (yet) but the code below is tested and working over here.

    You can drop it into a child theme functions.php or make a simple plugin out of it.

    The code will check to see if you’re adding / updating a post with a YouTube video, and if so, attempt to update the duration if it’s not been previously set.

    The duration is stored in two custom fields:

    • tube_video_duration_hms is an array like this… array( ‘h’ => 0, ‘m’ => 3, ‘s’ => 47 )
    • tube_video_duration_seconds is the total duration in seconds.

    Give it a shot, and please mark resolved if it’s working for you. And if you dig the plugin, please consider leaving a review.

    
    add_action('added_post_meta', 'tube_maybe_get_youtube_duration', 10, 4);
    add_action('updated_post_meta', 'tube_maybe_get_youtube_duration', 10, 4);
    
    function tube_maybe_get_youtube_duration( $meta_id, $post_id, $meta_key='', $meta_value='' ){
           
      // Do nothing unless correct key
      if ( $meta_key != 'tube_video_site' ) return;
      
      // only applied to youtube
      if ( $meta_value != 'youtube' ) return;
    
      // make sure it doesn't have duration already
      $has_duration = get_post_meta( $post_id, 'tube_video_duration_hms', true );
        
      if ( $has_duration ) return;
      
      tube_get_youtube_duration( $post_id );
    
    }
    
    function tube_get_youtube_duration( $post_id ){
      
      // get the video ID
      $video_id = get_post_meta( $post_id, 'tube_video_id', true );
      
      if ( ! $video_id ) return;
      
      // get the API key
      global $tube_video_curator;
      $youtube_api_key = $tube_video_curator :: $tube_youtube_videos -> get_youtube_api_key();
      
      if ( ! $youtube_api_key ) return;
      
      // generate the endpoint
      $endpoint = add_query_arg(
        array( 
          'part' => 'contentDetails', 
          'id' => $video_id, 
          'key' => $youtube_api_key
         ),
        'https://www.googleapis.com/youtube/v3/videos'
      );
      
      // get the raw results
      $results_json = file_get_contents( $endpoint );
      
      // decode the results
      $results = json_decode( $results_json, true );
      
      // extract the duration
      $duration = $results['items'][0]['contentDetails']['duration'];
      
      // get the duraton parts  
      preg_match_all( '/PT(\d+H)?(\d+M)?(\d+S)?/', $duration, $duration_parts );
      
      // create HMS array
      $time_hms = array( 
        'h' => (int) str_replace( 'H', '', $duration_parts[1][0] ), 
        'm' => (int) str_replace( 'M', '', $duration_parts[2][0] ), 
        's' => (int) str_replace( 'S', '', $duration_parts[3][0] ) 
      );
      
      // calculate total seconds
      $time_total_seconds = ( $time_hms['h'] * 3600 ) + ( $time_hms['m'] * 60 ) + $time_hms['s'];
      
      // update the post meta
      add_post_meta( $post_id, 'tube_video_duration_hms', $time_hms );
      
      add_post_meta( $post_id, 'tube_video_duration_seconds', $time_total_seconds );  
      
      // all done
      return;
      
    }
    
    • This reply was modified 8 years, 9 months ago by tubegtld.
    • This reply was modified 8 years, 9 months ago by tubegtld.
    Thread Starter dreyg9

    (@dreyg9)

    Thank you!!!

    but

    tube_video_duration_seconds – stored
    tube_video_duration_hms – not stored

    • This reply was modified 8 years, 9 months ago by dreyg9.
    • This reply was modified 8 years, 9 months ago by dreyg9.
    Plugin Author tubegtld

    (@tubegtld)

    Are you actually looking in the database?

    Or are you just looking at the Custom Fields meta box on the Edit Post page?

    Assuming metabox, you will NOT see the tube_video_duration_hms there because it’s stored as a serialized array.

    Try looking in your postmeta database table, or do a get_post_meta( $post_id, 'tube_video_duration_hms', true ) somewhere on the front end and you should see the hms value.

    If it’s truly not being saved (unlikely) please provide a link to the video on YouTube.

    Thread Starter dreyg9

    (@dreyg9)

    tube_video_duration_seconds – display numbers (seconds)
    tube_video_duration_hms – display “Array”

    screenshot: http://imagestun.com/hosting/kartinki/array.jpg

    video https://www.youtube.com/watch?v=ySNM2GCq_BE
    (but this happens to all videos)

    • This reply was modified 8 years, 9 months ago by dreyg9.
    • This reply was modified 8 years, 9 months ago by dreyg9.
    Plugin Author tubegtld

    (@tubegtld)

    This is “correct” in that the data IS stored as an array.

    I think the issue with the way it’s being displayed.

    The tube_video_duration_hms comes back as an array like this…

    array( 
      'h' => 0, 
      'm' => 3, 
      's' => 47 
    )

    …so you’ll need to do something with it before you display it.

    Simple example (not tested)…

    
    $hms = get_post_meta( $post_id, 'tube_video_duration_hms', true );
    
    echo sprintf( 
      'Duration: %1$d Hours %2$d Minutes %3$d Seconds', 
      $hms['h'], 
      $hms['m'], 
      $hms['s'] 
    );
    

    Of course you may want to get fancy with plural / singular or display it some other way but that should get you started.

    Thread Starter dreyg9

    (@dreyg9)

    Ok. Now

    Duration: 0 Hours 0 Minutes 0 Seconds

    Plugin Author tubegtld

    (@tubegtld)

    Can you try this…

    $hms = get_post_meta( $post_id, 'tube_video_duration_hms', true );
    
    var_dump($hms);

    Also, make sure the $post_id variable is properly set.

    Depending on your theme, you may be able to use get_the_ID() there instead.

    • This reply was modified 8 years, 9 months ago by tubegtld.
    Thread Starter dreyg9

    (@dreyg9)

    Well,

    <?php echo 
    	$hms = get_post_meta( $post->ID, 'tube_video_duration_hms', true );
    
    echo sprintf( 
      'Duration: %1$d Hours %2$d Minutes %3$d Seconds', 
      $hms['h'], 
      $hms['m'], 
      $hms['s'] 
    );
    
    ?>

    this code is work, but I have little unpleasant problem

    ArrayDuration: 0 Hours 6 Minutes 20 Seconds
    screenshot: http://funkyimg.com/i/2xiMv.jpg

    How to remove “Array”?

    • This reply was modified 8 years, 9 months ago by dreyg9.
    Thread Starter dreyg9

    (@dreyg9)

    Correct code:

    <?php  
    	$hms = get_post_meta( $post->ID, 'tube_video_duration_hms', true );
    
     echo sprintf( 
      'Duration: %1$d Hours %2$d Minutes %3$d Seconds', 
      $hms['h'], 
      $hms['m'], 
      $hms['s'] 
    );
    
    ?>

    Thank you very much!

    Best plugin πŸ™‚ Best support πŸ™‚

    Plugin Author tubegtld

    (@tubegtld)

    Glad you got it working!

    Gonna go ahead and mark this resolved.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Video length’ is closed to new replies.