Compare commits

..

No commits in common. "master" and "0.1.1" have entirely different histories.

5 changed files with 238 additions and 362 deletions

9
Cargo.lock generated
View file

@ -104,6 +104,12 @@ 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]]
name = "heck"
version = "0.5.0"
@ -264,10 +270,11 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "xbst"
version = "0.2.0"
version = "0.1.1"
dependencies = [
"clap",
"deunicode",
"file-format",
"thiserror",
"zerocopy",
]

View file

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

View file

@ -26,9 +26,7 @@ The input music folder needs the following structure:
└ 💾 Music 1
```
Directory/File names must not exceed 31~ characters.
If you encounter issues with song not playing you could try to change the codec to wmav1 or lower the bitrate.
Directory/File names must not exceed 31 characters.
```
Usage: xbst [OPTIONS] [INPUT] [OUTPUT]
@ -38,16 +36,14 @@ Arguments:
[OUTPUT] Output folder for the database and converted musics [default: ./output]
Options:
-b, --bitrate <BITRATE> Bitrate for the output [default: 128]
-c, --codec <CODEC> Codec to use for conversion [default: wmav2] [possible values: wmav1, wmav2]
-b, --bitrate <BITRATE> Bitrate for the output [default: 192]
-h, --help Print help
-V, --version Print version
```
## Known issues
- The progress bar on soundtrack other than the first one doesn't progress.
- The progress bar on soundtrack other than the first one doesn't progress
- Some files, once converted, are quieter than usual?
- When using wmav1, some audio files might sounds absolute ass.
- Untested with a large library, probably has issues?
- Code is still poo poo but a bit better
- Code is poo poo

View file

@ -1,8 +1,8 @@
use std::{
ffi::OsStr,
fs::{self, read_dir, DirEntry, File},
fs::{self, read_dir, File},
io::{stdout, Write},
path::{Path, PathBuf},
path::PathBuf,
process::Command,
string::FromUtf8Error,
};
@ -11,10 +11,11 @@ mod utils;
use clap::Parser;
use deunicode::AsciiChars;
use file_format::{FileFormat, Kind};
use thiserror::Error;
use zerocopy::IntoBytes;
use crate::utils::{Ansi, Codec, Header, Song, SongGroup, Soundtrack};
use crate::utils::{Header, MusicFile, Song, Soundtrack};
#[derive(Error, Debug)]
enum Errors {
@ -26,7 +27,7 @@ enum Errors {
FromUtf8(#[from] FromUtf8Error),
#[error("Skill issue on the programmer part ngl, report this to dev pls")]
SkillIssue(),
#[error("Didn't find any file to convert, is your input folder structured correctly?")]
#[error("Didn't find any file to convert, is your input folder structued correctly?")]
NoFileToConvert(),
#[error("You are missing ffprobe in your PATH")]
@ -45,12 +46,8 @@ struct Args {
#[arg(default_value = "./output")]
output: String,
/// Bitrate for the output
#[arg(short, long, default_value_t = 128)]
#[arg(short, long, default_value_t = 192)]
bitrate: i16,
/// Codec to use for conversion
#[clap(value_enum)]
#[arg(short, long, default_value_t = Codec::Wmav2)]
codec: Codec,
}
fn main() {
@ -79,16 +76,21 @@ fn main() {
}
fn process(args: &Args) -> Result<(), Errors> {
let mut soundtrack_count = 0;
let mut soundtrack_count: i32 = 0;
let mut songs_count: u32 = 0;
let mut total_songs_count: u32 = 0;
let mut total_song_groups_count: i32 = 0;
let mut song_time_miliseconds: [i32; 6] = [0; 6];
let mut song_group_id: i32;
let mut soundtracks: Vec<Soundtrack> = Default::default();
let mut songs: Vec<Song> = Default::default();
let mut sound_groups_ids: Vec<i32> = Vec::with_capacity(84);
let mut files_to_convert: Vec<MusicFile> = Vec::new();
let input_path = PathBuf::from(&args.input);
let music_directory = read_dir(input_path).map_err(Errors::UnknownFolder)?;
let mut soundtracks: Vec<Soundtrack> = Default::default();
let mut song_groups: Vec<Vec<SongGroup>> = Default::default();
let mut total_songs = 0;
let mut total_song_groups = 0;
let mut files_to_convert: Vec<PathBuf> = Default::default();
// Loop through each folders for the soundtrack struct
for (i, soundtrack_dirs) in music_directory.enumerate() {
@ -100,56 +102,167 @@ fn process(args: &Args) -> Result<(), Errors> {
}
soundtrack_count += 1;
song_group_id = 0;
let st;
let song_group;
let mut unprocessed_files;
let soundtrack_name_str = soundtrack
.file_name()
.map_or(OsStr::new("Unknown soundtrack"), |f| f)
.to_string_lossy()
.trim()
.ascii_chars()
.to_string();
// Convert the folder name into 2 bytes
let mut soundtrack_name = soundtrack_name_str
.bytes()
.map(|b| [b, 0])
.collect::<Vec<[u8; 2]>>();
// Max value of 32
soundtrack_name.resize(32, [0; 2]);
let mut song_name: [[u8; 2]; 192] = [[0; 2]; 192];
let files = read_dir(soundtrack)
.map_err(Errors::UnknownIO)?
.collect::<Vec<_>>();
// Loop through each files in chunk of 6 (max songs allowed in a song group)
files.chunks(6).for_each(|song_files| {
let mut song_id: [i32; 6] = [0; 6];
song_group_id += 1;
sound_groups_ids.push(total_song_groups_count);
total_song_groups_count += 1;
let mut char_count = 0;
song_time_miliseconds = [0; 6];
for (g, f) in song_files.iter().enumerate() {
let song = f.as_ref().unwrap();
let song_path = song.path();
// Get file format kind for the files inside soundtracks
let format = match FileFormat::from_file(&song_path).map_err(Errors::UnknownIO) {
Ok(f) => f.kind(),
Err(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
}
};
songs_count += 1;
total_songs_count += 1;
let filepath = song.path();
let filename = filepath
.file_stem()
.map_or(OsStr::new("Unknown track"), |f| f)
.to_string_lossy()
.ascii_chars()
.to_string();
let mut name = filename.trim().bytes().collect::<Vec<u8>>();
name.resize(32, 0);
for b in name.iter() {
song_name[char_count] = [*b, 0];
char_count += 1;
}
files_to_convert.push(MusicFile {
path: song.path(),
soundtrack_name: soundtrack_name_str.clone(),
soundtrack_index: 0,
index: total_songs_count - 1,
});
}
let s = Song {
magic: 200819,
id: song_group_id - 1,
ipadding: 0,
soundtrack_id: i as i32,
song_id,
song_time_miliseconds,
song_name,
cpadding: [char::MIN; 16],
};
songs.push(s);
song_name = [[0; 2]; 192];
});
let mut total_time_miliseconds: i32 = 0;
for s in &songs {
if s.soundtrack_id == i as i32 {
total_time_miliseconds += s.song_time_miliseconds.iter().sum::<i32>()
}
}
sound_groups_ids.resize(84, 0);
let song_groups_ids: [i32; 84] = sound_groups_ids
.clone()
.try_into()
.map_err(|_| Errors::SkillIssue())?;
let st = Soundtrack {
magic: 136049,
id: i as i32,
num_songs: songs_count,
song_groups_ids: song_groups_ids,
total_time_miliseconds,
name: soundtrack_name
.try_into()
.map_err(|_| Errors::SkillIssue())?,
padding: [char::MIN; 24],
};
(
st,
song_group,
total_songs,
total_song_groups,
unprocessed_files,
) = process_soundtrack(
soundtrack,
i,
soundtrack_count,
total_songs,
total_song_groups,
)?;
soundtracks.push(st);
song_groups.push(song_group);
files_to_convert.append(&mut unprocessed_files);
sound_groups_ids = Vec::with_capacity(84);
songs_count = 0;
}
let mut soundtrack_ids: [i32; 100] = [0; 100];
for i in 0..soundtrack_count {
soundtrack_ids[i] = i as i32;
soundtrack_ids[i as usize] = i;
}
let header = Header {
magic: 1,
num_soundtracks: soundtrack_count as i32,
next_soundtrack_id: (soundtrack_count + 1) as i32,
magic: 0001,
num_soundtracks: soundtrack_count,
next_soundtrack_id: soundtrack_count + 1,
soundtrack_ids,
next_song_id: (total_songs + 1) as i32,
next_song_id: (songs_count as i32),
padding: [char::MIN; 24],
};
if files_to_convert.is_empty() {
if files_to_convert.len() == 0 {
return Err(Errors::NoFileToConvert());
}
write_database(&args.output, header, soundtracks, song_groups)?;
write_database(&args.output, header, soundtracks, songs)?;
for (i, f) in files_to_convert.iter().enumerate() {
let percentage: f64 = ((i + 1) as f64 / total_songs as f64) * 100.0;
for f in files_to_convert {
let percentage: f64 = ((f.index + 1) as f64 / total_songs_count as f64) * 100.0;
print!(
"{}{}\r{:3}% [{}{}] {:3}/{}",
Ansi::CursorUp,
Ansi::ClearLine,
"\x1B[1A",
"\x1B[K",
percentage as usize,
{
let mut bar = "=".repeat(percentage as usize / 3);
@ -159,26 +272,30 @@ fn process(args: &Args) -> Result<(), Errors> {
bar
},
" ".repeat(100 / 3 - percentage as usize / 3),
i + 1,
total_songs
f.index + 1,
total_songs_count
);
print!(
"{}\r{}Processing {} - {}",
Ansi::CursorDown,
Ansi::ClearLine,
f.parent()
.and_then(|f| f.file_stem())
.unwrap_or_else(|| OsStr::new("Unknown soundtrack"))
.to_string_lossy(),
f.file_stem()
.unwrap_or_else(|| OsStr::new("Unknown track"))
"\x1B[1B",
"\x1B[K",
f.soundtrack_name,
f.path
.file_stem()
.map_or(OsStr::new("Unknown track"), |f| f)
.to_string_lossy()
);
stdout().flush().map_err(Errors::UnknownIO)?;
convert_to_wma(f, &args.output, args.bitrate, &args.codec, i)?;
convert_to_wma(
f.path,
&args.output,
args.bitrate,
f.soundtrack_index as usize,
f.index as usize,
)?;
}
print!("\x1B[1A\x1B[K\r\x1B[K Done.");
@ -186,173 +303,6 @@ fn process(args: &Args) -> Result<(), Errors> {
Ok(())
}
fn build_songs_group(
chunks: &[DirEntry],
song_group_id: usize,
soundtrack_id: usize,
total_songs: usize,
) -> Result<(SongGroup, Vec<PathBuf>), Errors> {
let mut files_to_convert: Vec<PathBuf> = Default::default();
let mut songs: Vec<Song> = Default::default();
let mut song_group: SongGroup = SongGroup::default();
// Loop through each files in chunk of 6 (max songs allowed in a song group)
chunks.iter().enumerate().for_each(|(i, song)| {
let song_path = song.path();
// Ignore non files for song groups
if !song_path.is_file() {
return;
}
let song_id: i32 = (total_songs - chunks.len() + i) as i32;
let song_time_miliseconds = match get_duration(song_path) {
Ok(s) => s,
Err(e) => {
eprintln!("\x1b[0;31mFailed to get duration: {}\x1b[0;20m", e);
0
}
};
let filepath = song.path();
let filename = filepath
.file_stem()
.unwrap_or_else(|| OsStr::new("Unknown track"))
.to_string_lossy()
.ascii_chars()
.to_string();
let mut name = filename.trim().bytes().collect::<Vec<u8>>();
name.resize(32, 0);
let mut song_name: [[u8; 2]; 32] = [[0; 2]; 32];
for (char_count, b) in name.iter().enumerate() {
song_name[char_count] = [*b, 0];
}
let s = Song {
song_id,
song_time_miliseconds,
song_name,
};
songs.push(s);
files_to_convert.push(song.path());
});
for s in songs.chunks(6) {
let empty_song = Song::default();
let mut empty_song_groups = s.to_vec();
empty_song_groups.resize(6, empty_song);
song_group = SongGroup {
magic: 200819,
soundtrack_id: soundtrack_id as i32,
id: song_group_id as i32,
ipadding: 0,
songs: empty_song_groups.try_into().unwrap(),
cpadding: [char::MIN; 16],
};
}
Ok((song_group, files_to_convert))
}
fn process_soundtrack(
soundtrack: PathBuf,
soundtrack_index: usize,
soundtrack_count: usize,
mut total_songs: usize,
total_song_groups: usize,
) -> Result<(Soundtrack, Vec<SongGroup>, usize, usize, Vec<PathBuf>), Errors> {
let mut files_to_convert: Vec<PathBuf> = Default::default();
let mut songs_count = 0;
let mut song_groups: Vec<SongGroup> = Default::default();
let mut song_groups_ids: Vec<i32> = Vec::with_capacity(84);
let mut song_group_id = total_song_groups;
let soundtrack_name_str = soundtrack
.file_name()
.unwrap_or_else(|| OsStr::new("Unknown soundtrack"))
.to_string_lossy()
.trim()
.ascii_chars()
.to_string();
// Convert the folder name into 2 bytes
let mut soundtrack_name = soundtrack_name_str
.bytes()
.map(|b| [b, 0])
.collect::<Vec<[u8; 2]>>();
// Max value of 32
soundtrack_name.resize(32, [0; 2]);
let files = read_dir(soundtrack)?
.filter_map(|x| x.ok())
.collect::<Vec<_>>();
let mut total_time_miliseconds: i32 = 0;
for chunk in files.chunks(6) {
total_songs += chunk.len();
songs_count += chunk.len();
let (songs, mut unconverted_files) =
build_songs_group(chunk, song_group_id, soundtrack_index, total_songs)?;
songs.songs.iter().for_each(|x| {
if songs.soundtrack_id == soundtrack_index as i32 {
total_time_miliseconds += x.song_time_miliseconds;
}
});
song_groups.push(songs);
song_group_id += 1;
files_to_convert.append(&mut unconverted_files);
}
for s in &song_groups {
song_groups_ids.push(s.id);
}
song_groups_ids.resize(84, 0);
let song_groups_ids: [i32; 84] = song_groups_ids
.clone()
.try_into()
.map_err(|_| Errors::SkillIssue())?;
let st = Soundtrack {
magic: 136049,
id: soundtrack_index as i32,
num_songs: songs_count as u32,
song_groups_ids,
total_time_miliseconds,
name: soundtrack_name
.try_into()
.map_err(|_| Errors::SkillIssue())?,
padding: [char::MIN; 24],
};
let mut soundtrack_ids: [i32; 100] = [0; 100];
for i in 0..soundtrack_count {
soundtrack_ids[i] = i as i32;
}
Ok((
st,
song_groups,
total_songs,
song_group_id,
files_to_convert,
))
}
fn get_duration(path: PathBuf) -> Result<i32, Errors> {
let output = Command::new("ffprobe")
.args([
@ -374,23 +324,24 @@ fn get_duration(path: PathBuf) -> Result<i32, Errors> {
}
fn convert_to_wma(
input: &Path,
input: PathBuf,
output: &String,
bitrate: i16,
codec: &Codec,
soundtrack_index: usize,
song_index: usize,
) -> Result<(), Errors> {
let binding = input.as_os_str();
let binding = input.into_os_string();
let input = binding.to_str().unwrap();
fs::create_dir_all(format!("{}/0000", output)).map_err(Errors::UnknownIO)?;
fs::create_dir_all(format!("{}/{:0>4}", output, soundtrack_index))
.map_err(Errors::UnknownIO)?;
Command::new("ffmpeg")
.args([
"-i",
input,
"-acodec",
&codec.to_string(),
"wmav1",
"-ac",
"2",
"-ar",
@ -402,7 +353,10 @@ fn convert_to_wma(
"-map",
"0:a",
"-y",
&format!("{}/0000/{:0>8x}.wma", output, song_index),
&format!(
"{}/{:0>4}/{:0>8x}.wma",
output, soundtrack_index, song_index
),
])
.output()
.map_err(Errors::MissingFfmpeg)?;
@ -414,42 +368,41 @@ fn write_database(
output: &String,
header: Header,
soundtracks: Vec<Soundtrack>,
songs: Vec<Vec<SongGroup>>,
songs: Vec<Song>,
) -> Result<(), Errors> {
fs::create_dir_all(format!("{}/", &output)).map_err(Errors::UnknownIO)?;
let mut database = File::create(format!("{}/ST.DB", &output)).map_err(Errors::UnknownIO)?;
database.write_all(header.as_bytes())?;
database.write_all(&header.magic.as_bytes())?;
database.write_all(&header.num_soundtracks.as_bytes())?;
database.write_all(&header.next_soundtrack_id.as_bytes())?;
database.write_all(&header.soundtrack_ids.as_bytes())?;
database.write_all(&header.next_song_id.as_bytes())?;
database.write_all(&header.padding.as_bytes())?;
database.write_all(soundtracks.as_bytes())?;
for st in &soundtracks {
database.write_all(&st.magic.as_bytes())?;
database.write_all(&st.id.as_bytes())?;
database.write_all(&st.num_songs.as_bytes())?;
database.write_all(&st.song_groups_ids.as_bytes())?;
database.write_all(&st.total_time_miliseconds.as_bytes())?;
database.write_all(&st.name.as_bytes())?;
database.write_all(&st.padding.as_bytes())?;
}
for _ in 0..100 - &soundtracks.len() {
database.write_all(&[0_u8; 512])?;
database.write_all(&[0 as u8; 512])?;
}
for s in songs {
for s in s {
database.write_all(s.magic.as_bytes())?;
database.write_all(s.soundtrack_id.as_bytes())?;
database.write_all(s.id.as_bytes())?;
database.write_all(s.ipadding.as_bytes())?;
for x in s.songs.chunks(6) {
for chunk in x {
database.write_all(chunk.song_id.as_bytes())?;
}
for chunk in x {
database.write_all(chunk.song_time_miliseconds.as_bytes())?;
}
for chunk in x {
database.write_all(chunk.song_name.as_bytes())?;
}
}
database.write_all(s.cpadding.as_bytes())?;
}
database.write_all(&s.magic.as_bytes())?;
database.write_all(&s.soundtrack_id.as_bytes())?;
database.write_all(&s.id.as_bytes())?;
database.write_all(&s.ipadding.as_bytes())?;
database.write_all(&s.song_id.as_bytes())?;
database.write_all(&s.song_time_miliseconds.as_bytes())?;
database.write_all(&s.song_name.as_bytes())?;
database.write_all(&s.cpadding.as_bytes())?;
}
Ok(())

View file

@ -1,22 +1,17 @@
use zerocopy::{Immutable, IntoBytes};
use std::path::PathBuf;
#[derive(clap::ValueEnum, Debug, Clone)]
pub enum Codec {
Wmav1,
Wmav2,
}
use zerocopy::TryFromBytes;
impl std::fmt::Display for Codec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Codec::Wmav1 => write!(f, "wmav1"),
Codec::Wmav2 => write!(f, "wmav2"),
}
}
pub struct MusicFile {
pub path: PathBuf,
pub soundtrack_index: u32,
pub soundtrack_name: String,
pub index: u32,
}
// https://xboxdevwiki.net/Soundtracks#ST.DB
#[derive(Debug, Immutable, IntoBytes)]
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Header {
pub magic: i32,
@ -27,7 +22,7 @@ pub struct Header {
pub padding: [char; 24],
}
#[derive(Debug, Immutable, IntoBytes)]
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Soundtrack {
pub magic: i32,
@ -39,91 +34,15 @@ pub struct Soundtrack {
pub padding: [char; 24],
}
#[derive(Debug, Immutable, IntoBytes, Clone, Copy)]
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Song {
pub song_id: i32,
pub song_time_miliseconds: i32,
pub song_name: [[u8; 2]; 32],
}
#[derive(Debug, Immutable, IntoBytes, Clone, Copy)]
#[repr(C)]
pub struct SongGroup {
pub magic: i32,
pub soundtrack_id: i32,
pub id: i32,
pub ipadding: i32,
pub songs: [Song; 6],
pub song_id: [i32; 6],
pub song_time_miliseconds: [i32; 6],
pub song_name: [[u8; 2]; 192],
pub cpadding: [char; 16],
}
impl Default for Header {
#[inline]
fn default() -> Header {
Header {
magic: 0,
num_soundtracks: 0,
next_soundtrack_id: 0,
soundtrack_ids: [0; 100],
next_song_id: 0,
padding: [char::MIN; 24],
}
}
}
impl Default for Soundtrack {
#[inline]
fn default() -> Soundtrack {
Soundtrack {
magic: 0,
id: 0,
num_songs: 0,
song_groups_ids: [0; 84],
total_time_miliseconds: 0,
name: [[0; 2]; 32],
padding: [char::MIN; 24],
}
}
}
impl Default for Song {
#[inline]
fn default() -> Song {
Song {
song_id: 0,
song_time_miliseconds: 0,
song_name: [[0; 2]; 32],
}
}
}
impl Default for SongGroup {
#[inline]
fn default() -> SongGroup {
SongGroup {
magic: 0,
soundtrack_id: 0,
id: 0,
ipadding: 0,
songs: [Song::default(); 6],
cpadding: [char::MIN; 16],
}
}
}
pub enum Ansi {
ClearLine,
CursorUp,
CursorDown,
}
impl std::fmt::Display for Ansi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Ansi::ClearLine => write!(f, "\x1B[K"),
Ansi::CursorUp => write!(f, "\x1B[1A"),
Ansi::CursorDown => write!(f, "\x1B[1B"),
}
}
}