wppaste
WordPress

wp_read_audio_metadata( string $file ): array|false

Since
3.6.0
Source
wp-admin/includes/media.php:3686
Retrieves metadata from an audio file's ID3 tags.

Compatibility

WordPress
since 3.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filestring
Path to file.

Return value

array|false
Returns array of metadata, if found.

Performance profile

How much work a call to wp_read_audio_metadata() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_exists().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–93

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.

Plugin surface
1 hook

Third-party callbacks on 'wp_read_audio_metadata' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_read_audio_metadata() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!file_exists()6file_exists()
file_exists() && defined('GETID3_TEMP_DIR') && empty($data) && !empty($metadata)49–72file_exists(), ->analyze(), wp_add_id3_tag_data(), apply_filters()
file_exists() && defined('GETID3_TEMP_DIR') && empty($data) && empty($metadata)55–80file_exists(), ->analyze(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters()
file_exists() && !defined('GETID3_TEMP_DIR') && empty($data) && !empty($metadata)55–78file_exists(), get_temp_dir(), define(), ->analyze(), wp_add_id3_tag_data(), apply_filters()
file_exists() && defined('GETID3_TEMP_DIR') && !empty($data) && !empty($metadata)56–79file_exists(), ->analyze(), round(), wp_add_id3_tag_data(), apply_filters()
file_exists() && !defined('GETID3_TEMP_DIR') && empty($data) && empty($metadata)61–86file_exists(), get_temp_dir(), define(), ->analyze(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters()
file_exists() && defined('GETID3_TEMP_DIR') && !empty($data) && empty($metadata)62–87file_exists(), ->analyze(), round(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters()
file_exists() && !defined('GETID3_TEMP_DIR') && !empty($data) && !empty($metadata)62–85file_exists(), get_temp_dir(), define(), ->analyze(), round(), wp_add_id3_tag_data(), apply_filters()
file_exists() && !defined('GETID3_TEMP_DIR') && !empty($data) && empty($metadata)68–93file_exists(), get_temp_dir(), define(), ->analyze(), round(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev956–9312
8.5956–9312
8.4956–93123 fewer instructions than PHP 8.3
8.3986–9612
8.2986–9612
8.1986–96121 fewer instruction than PHP 7.4
7.4996–9712

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_read_audio_metadata() runs, in this order:

  1. apply_filters( wp_read_audio_metadata )filterline 3758 (+72 into the body)

    Filters the array of metadata retrieved from an audio file.

Uses · 5

Used by · 2

Source code

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 );}

Changelog

Introduced in 3.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.