Last active
March 31, 2020 16:46
-
-
Save tripulse/2f9326ec3ac711310319e22cf7d74b0d to your computer and use it in GitHub Desktop.
C++ program to strip out ID3v2 tag from beginning of a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstdint> | |
#include <cstring> | |
#include <boost/program_options.hpp> | |
uint32_t get_ID3v2_size(uint8_t rawhdr[10]) { | |
if(memcmp(rawhdr, "ID3", 3) != 0) | |
throw "ID3v2 magic numbers not found!"; | |
return rawhdr[6] << 21 | | |
rawhdr[7] << 14 | | |
rawhdr[8] << 7 | | |
rawhdr[9]; } | |
int main(int argc, char** argv) { | |
boost::program_options::options_description prog_argp("strips down ID3v2 tags from any file"); | |
boost::program_options::variables_map prog_opts; | |
prog_argp.add_options() | |
("help,h", "show this help message") | |
("input,i", boost::program_options::value<std::string>(), "input file containing ID3v2 tag") | |
("output,o", boost::program_options::value<std::string>(), "output file to put remaining data"); | |
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, prog_argp), prog_opts); | |
boost::program_options::notify(prog_opts); | |
if(prog_opts.count("help") || argc < 2) | |
std::cerr << prog_argp << std::endl; | |
/* iostreams for input and output files */ | |
std::fstream in_id3v2 (prog_opts["input"] .as<std::string>(), std::ios::in | std::ios::binary); | |
std::fstream out_stripped(prog_opts["output"].as<std::string>(), std::ios::out | std::ios::binary); | |
struct { | |
uint8_t raw[10]; | |
uint32_ size; | |
} id3v2_header; | |
in_id3v2.read(reinterpret_cast<char*>(id3v2_header.raw), 10); | |
try { id3v2_header.size = get_ID3v2_size(id3v2_header.raw); } | |
catch(const char* id3v2_err) { std::cerr << id3v2_err << std::endl; } | |
/* write all non ID3v2 data to the output file by | |
using a chunk of 16384 bytes for reading and writing. */ | |
in_id3v2.seekg(in_id3v2.tellg() + id3v2_header.size); | |
out_stripped << in_id3v2.rdbuf(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependencies
Building
# linux / mingw / cygwin $ g++ -o id3strip id3strip.cc -lboost_program_options