Skip to content

Instantly share code, notes, and snippets.

@jprjr
Created July 31, 2025 15:42
Show Gist options
  • Save jprjr/a180924a126936ec7c49ca97e8f7b260 to your computer and use it in GitHub Desktop.
Save jprjr/a180924a126936ec7c49ca97e8f7b260 to your computer and use it in GitHub Desktop.
Extract raw ID3 mpg123
/* SPDX-License-Identifier: 0BSD
minimum demo program to save the "raw" id3 data
from mpg123 into a separate file.
Compile with something like:
cc -o extract extract.c $(pkg-config --cflags --libs libmpg123)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpg123.h>
int main(int argc, const char* argv[]) {
unsigned char* id3_data;
size_t id3_size;
mpg123_handle* mh;
long rate;
int err, channels, encoding;
char* filename;
FILE* out;
if(argc < 2) {
fprintf(stderr,"Usage: %s /path/to/file.mp3\n", argv[0]);
return 1;
}
mpg123_init();
mh = mpg123_new(NULL,NULL);
if(mh == NULL) return 1;
mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_STORE_RAW_ID3, 0);
err = mpg123_open(mh, argv[1]);
if(err != MPG123_OK) {
fprintf(stderr,"open failure\n");
return 1;
}
err = mpg123_getformat(mh, &rate, &channels, &encoding);
if(err != MPG123_OK) {
fprintf(stderr,"getformat failure\n");
return 1;
}
if(mpg123_id3_raw(mh, NULL, NULL, &id3_data, &id3_size) != MPG123_OK) {
fprintf(stderr,"id3_raw failure\n");
return 1;
}
if(id3_data == NULL || id3_size == 0) {
fprintf(stderr,"id3_data is null or size is 0\n");
}
filename = malloc(strlen(argv[1]) + strlen(".id3") + 1);
filename[0] = '\0';
strcpy(filename,argv[1]);
strcat(filename,".id3");
out = fopen(filename,"wb");
if(out == NULL) return 1;
fwrite(id3_data,1,id3_size,out);
fclose(out);
fprintf(stderr,"wrote %lu bytes to %s\n", (long unsigned) id3_size, filename);
return 0;
}
/*
Zero-Clause BSD
=============
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment