Advertisement
Guest User

Read MP3 Description

a guest
Feb 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. // ID3v1 files
  2. // https://en.wikipedia.org/wiki/ID3
  3. // http://www.dreamincode.net/forums/topic/287707-read-mp3-tags-id3-v1-c/
  4. // http://freemusicarchive.org/member/happypuppyrecords/30_seconds_or_less
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define MP3_FILENAME "John_Wesley_Coleman_-_07_-_Tequila_10_Seconds.mp3"
  11.  
  12. typedef struct
  13. {
  14.    char tag[3];         // 3 byte(s)
  15.    char title[30];      //30 byte(s)
  16.    char artist[30];     //30 byte(s)
  17.    char album[30];      //30 byte(s)
  18.    char year[4];        // 4 byte(s)
  19.    char comment[30];    //30 byte(s)
  20.    unsigned char genre; // 1 byte(s)
  21. } mp3Tag ;
  22.  
  23.  
  24. int main()
  25. {
  26.     /* Open MP3 binary file */
  27.     FILE *file_ptr = NULL;
  28.     file_ptr = fopen(MP3_FILENAME, "rb");
  29.     if (!file_ptr)
  30.     {
  31.         perror("Error fopen()");
  32.         return EXIT_FAILURE;
  33.     }
  34.  
  35.     /* make new header struct */
  36.     mp3Tag tag = {"NA", "NA", "NA", "NA", "NA", "NA", '\0'}; // NA = NOT-AVAILABLE
  37.  
  38.     // Seek to 128 bytes before the end of the file
  39.     if (fseek(file_ptr, -1 * sizeof(mp3Tag), SEEK_END) == -1)
  40.     {
  41.         perror("Error fseek()");
  42.         return EXIT_FAILURE;
  43.     }
  44.  
  45.     // Read the tag
  46.     if (fread(&tag, sizeof(mp3Tag), 1, file_ptr) != 1)
  47.     {
  48.         fprintf(stderr, "Error fread()\n");
  49.         return EXIT_FAILURE;
  50.     }
  51.  
  52.     fclose(file_ptr);
  53.  
  54.     // Make sure we've got what we expect.
  55.     if (memcmp(tag.tag, "TAG", 3) == 0)
  56.     {
  57.         // Found the tag where we expected
  58.         printf("Title ... %.30s\n", tag.title);
  59.         printf("Artist .. %.30s\n", tag.artist);
  60.         printf("Album ... %.30s\n", tag.album);
  61.         printf("Year .... %.4s\n", tag.year);
  62.        
  63.         if (tag.comment[28] == '\0')
  64.         {
  65.             printf("Comment..%.28s\n", tag.comment);
  66.             printf("Track ... %d\n", tag.comment[29]);
  67.         }
  68.         else
  69.         {
  70.             printf("Comment..%.30s\n", tag.comment);
  71.         }
  72.         printf("Genre ... %d\n", tag.genre);
  73.     }
  74.     else
  75.     {
  76.         fprintf(stderr, "Failed to find TAG\n");
  77.         return EXIT_FAILURE;
  78.     }
  79.     return EXIT_SUCCESS;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement