110 lines
3 KiB
C++
110 lines
3 KiB
C++
#include <iostream>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <regex>
|
|
|
|
#include <tag.h>
|
|
#include <fileref.h>
|
|
#include <id3v1tag.h>
|
|
#include <id3v2tag.h>
|
|
|
|
#ifndef VERSION
|
|
#define VERSION "0.1"
|
|
#endif
|
|
|
|
void help() {
|
|
std::cout <<
|
|
"mcopy " << VERSION << "\n\n" <<
|
|
"mcopy -f, --format Specify a format to output files using\n" <<
|
|
"mcopy -a, --ask Ask the user if metadata cannot be retrieved from file\n" <<
|
|
"mcopy -na, --no-ask Don't ask if metadata cannot be retrieved from file\n" <<
|
|
"mcopy -h, --help Display help\n" <<
|
|
"mcopy -v, --version Display version\n" <<
|
|
"\n" <<
|
|
"Example: mcopy --format \"/home/anon/Music/Albums/@A/@a/@n. @A - @t.flac" "~/Downloads/myflac1.flac" "~/Downloads/myflac2.flac\n" <<
|
|
"\n" <<
|
|
"Formats:\n" <<
|
|
"\n" <<
|
|
"@A Artist\n" <<
|
|
"@a Album name\n" <<
|
|
"@t Title\n" <<
|
|
"@n Track number\n";
|
|
}
|
|
|
|
TagLib::FileRef get_file(std::string str) {
|
|
const char *cstr = str.c_str();
|
|
TagLib::FileName fn(cstr);
|
|
TagLib::FileRef file(fn);
|
|
|
|
return file;
|
|
}
|
|
|
|
void getMetadataFromFile(std::string format, std::string str) {
|
|
TagLib::FileRef file = get_file(str);
|
|
TagLib::Tag *tag = file.tag();
|
|
|
|
if (file.isNull() || !file.audioProperties()) {
|
|
return;
|
|
}
|
|
|
|
std::string title = tag->title().to8Bit(true);
|
|
std::string album = tag->album().to8Bit(true);
|
|
std::string artist = tag->artist().to8Bit(true);
|
|
std::string track = std::to_string(tag->track());
|
|
|
|
std::string filename = format;
|
|
|
|
// TODO: Replace this junk
|
|
filename = std::regex_replace(format, std::regex("@t"), title);
|
|
filename = std::regex_replace(filename, std::regex("@a"), album);
|
|
filename = std::regex_replace(filename, std::regex("@A"), artist);
|
|
filename = std::regex_replace(filename, std::regex("@n"), track);
|
|
|
|
std::filesystem::path fs;
|
|
std::string dir = (fs = filename).remove_filename();
|
|
std::filesystem::create_directories(dir);
|
|
|
|
if (!std::filesystem::copy_file(str, filename)) {
|
|
std::cout << "mcopy: Failed to copy file " << str << " to " << filename << "\n";
|
|
exit(1);
|
|
} else {
|
|
std::cout << "mcopy: Copied file " << str << " to " << filename << "\n";
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int ac = argc;
|
|
char **av = argv;
|
|
|
|
std::string format = "";
|
|
|
|
for (int i{1}; i < argc; i++) {
|
|
std::string arg = argv[i];
|
|
|
|
if (!arg.compare("-h")) {
|
|
help();
|
|
exit(0);
|
|
} else if (!arg.compare("-v")) {
|
|
std::cout << "mcopy " << VERSION << '\n';
|
|
exit(0);
|
|
} else if (!arg.compare("-f") && i < argc) {
|
|
format = argv[++i];
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (!format.compare("")) {
|
|
std::cout << "You must specify a format.\n";
|
|
exit(1);
|
|
}
|
|
|
|
for (int i{1}; i < ac; i++) {
|
|
std::ifstream f(av[i]);
|
|
|
|
if (static_cast<bool>(f.good())) {
|
|
getMetadataFromFile(format, av[i]);
|
|
}
|
|
}
|
|
}
|