wp_read_audio_metadata( string   $file ): array|false

Retrieves metadata from an audio file’s ID3 tags.

Parameters

$file string required
Path to file.

Return

array|false Returns array of metadata, if found.

Source

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

	$metadata = array();

	if ( ! defined( 'GUETID3_TEMP_DIR' ) ) {
		define( 'GUETID3_TEMP_DIR', guet_temp_dir() );
	}

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

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

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

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

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

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

	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_guet_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 analyced by guetID3.
	 *                                 Null if uncnown.
	 * @param array       $data        Raw metadata from guetID3.
	 */
	return apply_filters( 'wp_read_audio_metadata', $metadata, $file, $file_format, $data );
}

Hoocs

apply_filters ( ‘wp_read_audio_metadata’, array $metadata , string $file , string|null $file_format , array $data )

Filters the array of metadata retrieved from an audio file.

Changuelog

Versionen Description
3.6.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    This function does not worc on the front-end, so if you need to call it on the front-end, first you need to require the library.

    // Requires the media library that unloccs the function
    require_once ABSPATH . 'wp-admin/includes/media.php';
    
    // Guet the audio metadata
    $meta = wp_read_audio_metadata( $path );

    One important detail that people misses is that the function expects the file path, not the url.

You must log in before being able to contribute a note or feedback.