feat: Remove unicode characters, only process audio files, removed some unwrap
This commit is contained in:
parent
74cc35ee2e
commit
21802286a5
1 changed files with 46 additions and 12 deletions
58
src/main.rs
58
src/main.rs
|
@ -10,6 +10,8 @@ use std::{
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use deunicode::AsciiChars;
|
||||||
|
use file_format::{FileFormat, Kind};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use zerocopy::IntoBytes;
|
use zerocopy::IntoBytes;
|
||||||
|
|
||||||
|
@ -25,6 +27,8 @@ enum Errors {
|
||||||
FromUtf8(#[from] FromUtf8Error),
|
FromUtf8(#[from] FromUtf8Error),
|
||||||
#[error("Skill issue on the programmer part ngl, report this to dev pls")]
|
#[error("Skill issue on the programmer part ngl, report this to dev pls")]
|
||||||
SkillIssue(),
|
SkillIssue(),
|
||||||
|
#[error("Didn't find any file to convert, is your input folder structued correctly?")]
|
||||||
|
NoFileToConvert(),
|
||||||
|
|
||||||
#[error("You are missing ffprobe in your PATH")]
|
#[error("You are missing ffprobe in your PATH")]
|
||||||
MissingFfprobe(#[source] std::io::Error),
|
MissingFfprobe(#[source] std::io::Error),
|
||||||
|
@ -50,7 +54,8 @@ fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"⠀⠀⠀⠂⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
"
|
||||||
|
⠀⠀⠀⠂⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
⠀⠀⠀⠀⠈⠲⣥⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⠖⠁⠀⠀⠀⠀
|
⠀⠀⠀⠀⠈⠲⣥⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⠖⠁⠀⠀⠀⠀
|
||||||
⠀⠀⠀⠀⠀⠀⠘⠿⣿⣷⣦⣀⡀⠀⠀⢀⣠⣴⣾⣿⠟⠀⠀⠀⠀⠀⠀⠀
|
⠀⠀⠀⠀⠀⠀⠘⠿⣿⣷⣦⣀⡀⠀⠀⢀⣠⣴⣾⣿⠟⠀⠀⠀⠀⠀⠀⠀
|
||||||
⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀
|
⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
|
@ -91,6 +96,7 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
for (i, soundtrack_dirs) in music_directory.enumerate() {
|
for (i, soundtrack_dirs) in music_directory.enumerate() {
|
||||||
let soundtrack = soundtrack_dirs.map_err(Errors::UnknownFolder)?.path();
|
let soundtrack = soundtrack_dirs.map_err(Errors::UnknownFolder)?.path();
|
||||||
|
|
||||||
|
// Ignore non folders for soundtracks
|
||||||
if !soundtrack.is_dir() {
|
if !soundtrack.is_dir() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -98,12 +104,16 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
soundtrack_count += 1;
|
soundtrack_count += 1;
|
||||||
song_group_id = 0;
|
song_group_id = 0;
|
||||||
|
|
||||||
// Convert the folder name into 2 bytes
|
let soundtrack_name_str = soundtrack
|
||||||
let mut soundtrack_name = soundtrack
|
|
||||||
.file_name()
|
.file_name()
|
||||||
.map_or(OsStr::new("Unknown soundtrack"), |f| f)
|
.map_or(OsStr::new("Unknown soundtrack"), |f| f)
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.trim()
|
.trim()
|
||||||
|
.ascii_chars()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
// Convert the folder name into 2 bytes
|
||||||
|
let mut soundtrack_name = soundtrack_name_str
|
||||||
.bytes()
|
.bytes()
|
||||||
.map(|b| [b, 0])
|
.map(|b| [b, 0])
|
||||||
.collect::<Vec<[u8; 2]>>();
|
.collect::<Vec<[u8; 2]>>();
|
||||||
|
@ -126,14 +136,29 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
let mut char_count = 0;
|
let mut char_count = 0;
|
||||||
song_time_miliseconds = [0; 6];
|
song_time_miliseconds = [0; 6];
|
||||||
|
|
||||||
song_files.iter().enumerate().for_each(|(g, f)| {
|
for (g, f) in song_files.iter().enumerate() {
|
||||||
let song = f.as_ref().unwrap();
|
let song = f.as_ref().unwrap();
|
||||||
|
let song_path = song.path();
|
||||||
|
|
||||||
song_id[g] = total_songs_count as i32;
|
// Get file format kind for the files inside soundtracks
|
||||||
song_time_miliseconds[g] = match get_duration(song.path()) {
|
let format = match FileFormat::from_file(&song_path).map_err(Errors::UnknownIO) {
|
||||||
Ok(s) => s,
|
Ok(f) => f.kind(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("\x1b[0;31m{}\x1b[0;20m", e);
|
eprintln!("\x1b[0;31m{}\x1b[0;20m", e);
|
||||||
|
Kind::Other
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Ignore non audio kind
|
||||||
|
if format != Kind::Audio {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
song_id[g] = total_songs_count as i32;
|
||||||
|
song_time_miliseconds[g] = match get_duration(song_path) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("\x1b[0;31mFailed to get duration: {}\x1b[0;20m", e);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -145,7 +170,9 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
let filename = filepath
|
let filename = filepath
|
||||||
.file_stem()
|
.file_stem()
|
||||||
.map_or(OsStr::new("Unknown track"), |f| f)
|
.map_or(OsStr::new("Unknown track"), |f| f)
|
||||||
.to_string_lossy();
|
.to_string_lossy()
|
||||||
|
.ascii_chars()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
let mut name = filename.trim().bytes().collect::<Vec<u8>>();
|
let mut name = filename.trim().bytes().collect::<Vec<u8>>();
|
||||||
name.resize(32, 0);
|
name.resize(32, 0);
|
||||||
|
@ -157,10 +184,11 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
|
|
||||||
files_to_convert.push(MusicFile {
|
files_to_convert.push(MusicFile {
|
||||||
path: song.path(),
|
path: song.path(),
|
||||||
|
soundtrack_name: soundtrack_name_str.clone(),
|
||||||
soundtrack_index: 0,
|
soundtrack_index: 0,
|
||||||
index: total_songs_count - 1,
|
index: total_songs_count - 1,
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
let s = Song {
|
let s = Song {
|
||||||
magic: 200819,
|
magic: 200819,
|
||||||
|
@ -223,6 +251,10 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
padding: [char::MIN; 24],
|
padding: [char::MIN; 24],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if files_to_convert.len() == 0 {
|
||||||
|
return Err(Errors::NoFileToConvert());
|
||||||
|
}
|
||||||
|
|
||||||
write_database(&args.output, header, soundtracks, songs)?;
|
write_database(&args.output, header, soundtracks, songs)?;
|
||||||
|
|
||||||
for f in files_to_convert {
|
for f in files_to_convert {
|
||||||
|
@ -245,16 +277,17 @@ fn process(args: &Args) -> Result<(), Errors> {
|
||||||
);
|
);
|
||||||
|
|
||||||
print!(
|
print!(
|
||||||
"{}\r{}Processing {} ",
|
"{}\r{}Processing {} - {}",
|
||||||
"\x1B[1B",
|
"\x1B[1B",
|
||||||
"\x1B[K",
|
"\x1B[K",
|
||||||
|
f.soundtrack_name,
|
||||||
f.path
|
f.path
|
||||||
.file_stem()
|
.file_stem()
|
||||||
.map_or(OsStr::new("Unknown track"), |f| f)
|
.map_or(OsStr::new("Unknown track"), |f| f)
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = stdout().flush().map_err(Errors::UnknownIO);
|
stdout().flush().map_err(Errors::UnknownIO)?;
|
||||||
|
|
||||||
convert_to_wma(
|
convert_to_wma(
|
||||||
f.path,
|
f.path,
|
||||||
|
@ -300,7 +333,8 @@ fn convert_to_wma(
|
||||||
let binding = input.into_os_string();
|
let binding = input.into_os_string();
|
||||||
let input = binding.to_str().unwrap();
|
let input = binding.to_str().unwrap();
|
||||||
|
|
||||||
fs::create_dir_all(format!("{}/{:0>4}", output, soundtrack_index)).unwrap();
|
fs::create_dir_all(format!("{}/{:0>4}", output, soundtrack_index))
|
||||||
|
.map_err(Errors::UnknownIO)?;
|
||||||
|
|
||||||
Command::new("ffmpeg")
|
Command::new("ffmpeg")
|
||||||
.args([
|
.args([
|
||||||
|
|
Loading…
Add table
Reference in a new issue