wp_read_audio_metadata()
Gets all data about an audio file from its ID3 tags. This includes all available data: format, channels, bitrate, size, MIME type, duration, year, album, artist, comment, picture, etc.
The function is not defined on the public side of the site (front-end). For it to work there, you need to include the file:
if( ! is_admin() ){
require_once ABSPATH . 'wp-admin/includes/media.php';
}
1 time — 0.014777 sec (extremely slow) | 50000 times — 1538.81 sec (extremely slow) | PHP 7.0.5, WP 4.5
Hooks from the function
Returns
Array|false. Array of file data, if it is found.
Usage
wp_read_audio_metadata( $file );
- $file(string) (required)
- Path to the file. You must specify the server absolute path, not a URL.
Examples
#1 Get all the data of the audio file by it's file path
if( ! function_exists('wp_read_audio_metadata') ){
require_once ABSPATH . 'wp-admin/includes/media.php';
}
// the path to the file
$file = wp_get_upload_dir()['basedir'] . '/2016/04/Happy_Birthday.mp3';
$metadata = wp_read_audio_metadata( $file );
print_r( $metadata );
/* Print:
Array
(
[dataformat] => mp3
[channels] => 2
[sample_rate] => 44100
[bitrate] => 320000
[channelmode] => stereo
[bitrate_mode] => cbr
[lossless] =>
[encoder_options] => CBR320
[compression_ratio] => 0.226757369615
[fileformat] => mp3
[filesize] => 342016
[mime_type] => audio/mpeg
[length] => 8
[length_formatted] => 0:08
[text] => Elsynor Elsy
[artist] => Gregory House
[album] => Original from TVShow
[band] => Gregory House
[title] => Happy Birthday
[publisher] => FOX
[year] => 2004
[comment] => House M.D. - 01x06 The Socratic Method
[genre] => Sound clip
[image] => Array
(
[data] => ����JFIFC ...file data itself
[mime] => image/jpeg
[width] => 624
[height] => 352
)
)
*/ #2 Display the duration of the audio file in seconds
// wp_read_audio_metadata() is not available at the front.
if( ! function_exists( 'wp_read_audio_metadata' ) ){
require_once ABSPATH . 'wp-admin/includes/media.php';
}
// the path to the file
$file = wp_get_upload_dir()['basedir'] . '/2016/04/Happy_Birthday.mp3';
$metadata = wp_read_audio_metadata( $file );
echo sprintf( 'The duration of the audio is % sec.', $metadata['length'] );
// will print: The duration of the audio is 60 sec.
Changelog
| Since 3.6.0 | Introduced. |