wp_read_audio_metadata()
云策文档标注
概述
wp_read_audio_metadata() 函数用于从音频文件的 ID3 标签中提取元数据。它接受文件路径作为参数,返回包含音频信息的数组或 false。
关键要点
- 参数:$file(字符串,必需),音频文件的路径。
- 返回值:成功时返回元数据数组,失败时返回 false。
- 依赖 getID3 库进行音频分析,自动处理库的初始化和配置。
- 提取的元数据包括音频属性、文件格式、大小、MIME 类型、播放时长和创建时间戳等。
- 通过 wp_read_audio_metadata 过滤器允许自定义元数据输出。
代码示例
$uploads = wp_upload_dir();
$uploads_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
$file = $uploads_dir . '/example.mp3';
$metadata = wp_read_audio_metadata( $file );
print "Audio is " . $metadata['length'] . ' seconds long';注意事项
- 函数在前端不可用,调用前需引入媒体库:require_once ABSPATH . 'wp-admin/includes/media.php';
- 参数必须是文件路径,而非 URL。
原文内容
Retrieves metadata from an audio file’s ID3 tags.
Parameters
$filestringrequired-
Path to file.
Source
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 );
}
Hooks
- 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.
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
Skip to note 3 content
Codex
Example
$uploads = wp_upload_dir(); $uploads_dir = ( $uploads['baseurl'] . $uploads['subdir'] ); $file = $uploads_dir . '/example.mp3'; $metadata = wp_read_audio_metadata( $file ); print "Audio is " . $metadata['length'] . ' seconds long';Skip to note 4 content
Lucas Marini Falbo
This function does not work 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 unlocks the function require_once ABSPATH . 'wp-admin/includes/media.php'; // Get 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.