From 90b72c3a3282241e608f8bf778003f8afc96fdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigrid=20Solveig=20Hafl=C3=ADnud=C3=B3ttir?= Date: Tue, 30 Aug 2022 18:09:32 +0000 Subject: [PATCH] libtags: try other formats after id3v2 is found Even though a valid id3v2 tag header may be present, the file format itself might not be mp3. Instead, retry other format parsers on the file after seeking right past the id3v2 header. This fixes FLACs that are ID3v2-tagged (the reasoning behind that is left with no comment). Also convert "TLEN" into duration while at it. --- flac.c | 2 +- id3v2.c | 4 ++++ tags.c | 3 ++- tags.h | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/flac.c b/flac.c index 7bbc193..824f6ab 100644 --- a/flac.c +++ b/flac.c @@ -25,7 +25,7 @@ tagflac(Tagctx *ctx) ctx->duration = g * 1000 / ctx->samplerate; /* skip the rest of the stream info */ - if(ctx->seek(ctx, sz-18, 1) != 8+sz) + if(ctx->seek(ctx, sz-18, 1) != ctx->restart+8+sz) return -1; for(last = 0; !last;){ diff --git a/id3v2.c b/id3v2.c index 1876ee5..f5784c2 100644 --- a/id3v2.c +++ b/id3v2.c @@ -27,6 +27,8 @@ v2cb(Tagctx *ctx, char *k, char *v) txtcb(ctx, Tdate, k-1, v); else if(strcmp(k, "RK") == 0 || strcmp(k, "RCK") == 0) txtcb(ctx, Ttrack, k-1, v); + else if(strcmp(k, "LEN") == 0) + ctx->duration = atoi(v); else if(strcmp(k, "CO") == 0 || strcmp(k, "CON") == 0){ for(; v[0]; v++){ if(v[0] == '(' && v[1] <= '9' && v[1] >= '0'){ @@ -388,6 +390,8 @@ header: if(ver == 2 && (d[5] & (1<<6)) != 0) /* compression */ return -1; + ctx->restart = sizeof(d)+sz; + if(ver > 2){ if((d[5] & (1<<4)) != 0) /* footer */ sz -= 10; diff --git a/tags.c b/tags.c index 44729a6..6c6b2b5 100644 --- a/tags.c +++ b/tags.c @@ -65,6 +65,7 @@ tagsget(Tagctx *ctx) ctx->channels = ctx->samplerate = ctx->bitrate = ctx->duration = 0; ctx->found = 0; ctx->format = Funknown; + ctx->restart = 0; res = -1; for(i = 0; i < nelem(g); i++){ ctx->num = 0; @@ -72,7 +73,7 @@ tagsget(Tagctx *ctx) ctx->format = g[i].format; res = 0; } - ctx->seek(ctx, 0, 0); + ctx->seek(ctx, ctx->restart, 0); } return res; diff --git a/tags.h b/tags.h index 7625b91..1feb10d 100644 --- a/tags.h +++ b/tags.h @@ -83,6 +83,7 @@ struct Tagctx /* Private, don't touch. */ int found; int num; + int restart; }; /* Parse the file using this function. Returns 0 on success. */ -- 2.38.5