Compare commits

..

3 commits

4 changed files with 66 additions and 20 deletions

16
Cargo.lock generated
View file

@ -98,6 +98,18 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "deunicode"
version = "1.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abd57806937c9cc163efc8ea3910e00a62e2aeb0b8119f1793a978088f8f6b04"
[[package]]
name = "file-format"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ef3d5e8ae27277c8285ac43ed153158178ef0f79567f32024ca8140a0c7cd8"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
@ -258,9 +270,11 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]] [[package]]
name = "xbst" name = "xbst"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"clap", "clap",
"deunicode",
"file-format",
"thiserror", "thiserror",
"zerocopy", "zerocopy",
] ]

View file

@ -1,9 +1,11 @@
[package] [package]
name = "xbst" name = "xbst"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
thiserror = "2.0.12" thiserror = "2.0.12"
clap = { version = "4.5.31", features = ["derive"] } clap = { version = "4.5.31", features = ["derive"] }
zerocopy = { version = "0.8.26", features = ["derive"] } zerocopy = { version = "0.8.26", features = ["derive"] }
deunicode = "1.6.2"
file-format = "0.26.0"

View file

@ -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([

View file

@ -5,15 +5,11 @@ use zerocopy::TryFromBytes;
pub struct MusicFile { pub struct MusicFile {
pub path: PathBuf, pub path: PathBuf,
pub soundtrack_index: u32, pub soundtrack_index: u32,
pub soundtrack_name: String,
pub index: u32, pub index: u32,
} }
#[derive(Debug, TryFromBytes)] // https://xboxdevwiki.net/Soundtracks#ST.DB
#[repr(C)]
pub struct Database {
pub header: Header,
pub soundtrack: Soundtrack,
}
#[derive(Debug, TryFromBytes)] #[derive(Debug, TryFromBytes)]
#[repr(C)] #[repr(C)]