Skip to content

Instantly share code, notes, and snippets.

@laabroo
Last active December 16, 2015 14:39
Show Gist options
  • Save laabroo/5449792 to your computer and use it in GitHub Desktop.
Save laabroo/5449792 to your computer and use it in GitHub Desktop.
Read information from song
/*
*reference from :
*http://supportforums.blackberry.com/t5/Cascades-Development/Reading-mp3-tags/td-p/2126579
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char tag[3];
char title[30];
char artist[30];
char album[30];
char year[4];
char comment[30];
unsigned char genre;
} mp3Tag;
QString ClassName::ReadInfoSong(const QString MP3_FILENAME) {
FILE *fp = fopen(MP3_FILENAME, "rb");
if (!fp) {
perror("File open failed");
return "1";
}
mp3Tag tag;
// Seek to 128 bytes before the end of the file
if (fseek(fp, -1 * sizeof(mp3Tag), SEEK_END) == -1) {
perror("fseek failed");
return "2";
}
// Read the tag
if (fread(&tag, sizeof(mp3Tag), 1, fp) != 1) {
fprintf(stderr, "Failed reading tag\n");
return "3";
}
// 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 "4";
}
return tag.title+"-"+tag.artist+"-"+tag.album+"-"+tag.year;
}
/*
* Note MP3_FILENAME is a file path from song.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment