搜尋套件、儲存庫與 APP 分類

套件詳情v1.8.1

audio_metadata_reader

音訊檔案的元資料讀取器。支援 ID3、Vorbis 標註和 ILST iTunes

AndroidiOSmacOSWindowsLinux
近 30 天下載
1.6萬
喜歡數
44
Pub 分數
160

安裝套件

flutter pub add audio_metadata_reader

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter/Dart 套件,可從音訊檔案中提取元資料,支援 ID3 標籤(常見於 MP3 檔案)、Vorbis 註解(用於 OGG 檔案)以及 ILST iTunes 元資料。此套件讓開發者能夠跨多個平台讀取音訊的重要資訊,例如標題、藝術家、專輯、類型等。該套件輕量級且設計為可無縫整合至需要音訊檔案分析的多媒體應用程式中。它可在 Android、iOS、macOS、Windows 及 Linux 上運作,非常適合跨平台音訊工具。

適用情境

  • 在音樂應用中讀取歌曲元數據
  • 從音頻文件中提取藝術家和專輯資訊
  • 建構媒體庫管理系統
  • 解析播客應用的元數據
  • 支援音訊檔案標籤功能

主要特色

  • 支援 ID3、Vorbis 標註和 ILST 元資料
  • 跨平台相容性
  • 輕量且容易整合
  • 提供結構化元數據輸出
  • 支援常見音訊格式

適合對象

  • 音樂播放器開發者
  • 媒體庫建構器
  • 音頻檔案處理工具
  • 播客應用開發者
  • 跨平台多媒體應用

使用建議

  • 僅限讀取元數據,無法編輯
  • 不支援所有音訊格式
  • 元數據解析依賴於檔案完整性
  • 損壞檔案沒有內建錯誤處理
  • 在某些平台需要正確的檔案權限

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Audio Metadata Reader

A pure Dart package for reading and writing metadata in various audio formats.

File Format Metadata Format(s) Read Write
MP3 ID3v1, ID3v2 ✅ ✅
MP4 iTunes-style ilst ✅ ✅
FLAC Vorbis Comments ✅ ✅
OGG Vorbis Comments ✅ ❌
Opus Vorbis Comments ✅ ❌
WebM/Matroska Tags / Opus ✅ ❌
WAV RIFF ✅ ✅
AIFF/AIFC IFF chunks ✅ ❌
APE APEv2 ✅ ✅

This package is still under active development. If there's a metadata format you'd like to see supported or specific information you’d like the library to expose, feel free to open an issue.

Usage

Read
import 'dart:io';
import 'package:audio_metadata_reader/audio_metadata_reader.dart';

void main() {
  final track = File("Pieces.mp3");

  // Fetching images can slow down metadata reading
  final metadata = readMetadata(track, getImage: false);

  print(metadata.title);
  print(metadata.album);
}

readMetadata returns the common AudioMetadata view shared by all supported formats. Use readAllMetadata when you need the format-specific metadata object, such as Mp3Metadata, Mp4Metadata, VorbisMetadata, RiffMetadata or ApeMetadata.

import 'dart:io';

import 'package:audio_metadata_reader/audio_metadata_reader.dart';

final track = File("Pieces.mp3");
final metadata = readAllMetadata(track, getImage: false);
print(metadata);
Supported File Extensions

The package currently recognizes these extensions:

.mp3, .flac, .mp4, .m4a, .ape, .ogg, .opus, .wav, .webm, .mkv, .aif, .aiff, .aifc and .mov.

Use supportedFileExtensions when filtering files before parsing:

final isSupported = supportedFileExtensions.any(
  (ext) => file.path.toLowerCase().endsWith(ext),
);
Write
import 'dart:io';
import 'dart:typed_data';

import 'package:audio_metadata_reader/audio_metadata_reader.dart';

void main() {
  final track = File("Pieces.mp3");

  // Use a switch if you want to update metadata based on the file type
  updateMetadata(
    track,
    (metadata) {
      switch (metadata) {
        case Mp3Metadata m:
          m.songName = "New title";
          break;
        case Mp4Metadata m:
          m.title = "New title";
          break;
        case VorbisMetadata m:
          m.title = ["New title"];
          break;
        case RiffMetadata m:
          m.title = "New title";
          break;
        case ApeMetadata m:
          m.title = "New title";
      }
    },
  );

  // Or use extension methods for common metadata updates
  updateMetadata(
    track,
    (metadata) {
      metadata.setTitle("New title");
      metadata.setArtist("New artist");
      metadata.setAlbum("New album");
      metadata.setTrackNumber(1);
      metadata.setYear(DateTime(2014));
      metadata.setLyrics("I'm singing");
      metadata.setGenres(["Rock", "Metal", "Salsa"]);
      metadata.setPictures([
        Picture(Uint8List.fromList([]), "image/png", PictureType.coverFront)
      ]);
    },
  );
}

updateMetadata reads the existing format-specific metadata, applies the callback, then writes it back to the same file while retaining metadata that the callback does not change. For an already constructed metadata object, use replaceMetadata directly:

import 'dart:io';

import 'package:audio_metadata_reader/audio_metadata_reader.dart';

final metadata = Mp3Metadata()..songName = "New title";
replaceMetadata(File("Pieces.mp3"), metadata);

replaceMetadata reconstructs the supported metadata from the object you provide. It is destructive: tags that are not represented by that object may be removed. writeMetadata is deprecated and behaves the same way; use updateMetadata when you need to preserve existing metadata that you are not editing.

The replacement is written to a temporary file in the same directory and renamed into place only after the writer completes, so a failed write leaves the original file untouched. The format-specific writer classes use the same atomic write path.

The common setter extension also provides setTrackTotal and setCD. Some formats do not support every field; unsupported setters are intentionally ignored for those formats.

MP4 metadata can also contain Chapter markers. OGG, Opus and AIFF/AIFC are currently read-only; metadata writing is supported for MP3, MP4, FLAC, WAV and APE files.

Performance

On my laptop with an SSD, the library can process metadata from 3,392 tracks in under 200ms — assuming covers aren't fetched. With covers, it's around 400ms.

import 'dart:io';
import 'package:audio_metadata_reader/audio_metadata_reader.dart';

void main() {
  final folder = Directory(r"music folder")
      .listSync(recursive: true)
      .whereType<File>()
      .where(
        (file) => supportedFileExtensions.any(
          (ext) => file.path.toLowerCase().endsWith(ext),
        ),
      )
      .toList();

  print("Number of tracks: ${folder.length}");

  final start = DateTime.now();

  for (final file in folder) {
    readMetadata(file, getImage: false);
  }

  final end = DateTime.now();
  print("Duration: ${end.difference(start)}");
}
Compare with FFmpeg/libavformat

The repository includes a standalone benchmark that recursively scans a music directory and reads metadata without loading cover images:

dart run tool/benchmark_metadata.dart --backend both --runs 3

It defaults to ~/Music. Use --music PATH for another directory and --backend library to measure only this package. The ffprobe comparison uses FFmpeg/libavformat through the ffprobe executable and requests only format_tags; its timing includes one process launch per file.

To list the files that fail to parse, run the diagnostic script:

dart run tool/find_metadata_errors.dart --music ~/Music

It prints each failing path, the exception message, and an error summary. Add --stack-traces when investigating a parser failure in detail.

Anonymize a Music Track

If you need to report an issue or test the library without sharing private audio, you can anonymize a track by replacing its audio with white noise using ffmpeg:

ffmpeg -i <your_track> -f lavfi -t 5 -i "anoisesrc=color=white:duration=5" -map_metadata 0 -map 1:a -t 5 <output_track>

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案