wp_read_audio_metadata()WP 3.6.0

Retrieve metadata from a audio file's ID3 tags

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

Return

Array|false. Returns array of metadata, if found.

Usage

wp_read_audio_metadata( $file );
$file(string) (required)
Path to file.

Examples

0

#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
		)

)

*/
0

#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.

wp_read_audio_metadata() code WP 6.5.3

function wp_read_audio_metadata( $file ) {
	if ( ! file_exists( $file ) ) {
		return false;
	}

	$metadata = array();

	if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
		define( 'GETID3_TEMP_DIR', get_temp_dir() );
	}

	if ( ! class_exists( 'getID3', false ) ) {
		require ABSPATH . WPINC . '/ID3/getid3.php';
	}

	$id3 = new getID3();
	// Required to get the `created_timestamp` value.
	$id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName

	$data = $id3->analyze( $file );

	if ( ! empty( $data['audio'] ) ) {
		unset( $data['audio']['streams'] );
		$metadata = $data['audio'];
	}

	if ( ! empty( $data['fileformat'] ) ) {
		$metadata['fileformat'] = $data['fileformat'];
	}

	if ( ! empty( $data['filesize'] ) ) {
		$metadata['filesize'] = (int) $data['filesize'];
	}

	if ( ! empty( $data['mime_type'] ) ) {
		$metadata['mime_type'] = $data['mime_type'];
	}

	if ( ! empty( $data['playtime_seconds'] ) ) {
		$metadata['length'] = (int) round( $data['playtime_seconds'] );
	}

	if ( ! empty( $data['playtime_string'] ) ) {
		$metadata['length_formatted'] = $data['playtime_string'];
	}

	if ( empty( $metadata['created_timestamp'] ) ) {
		$created_timestamp = wp_get_media_creation_timestamp( $data );

		if ( false !== $created_timestamp ) {
			$metadata['created_timestamp'] = $created_timestamp;
		}
	}

	wp_add_id3_tag_data( $metadata, $data );

	$file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;

	/**
	 * Filters the array of metadata retrieved from an audio file.
	 *
	 * In core, usually this selection is what is stored.
	 * More complete data can be parsed from the `$data` parameter.
	 *
	 * @since 6.1.0
	 *
	 * @param array       $metadata    Filtered audio metadata.
	 * @param string      $file        Path to audio file.
	 * @param string|null $file_format File format of audio, as analyzed by getID3.
	 *                                 Null if unknown.
	 * @param array       $data        Raw metadata from getID3.
	 */
	return apply_filters( 'wp_read_audio_metadata', $metadata, $file, $file_format, $data );
}