ID3v1 reader

From ProjectWiki
(Difference between revisions)
Jump to: navigation, search
m (Protected "ID3v1 reader" [edit=sysop:move=sysop])
 

Latest revision as of 10:21, 21 January 2012

Simple ID3v1 Reader

Contents

What is this?

A simple and quick command line ID3v1 tag reader for getting artist, title, etc data from mp3s. Googling for a pre-existing one was taking too long so decided to write this one night. Most of the others had lots of excess features, like a GUI, pffft! Enjoy the redundancy! :P

Examples

Compile
This is obvious, just copy the code below to main.c and:

gcc -o id3 main.c

Using

~/Projects/mp3tagread$ ./id3 /media/160gb/e/MP3s/Dead\ Prez\ -\ Hip-Hop.mp3 
title: Hip-Hop
artist: Dead Prez
album: Lets Get Free
year: 2001                            
comment:                             
genre: 12

Getting just one field

~/Projects/mp3tagread$ ./id3 /media/160gb/e/MP3s/Dead\ Prez\ -\ Hip-Hop.mp3 | grep "^title\:"
title: Hip-Hop

Translating SJIS to UTF-8
Requires the recode command, but can be used to translate id3 tags :D

~/Projects/mp3tagread$ ./id3 ~/Music/th03_東方夢時空\ ~\ Phantasmagoria\ \
of\ Dim.Dream/th03_08-夢消失 ~Lost\ \
Dream.mp3 | grep "^title\:" | recode -f SJIS...UTF-8
title: 夢消失 〜Lost Dream          ZUN(太田順也)            東方夢時空

(please note: the last example is entirely why i wrote this :P)

Source Code

/**************************************************************
*  quicke mp3 id3 tag reader... written in 8 mins by erisu :P
***************************************************************/
#include <stdio.h>
 
#define MP3ID3V1MAGIC	"TAG"
#define MP3ID3V1MAGIC_SIZE	3
 
 
//This at last 128 (-3 for "TAG") bytes of file is the tag
typedef struct _ID3V1_T {
	char 	magic[3];
	char	title[30];
	char	artist[30];
	char	album[30];
	char	year[4];
	char	comment[30];
	char	genre;
#ifdef ARM		//This rly should be like "NDS" or something
} ID3V1_T;
#else
} ID3V1_T __attribute__((packed));
#endif
 
int main(int argc, char **argv) {
	ID3V1_T id3;
	FILE *file;
	if(argc==2) {
		file=fopen(argv[1],"rb");
		if(file) {
			fseek(file, -sizeof(id3), SEEK_END);
			fread(&id3, sizeof(id3), 1, file);
			fclose(file);
			if(strncmp(id3.magic, MP3ID3V1MAGIC, MP3ID3V1MAGIC_SIZE)==0) {
				printf("title: %s\n", id3.title);
				printf("artist: %s\n", id3.artist);
				printf("album: %s\n", id3.album);
				printf("year: %s\n", id3.year);
				printf("comment: %s\n", id3.comment);
				printf("genre: %d\n", id3.genre);
			} else {
				printf("not id3?! (or id3 not at end of file)\n");
			}
		} else {
			printf("unable to open %s  u_u\n");
		} 
	} else {
		printf("usage: %s FILENAME.mp3\n",argv[0]);
	}
}
Personal tools
irssi scripts
eggdrop scripts