disco-dl

Discography/album downloader
git clone git://git.wimdupont.com/disco-dl.git
Log | Files | Refs | README | LICENSE

commit 2a1fe493b66fec3376722dcabd125b9693862882
parent 64e45df5da0e2259f39ab77d52b083eddd8413ac
Author: Wim Dupont <wim@wimdupont.com>
Date:   Fri, 13 Jun 2025 19:44:31 +0200

id3 command to lib

Diffstat:
MMakefile | 11+++--------
MREADME.adoc | 3+--
Aconfig.mk | 11+++++++++++
Mdisco-dl.c | 166++++++++++++++++++++++++++-----------------------------------------------------
4 files changed, 70 insertions(+), 121 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,11 +1,4 @@ -CC = gcc - -NAME = disco-dl -VERSION = 0.1 - -# paths -PREFIX = /usr/local -MANPREFIX = ${PREFIX}/share/man +include config.mk BIN = disco-dl SRC = ${BIN:=.c} @@ -16,6 +9,8 @@ ${OBJ}: config.h all: ${BIN} +LDFLAGS=-lid3 + ${BIN}: ${@:=.o} .o: ${OBJ} diff --git a/README.adoc b/README.adoc @@ -13,5 +13,4 @@ $ make clean install * https://github.com/yt-dlp/yt-dlp[yt-dlp] * https://git.ffmpeg.org/ffmpeg.git[ffmpeg] -* https://github.com/squell/id3[id3] - +* https://id3lib.sourceforge.net/[id3lib] diff --git a/config.mk b/config.mk @@ -0,0 +1,11 @@ +NAME = disco-dl + +VERSION = 0.1 + +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" +CFLAGS = -std=c99 -pedantic -Wall -Wextra -I/usr/local/include ${CPPFLAGS} + +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man + +CC = gcc diff --git a/disco-dl.c b/disco-dl.c @@ -3,10 +3,11 @@ #include <errno.h> #include <stdlib.h> #include <sys/stat.h> -#include <stdio.h> +#include <stdio.h> #include <string.h> #include <libgen.h> #include <fts.h> +#include <id3.h> #include "config.h" @@ -32,16 +33,14 @@ typedef struct } Track; static int make_dir(const char *name); -static char *concat(const char *s1, const char *s2); -static Track *get_track(Album **album, char **track_name, unsigned int count); static char *make_message(const char *str, ...); +static Track *get_track(Album **album, char **track_name, int count); static Album **get_albums(int *line_count); static Album *get_album(char *line_buf, ssize_t line_size); static void tag_album(Album *album); -static void dl_album(Album *album); +static void set_tag(ID3Tag *tag, ID3_FrameID frameId, char *value); static void id3_tag(Track *track); -static void tag(char *tag, char *value, FILE **f1); -static void merge_file(char *prefile1, char *file2); +static void dl_album(Album *album); static void convert(Track *track); static void fatal(const char *fmt, ...); @@ -90,8 +89,9 @@ dl_album(Album *album) album->dir = albumdir; - - sys_command = make_message("yt-dlp -x -f bestaudio -i -o \"%s/%(playlist_index)s - %(title)s.%(ext)s\" \"%s\"", albumdir, album->url); + sys_command = make_message( + "yt-dlp -x -f bestaudio -i -o \"%s/%(playlist_index)s - %(title)s.%(ext)s\" \"%s\"", + albumdir, album->url); system(sys_command); free(sys_command); @@ -104,7 +104,7 @@ tag_album(Album *album) { char *token; char *tracklist; - unsigned int count = 0; + int count = 0; Track *track; if (album->tracklist && *album->tracklist != '\0') { @@ -115,9 +115,7 @@ tag_album(Album *album) if (track->title != NULL) { printf("%s - %s\n", track->tracknum, track->title); convert(track); - /* TODO: fix tagging implementation and remove from convert() id3_tag(track); - */ } free(track->path); free(track->tracknum); @@ -131,7 +129,7 @@ tag_album(Album *album) } Track * -get_track(Album **album, char **track_name, unsigned int count) +get_track(Album **album, char **track_name, int count) { char *filename; char *tracknumber; @@ -205,19 +203,8 @@ convert(Track *track){ free(track->path); track->path = strdup(path); - sys_command = make_message("id3 -t '%s' -a '%s' -l '%s' -y '%d' -n '%s' -g '%s' '%s'", - track->title, - track->album->band, - track->album->album, - track->album->year, - track->tracknum, - track->album->genre, - track->path); - system(sys_command); - free(path); free(base); - free(sys_command); } Album ** @@ -226,23 +213,22 @@ get_albums(int *line_count) char *line_buf = NULL; size_t line_buf_size = 0; ssize_t line_size; - FILE *fp = fopen(FILENAME, "r"); - Album **albums = NULL; + FILE *fp; + Album **albums; - if (!fp) + if ((fp = fopen(FILENAME, "r")) == NULL) fatal("Error opening file '%s'\n", FILENAME); line_size = getline(&line_buf, &line_buf_size, fp); - albums = (Album**) malloc(sizeof(Album)); - - if (albums == NULL) + + if ((albums = (Album**) malloc(sizeof(Album))) == NULL) fatal("Fatal: failed to allocate bytes for albums.\n"); if (line_size <= 0) fatal("File '%s' is empty\n", FILENAME); while (line_size >= 0) { - albums = (Album**) realloc(albums, (sizeof(Album) * (*line_count + 1))); + albums = (Album**) realloc(albums, (sizeof(Album) * (*line_count + 1))); if (albums == NULL) fatal("Fatal: failed to reallocate bytes for albums.\n"); @@ -271,7 +257,11 @@ get_album(char *line_buf, ssize_t line_size) album->url = (char*) malloc(line_size); album->tracklist = (char*) malloc(line_size); - if (album->band == NULL | album->album == NULL | album->genre == NULL | album->url == NULL | album->tracklist == NULL) + if (album->band == NULL + || album->album == NULL + || album->genre == NULL + || album->url == NULL + || album->tracklist == NULL) fatal("Fatal: failed to allocate bytes for album.\n"); sscanf(line_buf, @@ -296,13 +286,16 @@ make_dir(const char * name) switch (errno) { case EACCES: fatal("The parent directory does not allow write: %s\n", name); + break; case EEXIST: status = 1; break; case ENAMETOOLONG: fatal("Pathname is too long: %s\n", name); + break; default: fatal("mkdir error: %s\n", name); + break; } } @@ -357,90 +350,42 @@ make_message(const char *fmt, ...) void id3_tag(Track *track) { - char *tagfile = "taginf.txt"; - char yearstr[5]; - unsigned char pad[7] = { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76 }; - - int len = snprintf(yearstr, 5, "%d", track->album->year); - FILE* fp; - fp = fopen(tagfile, "wb"); - if (fp == NULL) - fatal("Failed to open file %s\n", tagfile); - - fprintf(fp, "ID3"); - fwrite(pad, sizeof(pad), 1, fp); - - tag("TRCK", track->tracknum, &fp); - tag("TIT2", track->title, &fp); - tag("TYER", yearstr, &fp); - tag("TPE1", track->album->band, &fp); - tag("TALB", track->album->album, &fp); - tag("TCON", track->album->genre, &fp); - - fclose(fp); - merge_file(tagfile, track->path); -} + ID3Tag *tag; + char yearstr[5]; -void tag -(char *tag, char *value, FILE **f1) -{ - int size = 0; - unsigned char pad[3] = { 0x00, 0x00, 0x00 }; - - fprintf(*f1, tag); - fwrite(pad, sizeof(pad), 1, *f1); - size = strlen(value) + 1; - fprintf(*f1, "%c", size); - fwrite(pad, sizeof(pad), 1, *f1); - fprintf(*f1, "%s", value); + if ((tag = ID3Tag_New()) != NULL) { + snprintf(yearstr, 5, "%d", track->album->year); + + (void) ID3Tag_Link(tag, track->path); + + set_tag(tag, ID3FID_TITLE, track->title); + set_tag(tag, ID3FID_YEAR, yearstr); + set_tag(tag, ID3FID_LEADARTIST, track->album->band); + set_tag(tag, ID3FID_ALBUM, track->album->album); + set_tag(tag, ID3FID_CONTENTTYPE, track->album->genre); + set_tag(tag, ID3FID_TRACKNUM, track->tracknum); + + ID3Tag_Update(tag); + } } void -merge_file(char *prefile, char *file) -{ - FILE *f1, *f2, *f3; - char *tmp = "tmp.txt"; - int i = 0; - int ch; - f1 = fopen(prefile, "rb"); - f2 = fopen(file, "rb"); - f3 = fopen(tmp, "wb"); - - if (f1 == NULL) - fatal("Failed to open file %s\n", prefile); - if (f2 == NULL) - fatal("Failed to open file %s\n", file); - if (f3 == NULL) - fatal("Failed to open file %s\n", tmp); - - /* - while ((ch = fgetc(f2)) != EOF && ++i <= 34) - fputc(ch, f3); - - while ((ch = fgetc(f1)) != EOF) - fputc(ch, f3); - - i = 0; - while ((ch = fgetc(f2)) != EOF) { - if (++i > 34) - fputc(ch, f3); +set_tag(ID3Tag *tag, ID3_FrameID frameId, char *value) +{ + ID3Frame *frame; + ID3Field *field; + + if ((frame = ID3Tag_FindFrameWithID(tag, frameId)) != NULL) { + field = ID3Frame_GetField(frame, ID3FN_TEXT); + ID3Field_SetASCII(field, value); + } else { + frame = ID3Frame_New(); + ID3Frame_SetID(frame, frameId); + field = ID3Frame_GetField(frame, ID3FN_TEXT); + ID3Field_SetASCII(field, value); + ID3Tag_AddFrame(tag, frame); } - */ - - while ((ch = fgetc(f1)) != EOF) - fputc(ch, f3); - - while ((ch = fgetc(f2)) != EOF) - fputc(ch, f3); - - fclose(f1); - fclose(f2); - fclose(f3); - - rename(tmp, file); - remove(prefile); - remove(tmp); -} +} void fatal(const char *fmt, ...) @@ -453,4 +398,3 @@ fatal(const char *fmt, ...) exit(EXIT_FAILURE); } -