View/Edit Id3 Data for Mp3 Files

View/edit ID3 data for MP3 files

Thirding TagLib Sharp.

TagLib.File f = TagLib.File.Create(path);
f.Tag.Album = "New Album Title";
f.Save();

How to read and write ID3 tags to an MP3 in C#?

Taglib# is the best. It's direct port of the TagLib C library to C#.

To install TagLib#, run the following command in the Package Manager Console in Visual Studio.

PM> Install-Package taglib

The NuGet distribution of taglib-sharp can be found at http://nuget.org/packages/taglib.

The official source code repository is at https://github.com/mono/taglib-sharp.

Here's an example using the library:

TagLib.File file = TagLib.File.Create("mysong.mp3");
String title = file.Tag.Title;
String album = file.Tag.Album;
String length = file.Properties.Duration.ToString();

Changing a MP3 files ID3 tags


if( writeTags($row['title'],$new) ) { 
$fileName = rand(000000000,999999999).'_'.rand(0000000000,9999999999).'_'.rand(000000000,999999999).'.mp3';
$imageName = rand(000000000,999999999).'_'.rand(0000000000,9999999999).'_'.rand(000000000,999999999).'.jpg';
rename($new, $DPATH.'uploads/tracks/'.$fileName);
save_image($row['image'],$DPATH.'uploads/media/'.$imageName);
$track['uid'] = 151;
$track['title'] = $row['title'];
$track['description'] = '';
# fileName
$track['name'] = $fileName;
# make tag
$track['tag'] = $tag.',';
# download image
$track['art'] = $imageName;
# today date
$track['release'] = date("Y-m-d");
$track['size'] = filesize($DPATH.'uploads/tracks/'.$fileName);

$row['slippery_id'] = add_to_slippery($track);
}

function writeTags($title,$file) {
$TextEncoding = 'UTF-8';
require_once($DPATH.'cron/getid3/getid3.php');
$getID3 = new getID3;
$getID3->setOption(array('encoding'=>$TextEncoding));
require_once($DPATH.'cron/getid3/write.php');
$tagwriter = new getid3_writetags;
$tagwriter->filename = $file;
$tagwriter->tagformats = array('id3v1','id3v2.3');
$tagwriter->overwrite_tags = true;
$tagwriter->tag_encoding = $TextEncoding;
$tagwriter->remove_other_tags = true;
$TagData = array(
'album' => array($MP3TAG),
'comment' => array($MP3TAG),
);
$fd = fopen($DPATH.'cron/mp3image.png', 'rb');
$APICdata = fread($fd, filesize($DPATH.'cron/mp3image.png'));
fclose($fd);
$TagData['attached_picture'][0]['data'] = $APICdata;
$TagData['attached_picture'][0]['picturetypeid'] = 2;
$TagData['attached_picture'][0]['description'] = $MP3TAG;
$TagData['attached_picture'][0]['mime'] = 'image/jpeg';
$tagwriter->tag_data = $TagData;

if ($tagwriter->WriteTags()) {
return true;
} else {
return false;
}
}

I recently did something similar, you can edit the above code to match your requirements, if you wanted me to be more helpful you should have provided your own code within your question.

Please note that asking questions like this is often how your questions get shut down, good luck in the future and I hope this helps you.

EDIT: Use this GitHub for getting the ID3 tags.

How to edit metadata/ID3 of every filetype (.mp4, .mp3, .mpeg, .m4a, etc.) in C#?

Since you found TagLib, try out TagLib# AKA (taglib-sharp). There is even a nuget package for it.

How to get and set (change) ID3 tag (metadata) of audio files?

I think this is what you are looking for MyID3 library to set and get tags for media file.

Download this jar file MyID3_for_android and add it to your project's build path.
here is the sample code. here pathdata is the file path of the audio file.

            File src = new File(pathdata);
MusicMetadataSet src_set = null;
try {
src_set = new MyID3().read(src);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // read metadata

if (src_set == null) // perhaps no metadata
{
Log.i("NULL", "NULL");
}
else
{
try{
IMusicMetadata metadata = src_set.getSimplified();
String artist = metadata.getArtist();
String album = metadata.getAlbum();
String song_title = metadata.getSongTitle();
Number track_number = metadata.getTrackNumber();
Log.i("artist", artist);
Log.i("album", album);
}catch (Exception e) {
e.printStackTrace();
}
File dst = new File(pathdata);
MusicMetadata meta = new MusicMetadata("name");
meta.setAlbum("Chirag");
meta.setArtist("CS");
try {
new MyID3().write(src, dst, src_set, meta);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ID3WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // write updated metadata
}

Happy Coding :)

c# MP3 ID3 data (without taglib)

The code you provided only supports ID3v1. That version does not support images.

However, the ID3v2 tag does support images. See section 4.15 of the 2.3 informal standard for an explanation of the "attached image" tag. Note that you'll need to write a full ID3v2 parser if you wish to read out the image without a tag library.

Good luck!

Remove or edit ID3Tag version 2 from MP3 file using Delphi 7

As it happens, one of my projects sitting here that is awaiting completion (about 80%, I'm more a hobbyist when it comes to Delphi and had more pressing stuff come up, then I found a program I was able to download which fit my requirements precisely) is a full ID3 tag editor for MP3 files. While v1 was super-easy, v2 is much harder. You can refer to the standard document for v2.3 here.
But I will confine myself to the points addressed here.

You might want ID3v2 tags depending on the application. My portable MP3 player only accepts v2 tags, which is what pushed me to do the project in the first place.

ID3v2 tags are written at the beginning of files in a variable length manner with variable numbers of tags which may or may not be present. Fortunately, the full length of the data should be in the first record if it's an ID3v2 tagged file. Hence, read the file locate the length of the ID3v2 data, then rewrite the file without the ID3v2 data and the tags are removed. Having the data at the beginning makes this necessary and is indeed a frustration. Anything I do in the future to the code would involve trying to change data in place. Some very dirty code follows, which AFAIR worked, but you will need to clean up if you use (I'm sure some here will be content to point out exactly how I should). But test it well just to be sure. Also be sure to ask if I missed anything from the unit I copied this out of (it's a 19.3KB pas file) that you would need:

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;

Full editing capability that works in most all situations is obviously a tougher hill to climb than that, but all the details are there in that document I linked to. Hopefully this helps you out.



Related Topics



Leave a reply



Submit