Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * [cid3v2.c - v2]
- * Inserts some ID3v2 tags in a music file.
- * The tags are:
- * TIT2 : name of the music
- * TPE1 : artist/band
- * TALB : album name
- * APIC : cover art
- * TRCK : track number
- * TCON : genre
- *
- * [Author]
- * Marcos Paulo Ferreira (Daemonio)
- * undefinido gmail com
- * http://daemoniolabs.wordpress.com
- *
- * [How to compile]
- * $ gcc -o cid3v2 cid3v2.c
- *
- * [How to use (examples)]
- * 0) The following command will insert tags into
- * a mp3 file. If the audio file already has tags in
- * it, they will be replaced by the new ones.
- *
- * $ ./cid3v2 -t 'So What' -a 'Miles Davis' -l 'Kind Of Blue'\
- * --apic miles.jpg Miles_Davis-So_What.mp3
- *
- * 1) Inserting (= appending or replacing) tags in a audio file.
- *
- * $ cid3v2 --insert --album 'Kind Of Blue' Miles_Davis-So_What.mp3
- *
- * If the algum tag is already in the file so it will be replaced.
- * If not, it will be inserted without touching the other tags.
- *
- * OBS: The file must be ID3v2.3.x
- *
- * 2) Dumping tags of an audio file:
- *
- * $ cid3v2 --dump Miles_Davis-So_What.mp3
- *
- * FILE : Miles_Davis-So_What.mp3
- * TALB : Kind of Blue
- * TPE1 : Miles Davis
- * TIT2 : So What
- *
- * OBS: The file must be ID3v2.3.x
- *
- * 3) Removing ID3v2 tags (any version) of an audio file.
- *
- * $ cid3v2 --remove Miles_Davis-So_What.mp3
- *
- * Note that now the input file hasn't even a ID3v2 header
- * anymore.
- *
- * 4) Inserting APIC frame to multiple files
- *
- * $ cid3v2 --insert --apic cover_art.jpg *.mp3
- *
- * Insert a cover art imagem to all the mp3 files of
- * the current directory.
- *
- * [Notes]
- * - rhythmbox has some problems with the cover art, so
- * sometimes it won't display the picture.
- * - embedded APIC tag works fine in:
- * picard
- * windows media player
- * xmms (via plugin)
- * audacious
- * - This program uses ID3v2.3.0 and some flags
- * are null by default.
- * - This code was made/tested/compiled under a
- * linux box. I can't guarantee it will work
- * in others systems.
- * - Only one file will be tagged by each run
- * of the program. You can create a script
- * to insert tags in multiple files.
- *
- * First version:
- * Dom Set 25 13:07:08 BRT 2011
- * Seg Set 26 21:35:48 BRT 2011
- *
- * Second version:
- * Sab Out 8 13:06:28 BRT 2011
- * Dom Out 9 15:45:46 BRT 2011
- * Sex Out 14 01:05:30 BRT 2011 - Rest In Peace, dmr.
- * Sab Out 29 01:39:37 BRST 2011
- * Dom Dez 4 10:40:57 BRST 2011
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- /* stat */
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- /* Am I crazy? ;D */
- #include <getopt.h> /* for getopt_long */
- /* only two tag types for now */
- #define TEXT_TAG 0
- #define APIC_TAG 1
- #define INSERT_TAG 2
- /* boolean values */
- #define TRUE 1
- #define FALSE 0
- /* for -vv */
- #define DOUBLE_VERBOSE 2
- /* template filename used by mkstemp() */
- #define FILENAME_TEMPLATE "./cid3v2XXXXXX"
- /* supported tags */
- const char *TAGS[] = {"TIT2", "TPE1", "TALB", "APIC", "TRCK", "TCON"} ;
- /* struct of a frame */
- struct ID3v2frame {
- /* the next 10 bytes are part
- * of header frame */
- char id[4] ;
- char charsize[4] ;
- char flags[2] ;
- /* frame size as an int */
- unsigned long int size ;
- /* TEXT or APIC */
- int type ;
- /* body of the frame */
- char *body ;
- /* a pointer to the next frame */
- struct ID3v2frame *next ;
- } ;
- /* global var: head of the linked list */
- struct ID3v2frame *framehead = NULL;
- /* global var: true to show verbose info.
- * the value DOUBLE_VERBOSE means that
- * the user passed the -vv option. */
- int flagverbose = FALSE ;
- /* malloc()s and exits if error */
- void *my_malloc(unsigned long int size) ;
- /* fopen()s and exits if error */
- FILE *my_fopen(char *filename, char *mode) ;
- /* free the mallocs */
- void freetroydavis() ;
- /* free tags of input file and
- * leave in the memory the tags
- * passed to the command line. */
- void free_tags_of_input_file() ;
- /* u long int to synch safe int */
- unsigned long int synchsafe(unsigned long int in) ;
- /* synch to unsigned long int */
- /* thanks libtaggy ;) */
- unsigned long int ss2uli(char *buf) ;
- /* put an u long int in a string representation */
- void set_size(char *buf, unsigned long int n) ;
- /* string representation to u long int */
- unsigned long int get_size(char *buf) ;
- /* returns image/<image extension> */
- char *get_MIMEtype(char *file) ;
- /* open picture file and return its size in bytes */
- FILE *open_image_apic(char *file, unsigned long int *imgsize) ;
- /* insert a frame in the linked list */
- void insertframe(char *body, int type, int index) ;
- /* write a frame to stream */
- void frame_dump(struct ID3v2frame *frame, FILE *stream) ;
- /* write ID3v2 header to stream */
- void header_dump(struct ID3v2frame *framehead, FILE *stream) ;
- /* called by --help */
- void show_help() ;
- /* TRUE if the tag `id' already exists in
- * the input file */
- char tag_already_in(char *id) ;
- /* increase the linked list with the tags of
- * the input file. This function is called
- * by the --insert option */
- void get_frames_from_file(unsigned long int headersize, FILE *inputfile) ;
- /* dumps to stdout tags of input file on screen. */
- void dump_tags(unsigned long int headersize, FILE *inputfile) ;
- int main(int argc, char **argv) {
- /* options flags */
- int flaginsert = FALSE ;
- int flagremove = FALSE ;
- int flagdump = FALSE ;
- int flagnooption = TRUE ;
- /* TRUE if we have to write in the file */
- int flaghasoutput = TRUE ;
- /* return value of getopt_long() */
- int opt ;
- /* each byte of the music data */
- char byte ;
- /* input and output stream */
- FILE *inputfile, *outputfile ;
- /* each frame */
- struct ID3v2frame *f ;
- /* ID3v2 header of the inputfile (if it exists) */
- char header[10] ;
- /* the size of the ID3v2 header */
- unsigned long int headersize ;
- char *inputfilename ;
- int outputfilefd ;
- char outputfilename[] = FILENAME_TEMPLATE;
- /* for getopt_long */
- const struct option stoption[] =
- {
- {"title" , required_argument, 0 , 't'},
- {"artist" , required_argument, 0 , 'a'},
- {"album" , required_argument, 0 , 'l'},
- {"apic" , required_argument, 0 , 'p'},
- {"track" , required_argument, 0 , 'r'},
- {"genre" , required_argument, 0 , 'g'},
- {"help" , no_argument , 0 , 'h'},
- {"insert" , no_argument , 0 , 'i'},
- {"remove" , no_argument , 0 , 'm'},
- {"dump" , no_argument , 0 , 'd'},
- {"verbose", no_argument , 0 , 'v'},
- {0 , 0 , 0 , 0},
- } ;
- if ( argc < 2 ) {
- fprintf(stderr, "Try --help for more information.\n") ;
- return -1 ;
- }
- while ( (opt=getopt_long(argc, argv, "hvt:a:l:p:r:g:", stoption, NULL)) > 0 ) {
- flagnooption = FALSE ; /* we have an option */
- switch(opt) {
- case 'h':
- show_help() ;
- break;
- case 't':
- insertframe(optarg, TEXT_TAG, 0) ;
- break ;
- case 'a':
- insertframe(optarg, TEXT_TAG, 1) ;
- break ;
- case 'l':
- insertframe(optarg, TEXT_TAG, 2) ;
- break ;
- case 'p':
- insertframe(optarg, APIC_TAG, 3) ;
- break ;
- case 'r':
- insertframe(optarg, TEXT_TAG, 4) ;
- break ;
- case 'g':
- insertframe(optarg, TEXT_TAG, 5) ;
- break ;
- case 'i':
- flaginsert = TRUE ;
- break ;
- case 'v':
- /* for -v */
- if ( flagverbose == FALSE) {
- flagverbose |= TRUE ;
- /* for -vv */
- } else {
- flagverbose |= DOUBLE_VERBOSE ;
- }
- break ;
- case 'm':
- flagremove = TRUE ;
- break ;
- case 'd':
- flagdump = TRUE ;
- flaghasoutput = FALSE ;
- break ;
- default:
- /* strange options .... */
- return -1 ;
- }
- }
- /* I think it's not make sense */
- if ( flagremove == TRUE && flaginsert == TRUE ) {
- fprintf(stderr, "Error: Don't use --remove and --insert together.\n") ;
- return -1 ;
- }
- /* check if at least an input file was given
- * in the command line */
- if ( argv[optind] == NULL ) {
- fprintf(stderr, "Error: No file to parser.\n") ;
- return -1;
- }
- /* no option ? call --dump */
- if ( flagnooption == TRUE ) {
- flaghasoutput = FALSE ;
- flagdump = TRUE ;
- }
- /* clean up function */
- atexit(freetroydavis) ;
- while ( argv[optind++] != NULL ) {
- inputfilename = argv[optind-1] ;
- if ( flagverbose & DOUBLE_VERBOSE ) {
- fprintf(stdout, "Opening: %s\n", inputfilename) ;
- }
- /* open input file */
- inputfile = my_fopen(inputfilename, "rb") ;
- /* get the first 10 bytes. We're looking
- * for a header here */
- memset(header, 0x0, sizeof header) ;
- fread(header, 1, 10, inputfile) ;
- /* Is there a ID3v2.3.x header? */
- if ( !strncmp(header, "ID3", 3) ) {
- headersize = ss2uli(header+6) ;
- if ( header[3] == 0x3 ) {
- /* --dump */
- if ( flagdump == TRUE ) {
- fprintf(stdout, "FILE : %s\n", inputfilename) ;
- dump_tags(headersize, inputfile) ;
- } else if ( flaginsert == TRUE ) { /* --insert */
- /* called insert without a tag option */
- if ( framehead == NULL ) {
- fprintf(stderr,"Error: No tag to insert.\n") ;
- fclose(inputfile) ;
- return -1 ;
- }
- /* put the tags of the input file in memory */
- get_frames_from_file(headersize, inputfile) ;
- }
- } else if ( flagdump == TRUE || flaginsert == TRUE ) {
- fprintf(stderr, "Error: File is not ID3v2.3.x format.\n") ;
- fclose(inputfile) ;
- return -1 ;
- }
- /* skip the whole tags and put the
- * file pointer to the initial audio data */
- fseek(inputfile, 10 + headersize, SEEK_SET) ;
- }
- /* Do we have to write to a file? */
- if ( flaghasoutput == TRUE ) {
- /* mkstemp create a temporary file in the
- * current user directory */
- if ( (outputfilefd = mkstemp(outputfilename)) < 0 ) {
- fprintf(stderr,"Error: Cant create temporary file.\n") ;
- return -1 ;
- }
- /* convert file descriptor to stream */
- outputfile = fdopen(outputfilefd, "w") ;
- /* points to the first frame */
- f = framehead ;
- /* write the ID3v2 header */
- header_dump(f, outputfile) ;
- /* write each frame */
- while ( f != NULL ) {
- if ( flagverbose & DOUBLE_VERBOSE ) {
- fprintf(stderr, "storing: %c%c%c%c\n", f->id[0], f->id[1], f->id[2], f->id[3]) ;
- }
- frame_dump(f, outputfile) ;
- f = f->next ;
- }
- /* write the mp3 data */
- while ( fread(&byte, 1, 1, inputfile) > 0 ) fwrite(&byte, 1, 1, outputfile) ;
- /* replace the input file with the temporary one */
- if ( rename(outputfilename, inputfilename) < 0 ) {
- /* We dont have 'w' in the input file... */
- fprintf(stderr, "Error: Cant overwrite the inputfile.\n") ;
- unlink(outputfilename) ;
- return -1 ;
- }
- if ( flagverbose & TRUE ) {
- fprintf(stdout, "File `%s' was altered successfully.\n", inputfilename) ;
- }
- strcpy(outputfilename, FILENAME_TEMPLATE) ;
- fclose(outputfile) ;
- }
- /* free tags of input file */
- free_tags_of_input_file() ;
- fclose(inputfile) ;
- }
- return 0;
- }
- /*
- * Functions body
- */
- void *my_malloc(unsigned long int size) {
- void *buf ;
- if ( (buf=malloc(size)) == NULL ) {
- perror("malloc") ;
- exit(-1) ;
- }
- return buf ;
- }
- FILE *my_fopen(char *filename, char *mode) {
- FILE *f ;
- if ( (f = fopen(filename, mode)) == NULL ) {
- perror(filename) ;
- exit(-1) ;
- }
- return f ;
- }
- void freetroydavis() {
- struct ID3v2frame *f, *p ;
- f = framehead ;
- while ( f != NULL ) {
- free(f->body) ;
- p = f->next ;
- free(f) ;
- f = p ;
- }
- framehead = NULL ;
- }
- void free_tags_of_input_file() {
- struct ID3v2frame *f, *p=NULL ;
- if ( framehead == NULL ) {
- return ;
- }
- f = framehead ;
- /* p is the last element with non-INSERT_TAG type */
- while ( f != NULL && f->type != INSERT_TAG ) {
- p = f ;
- f = f->next ;
- }
- /* no INSERT_TAG type was found */
- if ( f == NULL ) return ;
- if ( p == NULL ) {
- framehead = NULL ;
- } else {
- p->next = NULL ;
- }
- while ( f != NULL ) {
- p = f->next ;
- if ( f->type == INSERT_TAG ) {
- free(f->body) ;
- free(f) ;
- }
- f = p ;
- }
- }
- unsigned long int synchsafe(unsigned long int in) {
- unsigned long int out, mask = 0x7F;
- while (mask ^ 0x7FFFFFFF) {
- out = in & ~mask;
- out <<= 1;
- out |= in & mask;
- mask = ((mask + 1) << 8) - 1;
- in = out;
- }
- return out;
- }
- unsigned long int ss2uli(char *buf) {
- unsigned long int r ;
- r = (buf[0] & 0x7f) << 21 ;
- r = r | ((buf[1] & 0x7f) << 14) ;
- r = r | ((buf[2] & 0x7f) << 7) ;
- r = r | (buf[3] & 0x7f) ;
- return r ;
- }
- void set_size(char *buf, unsigned long int n) {
- sprintf(buf, "%c%c%c%c",
- (int)(n & 0xFF000000) >> 24,
- (int)(n & 0xFF000) >> 16,
- (int)(n & 0xFF00) >> 8,
- (int)(n & 0xFF)) ;
- }
- unsigned long int get_size(char *buf) {
- unsigned long int n ;
- n = (buf[0] & 0xFF) << 24 ;
- n |= (buf[1] & 0xFF) << 16 ;
- n |= (buf[2] & 0xFF) << 8 ;
- n |= buf[3] & 0xFF;
- return n ;
- }
- char *get_MIMEtype(char *file) {
- char *ch ;
- if( (ch=strrchr(file, '.')) == NULL ) {
- fprintf(stderr, "Error: Cant get file extension.\n") ;
- exit(-1) ;
- }
- /* points to the file extension */
- ch++ ;
- if ( !strcmp(ch, "jpg") ) return "image/jpg" ;
- else if ( !strcmp(ch, "jpeg") ) return "image/jpeg" ;
- else if ( !strcmp(ch, "png") ) return "image/png" ;
- fprintf(stderr, "Error: Unsupported MIME type.\n");
- exit(-1) ;
- }
- FILE *open_image_apic(char *file, unsigned long int *imgsize) {
- FILE *fileapic ;
- struct stat statapic ;
- fileapic = my_fopen(file, "rb") ;
- if ( stat(file, &statapic) < 0 ) {
- perror("stat") ;
- exit(-1) ;
- }
- /* size in bytes of the picture file */
- *imgsize = statapic.st_size ;
- return fileapic ;
- }
- void insertframe(char *body, int type, int index) {
- struct ID3v2frame *new, *next ;
- char *newbody ;
- unsigned long int framesize ;
- unsigned long int bodysize ;
- /* create a new frame */
- new = (struct ID3v2frame *)my_malloc(sizeof(struct ID3v2frame)) ;
- /* is it the first frame? */
- if ( framehead == NULL ) {
- framehead = new;
- } else {
- /* go to the last frame */
- next = framehead ;
- while ( next->next != NULL ) {
- next = next->next ;
- }
- next->next = new ;
- }
- /* copy the id */
- strcpy(new->id, TAGS[index]) ;
- /* no flags */
- new->flags[0]=new->flags[1]=0;
- /* last element */
- new->next = NULL ;
- /* type (TEXT_TAG or APIC_TAG) */
- new->type = type ;
- if ( type == TEXT_TAG ) {
- bodysize = 1 + strlen(body) ;
- newbody = (char *)my_malloc(bodysize) ;
- /* text encoding: 00 */
- sprintf(newbody, "%c%s%n", 0, body, (int *)&framesize) ;
- } else if ( type == APIC_TAG ) {
- char *MIMEtype ;
- unsigned long int apicsize ;
- FILE *apic ;
- /* get the MIMe type */
- MIMEtype = get_MIMEtype(body) ;
- /* Get a stream for the picture */
- apic = open_image_apic(body, &apicsize) ;
- /* size of the apic tag body */
- bodysize = 1 + strlen(MIMEtype) + 1 + 1 + 1 + apicsize ;
- /* get memory for apic body */
- newbody = (char *)my_malloc(bodysize) ;
- /* fulfill apic body */
- sprintf(newbody, "%c%s%c%c%c%n", 0, MIMEtype, 0, 3, 0, (int *)&framesize) ;
- /* put image into memory */
- while ( fread(newbody+framesize, 1, 1, apic) > 0 ) framesize++ ;
- fclose(apic) ;
- }
- /* body of the frame */
- new->body = newbody ;
- /* size of the frame (char) */
- set_size(new->charsize, framesize) ;
- /* size of the frame (u long int) */
- new->size = framesize ;
- }
- void frame_dump(struct ID3v2frame *frame, FILE *stream) {
- fwrite(frame, 1, 10, stream) ;
- fwrite(frame->body, 1, frame->size, stream) ;
- }
- void header_dump(struct ID3v2frame *framehead, FILE *stream) {
- struct ID3v2frame *f ;
- unsigned long int headersize = 0;
- char charheadersize[4] ;
- /* should exist at least one tag to
- * put the header */
- if ( framehead != NULL ) {
- /* get total bytes of the frames */
- f = framehead ;
- while ( f != NULL ) {
- headersize += f->size + 10;
- f = f->next ;
- }
- /* synch safe it */
- headersize = synchsafe(headersize) ;
- set_size(charheadersize, headersize) ;
- /* write the header in stream */
- fprintf(stream, "%s%c%c%c%c%c%c%c", "ID3", 3, 0, 0,
- charheadersize[0],
- charheadersize[1],
- charheadersize[2],
- charheadersize[3]) ;
- }
- }
- void show_help() {
- fprintf(stderr,
- "\t\tcid3v2 - by Daemonio\n\
- Small program that handles useful\n\
- tags of ID3v2 format.\n\n\
- Use: cid3v2 [OPTION]... [FILE]...\n\n\
- -h, --help , show this help\n\
- -t, --title TITLE , song title\n\
- -a, --artist ARTIST , artist/band\n\
- -l, --album ALBUM , album\n\
- -g, --genre GENRE , genre\n\
- -r, --track NUMBER , track number\n\
- -p, --apic PICTURE , cover art\n\
- --insert , insert tags\n\
- --remove , remove all ID3v2 tags\n\
- --dump , dumps text tags on screen\n\
- --verbose , show debug info\n\
- ps: PICTURE = full path of the image\n") ;
- exit(-1);
- }
- char tag_already_in(char *id) {
- struct ID3v2frame *f = framehead ;
- while ( f != NULL ) {
- if ( strncmp(f->id, id, 4) == 0 ) {
- return TRUE ;
- }
- f = f->next ;
- }
- return FALSE ;
- }
- void get_frames_from_file(unsigned long int headersize, FILE *inputfile) {
- unsigned long int count = 0, framesize ;
- struct ID3v2frame *last, *new ;
- /* header of each frame */
- char buffer[10] ;
- /* body of the frame */
- char *body ;
- if ( framehead != NULL ) {
- last = framehead ;
- while ( last->next != NULL ) last = last->next ;
- }
- while ( headersize > count ) {
- /* read the frame header */
- fread(buffer, 1, 10, inputfile) ;
- /* we reach the padding */
- if ( buffer[0] == 0 ) break ;
- if ( flagverbose & DOUBLE_VERBOSE ) {
- fprintf(stdout, "Reading %c%c%c%c, size: %x%x%x%x",
- buffer[0],
- buffer[1],
- buffer[2],
- buffer[3],
- buffer[4] & 0xff,
- buffer[5] & 0xff,
- buffer[6] & 0xff,
- buffer[7] & 0xff) ;
- }
- /* frame size */
- framesize = get_size(buffer + 4) ;
- /* skip if the frame already exists */
- if ( tag_already_in(buffer) == FALSE ) {
- if ( flagverbose & DOUBLE_VERBOSE ) fprintf(stdout, ", doesn't exist in file.\n") ;
- new = (struct ID3v2frame *)my_malloc(sizeof(struct ID3v2frame)) ;
- memcpy(new, buffer, 10) ;
- /* get memory for the body */
- body = (char *)my_malloc(framesize) ;
- fread(body, 1, framesize, inputfile) ;
- new->body = body ;
- new->type = INSERT_TAG ;
- new->size = framesize ;
- new->next = NULL ;
- if ( framehead == NULL ) { /* first element */
- framehead = new ;
- } else {
- last->next = new ;
- }
- last = new ;
- } else {
- if ( flagverbose & DOUBLE_VERBOSE ) fprintf(stdout, ", already exists in file.\n") ;
- fseek(inputfile, framesize, SEEK_CUR) ;
- }
- count += 10 + framesize ;
- }
- }
- void dump_tags(unsigned long int headersize, FILE *inputfile) {
- struct ID3v2frame *f ;
- get_frames_from_file(headersize, inputfile) ;
- f = framehead ;
- while ( f != NULL ) {
- /* show tag ID */
- fprintf(stdout,"%c%c%c%c : ",
- f->id[0],
- f->id[1],
- f->id[2],
- f->id[3]) ;
- /* if it is a text tag, show its body */
- if ( f->id[0] == 'T' ) {
- fwrite(f->body+1, 1, f->size-1, stdout) ;
- } else {
- /* for APIC tag we can show the MIME type
- * and picture type (FRONT COVER, BACK CONVER, etc)*/
- if ( ! strncmp(f->id, "APIC", 4) ) {
- fprintf(stdout, "MIME type %s, pic type %x,",
- f->body + 1, f->body[0] & 0x000000FF) ;
- }
- fprintf(stdout, " size: %lu", get_size(f->charsize)) ;
- }
- putchar('\n') ;
- f = f->next ;
- }
- }
- /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment