F
F
FFCC2021-02-08 02:58:49
Video
FFCC, 2021-02-08 02:58:49

How to get video duration in WordPress?

I noticed that the latest build of WordPress 5.6 somehow recognizes the duration of the video.
On one of my projects, the duration of the video is set manually through additional fields.
I would like to understand which function of the WordPress media editor is responsible for displaying duration and how to get this value.

Well, a practical question - how to display the duration value in an additional field so that the content manager does not enter it manually, or how to display this value immediately in microdata?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
m0ze, 2021-02-08
@FFCC

It's about wp_read_video_metadata(), I guess. Works initially in the admin panel, so to display on the front of the site you need to use

require_once( ABSPATH . 'wp-admin/includes/media.php' );
in the page template, and then pull the information by the ID of the uploaded file.
Well, a practical question - how to display the duration value in an additional field so that the content manager does not enter it manually, or how to display this value immediately in microdata?

If I understood the question correctly, then like this (just for example):
<?php
  require_once( ABSPATH . 'wp-admin/includes/media.php' );
  $videoattachmentid = get_attached_file( X );
  $videometadata = wp_read_video_metadata( $videoattachmentid );
  print "Длительность видео: " . $videometadata['length_formatted'] . " секунд";
?>

Available data: lossless, bitrate, bitrate_mode, filesize, mime_type, length, length_formatted, width, height, fileformat, dataformat, encoder, codec, audio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question