-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid3tag.c
133 lines (121 loc) · 3.14 KB
/
id3tag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <id3v2lib.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "id3tag.h"
static int one(){
//Allow everything through by directly returning 1
return 1;
}
int checkSuffix(const char *str, const char *suffix){
// Check file extention
if (!str || !suffix){
return -1;
}
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr){
return -1;
}
return strcmp(str + (lenstr - lensuffix), suffix);
}
char* returnExt(const char *str){
// Returns the file extention
char* ext = strrchr(str,'.');
return ext;
}
int prepareName(ID3v2_Tag *tag, char* newName){
// Prepend track number to the beginning of the file name
ID3v2_TextFrame* track_frame = ID3v2_Tag_get_track_frame(tag);
ID3v2_TextFrame* title_frame = ID3v2_Tag_get_title_frame(tag);
for(int i = 0; i < strlen(track_frame->data->text); i++){
if(track_frame->data->text[i] == '/'){
newName[i] = '\0';
break;
}
newName[i] = track_frame->data->text[i];
}
strcat(newName," - ");
strcat(newName,title_frame->data->text);
return 0;
}
int readDir(char* dir){
// Read directory and prepend all track numbers to mp3 file names
const char* ext = ".mp3";
struct dirent **eps;
int n = scandir(dir, &eps, one, alphasort);
if(n >= 0){
for(int i = 0; i < n; i++){
if(checkSuffix(eps[i]->d_name,ext) == 0){
renameFile(eps[i]->d_name);
}
}
}
}
int renameFile(char *fPath){
// Rename the file using the prepareName()
ID3v2_Tag* tag = ID3v2_read_tag(fPath);
if(tag == NULL){
printf("File does not have a tag\n");
return -1;
}
char newName[PATH_MAX];
if(prepareName(tag, newName) != 0){
printf("Error preparing name\n");
}
strcat(newName,returnExt(fPath));
if(rename(fPath, newName) != 0){
perror("Error");
}
}
int printTag(char *fPath){
ID3v2_Tag* tag = ID3v2_read_tag(fPath);
if(tag == NULL){
printf("File does not have a tag\n");
return -1;
}
ID3v2_TextFrame* track_frame = ID3v2_Tag_get_track_frame(tag);
ID3v2_TextFrame* title_frame = ID3v2_Tag_get_title_frame(tag);
ID3v2_TextFrame* artist_frame = ID3v2_Tag_get_artist_frame(tag);
ID3v2_TextFrame* album_frame = ID3v2_Tag_get_album_frame(tag);
printf("Title: %s\n",title_frame->data->text);
printf("Track: %s\n",track_frame->data->text);
printf("Artist: %s\n",artist_frame->data->text);
printf("Album: %s\n",album_frame->data->text);
}
int main(int argc, char *argv[]){
if(argc<2){
printf("Options:\n");
printf("p: Prints file tag\n");
printf("r: Adds track number to the file name\n");
printf("r: Adds track number to all mp3 files in directory\n");
return -1;
}
int opt;
while((opt = getopt(argc, argv, "p:r:d:")) != -1)
{
switch(opt){
case 'r':
renameFile(optarg);
break;
case 'p':
printTag(optarg);
break;
case 'd':
readDir(optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
for(; optind < argc; optind++){
printf("extra arguments: %s\n", argv[optind]);
}
return 0;
}