碰巧的是,我坐在这里的一个项目正在等待完成(大约 80%,当谈到 Delphi 时,我更像是一个业余爱好者,并且有更多紧迫的事情出现,然后我找到了一个可以下载的程序正是我的要求)是一个完整的 MP3 文件 ID3 标签编辑器。虽然 v1 超级简单,但 v2 更难。您可以在此处参考v2.3 的标准文档。但我将仅限于这里提到的几点。
您可能需要 ID3v2 标签,具体取决于应用程序。我的便携式 MP3 播放器只接受 v2 标签,这是促使我首先做这个项目的原因。
ID3v2 标签以可变长度方式写入文件的开头,其中可能存在或不存在可变数量的标签。幸运的是,如果它是一个 ID3v2 标记文件,则数据的全长应该在第一条记录中。因此,读取文件找到 ID3v2 数据的长度,然后重写没有 ID3v2 数据的文件并删除标签。在一开始就拥有数据使得这很有必要,而且确实令人沮丧。我将来对代码所做的任何事情都将涉及尝试更改数据。下面是一些非常脏的代码,AFAIR 工作,但如果你使用,你将需要清理(我相信这里有一些内容会明确指出如何我应该)。但为了确定,好好测试一下。还请务必询问我是否遗漏了您需要的单元中的任何内容(这是一个 19.3KB 的 pas 文件):
type
sarray = array[0..3] of byte;
psarray = ^sarray;
ID3v2Header = packed record
identifier: array[0..2] of char;
major_version: byte;
minor_version: byte;
flags: byte;
size: DWord;
end;
function size_decodeh(insize: DWord): DWord;
{ decodes the size headers only, which does not use bit 7 in each byte,
requires MSB conversion as well }
var
outdval: DWord;
outd, ind: psarray;
tnext2, pnext2: byte;
begin
outdval := 0;
outd := @outdval;
ind := @insize;
tnext2 := ind^[2] shr 1;
pnext2 := ind^[1] shr 2;
outd^[0] := ind^[3] or ((ind^[2] and $01) shl 7);
outd^[1] := tnext2 or ((ind^[1] and $03) shl 6);
outd^[2] := pnext2 or ((ind^[0] and $07) shl 5);
outd^[3] := ind^[0] shr 3;
Result := outdval;
end;
procedure ID3v2_LoadData(filename: string; var memrec: pointer;
var outsize: integer);
{ procedure loads ID3v2 data from "filename". Returns outsize = 0 if
there is no ID3v2 data }
var
infile: file;
v1h: ID3V2Header;
begin
assign(infile, filename);
reset(infile, 1);
// read main header to get id3v2 size
blockread(infile, v1h, sizeof(v1h));
// detect if there is id3v2 data
if v1h.identifier = 'ID3' then
begin
outsize := size_decodeh(v1h.size);
// read ID3v2 header data
getmem(memrec, outsize);
blockread(infile, memrec^, outsize);
Close(infile);
end
else
outsize := 0;
end;
function id3v2_erase(infilestr: string): boolean;
{ erase all ID3v2 data. Data are stored at the beginning of file, so file
must be rewritten }
const
tempfilename = 'TMp@!0X.MP3';
var
memrec: pointer;
outsize, dataread: integer;
IsID3v2: boolean;
databuffer: array[1..32768] of byte;
newfile, origfile: file;
begin
// reuse service routine to get information
Id3v2_loaddata(infilestr, memrec, outsize);
// is there ID3v2 data?
if outsize > 0 then
begin
// need to clean up after the service routine
freemem(memrec);
// get amount of data to erase
outsize := outsize + sizeof(Id3v2Header);
writeln('Data to delete is: ', outsize, ' bytes.');
// now rewrite the file
AssignFile(origfile, infilestr);
reset(origfile, 1);
AssignFile(newfile, tempfilename);
rewrite(newfile, 1);
Seek(origfile, outsize);
repeat
blockread(origfile, databuffer, sizeof(databuffer), dataread);
blockwrite(newfile, databuffer, dataread);
until dataread = 0;
CloseFile(origfile);
CloseFile(newfile);
// rename temp file and delete original
DeleteFile(infilestr);
RenameFile(tempfilename, infilestr);
IsID3v2 := true;
end
else
IsID3v2 := false;
Result := IsID3v2;
end;
适用于大多数情况的完整编辑功能显然比这更难攀登,但所有细节都在我链接到的那个文档中。希望这可以帮助你。