Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ID3v1 files
- // https://en.wikipedia.org/wiki/ID3
- // http://www.dreamincode.net/forums/topic/287707-read-mp3-tags-id3-v1-c/
- // http://freemusicarchive.org/member/happypuppyrecords/30_seconds_or_less
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MP3_FILENAME "John_Wesley_Coleman_-_07_-_Tequila_10_Seconds.mp3"
- typedef struct
- {
- char tag[3]; // 3 byte(s)
- char title[30]; //30 byte(s)
- char artist[30]; //30 byte(s)
- char album[30]; //30 byte(s)
- char year[4]; // 4 byte(s)
- char comment[30]; //30 byte(s)
- unsigned char genre; // 1 byte(s)
- } mp3Tag ;
- int main()
- {
- /* Open MP3 binary file */
- FILE *file_ptr = NULL;
- file_ptr = fopen(MP3_FILENAME, "rb");
- if (!file_ptr)
- {
- perror("Error fopen()");
- return EXIT_FAILURE;
- }
- /* make new header struct */
- mp3Tag tag = {"NA", "NA", "NA", "NA", "NA", "NA", '\0'}; // NA = NOT-AVAILABLE
- // Seek to 128 bytes before the end of the file
- if (fseek(file_ptr, -1 * sizeof(mp3Tag), SEEK_END) == -1)
- {
- perror("Error fseek()");
- return EXIT_FAILURE;
- }
- // Read the tag
- if (fread(&tag, sizeof(mp3Tag), 1, file_ptr) != 1)
- {
- fprintf(stderr, "Error fread()\n");
- return EXIT_FAILURE;
- }
- fclose(file_ptr);
- // Make sure we've got what we expect.
- if (memcmp(tag.tag, "TAG", 3) == 0)
- {
- // Found the tag where we expected
- printf("Title ... %.30s\n", tag.title);
- printf("Artist .. %.30s\n", tag.artist);
- printf("Album ... %.30s\n", tag.album);
- printf("Year .... %.4s\n", tag.year);
- if (tag.comment[28] == '\0')
- {
- printf("Comment..%.28s\n", tag.comment);
- printf("Track ... %d\n", tag.comment[29]);
- }
- else
- {
- printf("Comment..%.30s\n", tag.comment);
- }
- printf("Genre ... %d\n", tag.genre);
- }
- else
- {
- fprintf(stderr, "Failed to find TAG\n");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement