Added Cover renaming

This commit is contained in:
DWW 2022-07-20 16:52:52 +03:00
parent eaf8bcdfc0
commit cfe37f20a5
3 changed files with 249 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ login.json
release.json release.json
cookies.txt cookies.txt
mcat_dl* mcat_dl*
.vscode/*

3
TODO
View File

@ -1,5 +1,4 @@
Add Cover Resize via stb - https://github.com/nothings/stb Add Cover Resize via lib or command ???
Add Cover rename to match extension
Add MP3 Tagging Add MP3 Tagging
Add FLAC Tagging Add FLAC Tagging
Implement main Implement main

257
main.cpp
View File

@ -1,11 +1,13 @@
// C++ Libs // C++ Libs
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <stdexcept>
#include <string> #include <string>
#include <vector> #include <vector>
#include <utility> #include <utility>
// C Libs // C Libs
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
// File Libs // File Libs
@ -21,6 +23,9 @@ struct tag
constexpr const char RELEASE_JSON[] = "release.json"; constexpr const char RELEASE_JSON[] = "release.json";
constexpr bool IS_MP3 = true; constexpr bool IS_MP3 = true;
constexpr bool IS_FLAC = false; constexpr bool IS_FLAC = false;
constexpr bool IS_JPG = true;
constexpr bool IS_PNG = false;
constexpr uint32_t COVER_SIZE = 750;
#ifdef _WIN32 #ifdef _WIN32
constexpr const char FOLDER_DELIM = '\\'; constexpr const char FOLDER_DELIM = '\\';
@ -63,12 +68,6 @@ void usage(const string& name)
cout << " Default: ." << endl; cout << " Default: ." << endl;
} }
// string get_track_name(const json& info, int track_num)
// {
// return "";
// }
bool system_command(const string& cmd) bool system_command(const string& cmd)
{ {
int result = system(cmd.c_str()); int result = system(cmd.c_str());
@ -350,6 +349,229 @@ string get_track_filename(int track_num, const string& artist,
return filename; return filename;
} }
bool should_resize_JPG(ifstream& file)
{
// Do a resize by default
bool result = true;
int marker = 0;
int symbol;
// Check Header 1
symbol = file.get();
if (symbol != 0xFF)
{
return result;
}
// Check Header 2
symbol = file.get();
if (symbol != 0xD8)
{
return result;
}
while(1)
{
int discarded_bytes = 0;
if (file.eof())
{
return result;
}
marker = file.get();
// Seek to 0xFF
while (marker != 0xFF)
{
discarded_bytes++;
if (file.eof())
{
return result;
}
marker = file.get();
}
// Find end of 0xFF
do
{
if (file.eof())
{
return result;
}
marker = file.get();
}
while (marker == 0xFF);
// ???
if (0 == discarded_bytes)
{
return result;
}
switch(marker)
{
// Read width & height
case 0xC0:
case 0xC1:
case 0xC2:
case 0xC3:
case 0xC5:
case 0xC6:
case 0xC7:
case 0xC9:
case 0xCA:
case 0xCB:
case 0xCD:
case 0xCE:
case 0xCF:
int16_t width;
int16_t height;
// Skip 3 bytes
file.get();
file.get();
file.get();
file.read(reinterpret_cast<char*>(&height), 2);
file.read(reinterpret_cast<char*>(&width), 2);
if ((width > COVER_SIZE) || (height > COVER_SIZE))
{
return true;
}
else
{
return false;
}
break;
// ???
case 0xDA:
case 0xD9:
return result;
break;
// Skip length-2 bytes
default:
int16_t length;
file.read(reinterpret_cast<char*>(&length), 2);
if (length < 2)
{
return 0;
}
length -= 2;
for (int i = 0; i < length; ++i)
{
file.get();
}
if (file.eof())
{
return result;
}
break;
}
}
}
bool should_resize_PNG(ifstream& file)
{
uint8_t buf[4];
uint32_t width;
uint32_t height;
// Seek
file.read(reinterpret_cast<char*>(&buf), 4);
file.read(reinterpret_cast<char*>(&buf), 4);
file.read(reinterpret_cast<char*>(&buf), 4);
file.read(reinterpret_cast<char*>(&buf), 4);
// Read width
width = 0;
file.read(reinterpret_cast<char*>(&buf), 4);
for (int i = 0; i < 4; ++i)
{
width <<= 8;
width += buf[i];
}
// Read height
height = 0;
file.read(reinterpret_cast<char*>(&buf), 4);
for (int i = 0; i < 4; ++i)
{
height <<= 8;
height += buf[i];
}
if ((width > COVER_SIZE) || (height > COVER_SIZE))
{
return true;
}
else
{
return false;
}
}
bool get_cover_type(const string& path)
{
string filename;
ifstream file;
int header;
filename = path;
filename += FOLDER_DELIM;
filename += "Cover";
file.open(filename);
header = file.peek();
switch (header)
{
// JPG
case 0xFF:
return IS_JPG;
break;
case 0x89:
return IS_PNG;
break;
}
throw invalid_argument(filename);
}
bool rename_cover(const string& path, bool is_jpg)
{
string filename;
string filename_new;
int error;
filename = path;
filename += FOLDER_DELIM;
filename += "Cover";
filename_new = filename;
if (is_jpg == IS_JPG)
{
filename_new += ".jpg";
}
else
{
filename_new += ".png";
}
error = rename(filename.c_str(), filename_new.c_str());
if (0 != error)
{
return false;
}
return true;
}
bool full_donwload(const string& path, const string& release_prefix, bool full_donwload(const string& path, const string& release_prefix,
int release_num, const string& release_suffix) int release_num, const string& release_suffix)
{ {
@ -361,6 +583,7 @@ bool full_donwload(const string& path, const string& release_prefix,
pair<string, string> release_artist_title; pair<string, string> release_artist_title;
int release_precision; int release_precision;
bool is_single_dir; bool is_single_dir;
bool cover_is_jpg;
vector<tag> tags; vector<tag> tags;
catalog_release_str = release_prefix; catalog_release_str = release_prefix;
@ -416,9 +639,7 @@ bool full_donwload(const string& path, const string& release_prefix,
tmp += FOLDER_DELIM; tmp += FOLDER_DELIM;
tmp += "FLAC"; tmp += "FLAC";
ok = make_dir(tmp); ok = make_dir(tmp);--dont-use-padding
if (!ok)
{
return false; return false;
} }
} }
@ -430,8 +651,24 @@ bool full_donwload(const string& path, const string& release_prefix,
return false; return false;
} }
// Try to find out Cover type
try
{
cover_is_jpg = get_cover_type(release_dir);
}
catch (const std::exception& e)
{
return false;
}
// TODO: Make 750x750 Cover JPEG Image // TODO: Make 750x750 Cover JPEG Image
// TODO: Rename Cover Image (Proper Extension)
// Rename Cover Image (Proper Extension)
ok = rename_cover(release_dir, cover_is_jpg);
if (!ok)
{
return false;
}
tags = get_tags_from_release(info); tags = get_tags_from_release(info);
for (int i = 0; i < info["Tracks"].size(); ++i) for (int i = 0; i < info["Tracks"].size(); ++i)