feat: Initial version of xbst 🚀

This commit is contained in:
LoicBersier 2025-07-29 20:33:52 +02:00
commit 84e92c85a7
6 changed files with 766 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

286
Cargo.lock generated Normal file
View file

@ -0,0 +1,286 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be92d32e80243a54711e5d7ce823c35c41c9d929dc4ab58e1276f625841aadf9"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "707eab41e9622f9139419d573eca0900137718000c517d47da73045f54331c3d"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "xbst"
version = "0.1.0"
dependencies = [
"clap",
"thiserror",
"zerocopy",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "xbst"
version = "0.1.0"
edition = "2021"
[dependencies]
thiserror = "2.0.12"
clap = { version = "4.5.31", features = ["derive"] }
zerocopy = { version = "0.8.26", features = ["derive"] }

47
readme.md Normal file
View file

@ -0,0 +1,47 @@
# XBST
I wanted a simple utility to sync my music library to something I can have on my original Xbox but I didn't find any easy to use, crossplatform and straight forward utility so I made this.
## Prerequisits
- Windows or Linux (maybe macOS, untested)
- ffmpeg and ffprobe in your PATH
If you are on Windows you can easily install ffmpeg and ffprobe by opening a terminal and doing `winget install ffmpeg`.
If you are on Linux, well, you're probably a nerd and already have it, if not, open a terminal and do `sudo apt install ffmpeg` on debian based distributions.
## Usage
Drag and drop your music folder onto xbst
The input music folder needs the following structure:
```
📁 Music
├📁 Soundtrack 1
|├ 💾 Music 1
|├ 💾 Music 2
|└ 💾 Music 3
└📁 Soundtrack 2
└ 💾 Music 1
```
```
Usage: xbst [INPUT] [OUTPUT] [BITRATE]
Arguments:
[INPUT] Input folder of your musics [default: ./music]
[OUTPUT] Output folder for the database and converted musics [default: ./output]
[BITRATE] Bitrate for the output [default: 320k]
Options:
-h, --help Print help
-V, --version Print version
```
## Known issues
- The progress bar on soundtrack other than the first one doesn't progress
- Some files, once converted, are quieter than usual?
- Untested with a large library, probably has issues?
- Code is poo poo

371
src/main.rs Normal file
View file

@ -0,0 +1,371 @@
use std::{
ffi::OsStr,
fs::{self, read_dir, File},
io::{stdout, Write},
path::PathBuf,
process::Command,
string::FromUtf8Error,
};
mod utils;
use clap::Parser;
use thiserror::Error;
use zerocopy::IntoBytes;
use crate::utils::{Header, MusicFile, Song, Soundtrack};
#[derive(Error, Debug)]
enum Errors {
#[error("Couldn't find your input folder. {}", .0.kind())]
UnknownFolder(#[source] std::io::Error),
#[error(transparent)]
UnknownIO(#[from] std::io::Error),
#[error(transparent)]
FromUtf8(#[from] FromUtf8Error),
#[error("Skill issue on the programmer part ngl, report this to dev pls")]
SkillIssue(),
#[error("You are missing ffprobe in your PATH")]
MissingFfprobe(#[source] std::io::Error),
#[error("You are missing ffmpeg in your PATH")]
MissingFfmpeg(#[source] std::io::Error),
}
#[derive(Parser, Debug)]
#[command(version)]
struct Args {
/// Input folder of your musics
#[arg(default_value = "./music")]
input: String,
/// Output folder for the database and converted musics
#[arg(default_value = "./output")]
output: String,
/// Bitrate for the output
#[arg(default_value = "320k")]
bitrate: String,
}
fn main() {
let args = Args::parse();
println!(
"⠀⠀⠀⠂⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
XBST
"
);
match process(&args) {
Ok(_) => (),
Err(e) => eprintln!("\r\x1B[K\x1b[0;31m{}\x1b[0;20m", e),
}
}
fn process(args: &Args) -> Result<(), Errors> {
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)?;
// Loop through each folders for the soundtrack struct
for (i, soundtrack_dirs) in music_directory.enumerate() {
let soundtrack = soundtrack_dirs.map_err(Errors::UnknownFolder)?.path();
if !soundtrack.is_dir() {
continue;
}
soundtrack_count += 1;
song_group_id = 0;
// Convert the folder name into 2 bytes
let mut soundtrack_name = soundtrack
.file_name()
.map_or(OsStr::new("Unknown soundtrack"), |f| f)
.to_string_lossy()
.trim()
.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];
song_files.iter().enumerate().for_each(|(g, f)| {
let song = f.as_ref().unwrap();
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;31m{}\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();
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_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],
};
soundtracks.push(st);
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 as usize] = i;
}
let header = Header {
magic: 0001,
num_soundtracks: soundtrack_count,
next_soundtrack_id: soundtrack_count + 1,
soundtrack_ids,
next_song_id: (songs_count as i32),
padding: [char::MIN; 24],
};
write_database(&args.output, header, soundtracks, songs)?;
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}/{}",
"\x1B[1A",
"\x1B[K",
percentage as usize,
{
let mut bar = "=".repeat(percentage as usize / 3);
if percentage < 100.0 {
bar += ">"
}
bar
},
" ".repeat(100 / 3 - percentage as usize / 3),
f.index + 1,
total_songs_count
);
print!(
"{}\r{}Processing {} ",
"\x1B[1B",
"\x1B[K",
f.path
.file_stem()
.map_or(OsStr::new("Unknown track"), |f| f)
.to_string_lossy()
);
let _ = stdout().flush().map_err(Errors::UnknownIO);
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.");
Ok(())
}
fn get_duration(path: PathBuf) -> Result<i32, Errors> {
let output = Command::new("ffprobe")
.args([
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
])
.arg(path.into_os_string())
.output()
.map_err(Errors::MissingFfprobe)?;
let binding = String::from_utf8(output.stdout).map_err(Errors::FromUtf8)?;
let stdout = binding.trim();
Ok((stdout.parse::<f32>().unwrap() * 1000.0) as i32)
}
fn convert_to_wma(
input: PathBuf,
output: &String,
bitrate: &String,
soundtrack_index: usize,
song_index: usize,
) -> Result<(), Errors> {
let binding = input.into_os_string();
let input = binding.to_str().unwrap();
fs::create_dir_all(format!("{}/{:0>4}", output, soundtrack_index)).unwrap();
Command::new("ffmpeg")
.args([
"-i",
input,
"-acodec",
"wmav2",
"-ac",
"2",
"-ar",
"44100",
"-b:a",
bitrate,
"-y",
&format!(
"{}/{:0>4}/{:0>8x}.wma",
output, soundtrack_index, song_index
),
])
.output()
.map_err(Errors::MissingFfmpeg)?;
Ok(())
}
fn write_database(
output: &String,
header: Header,
soundtracks: Vec<Soundtrack>,
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.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())?;
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 as u8; 512])?;
}
for s in songs {
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(())
}

52
src/utils.rs Normal file
View file

@ -0,0 +1,52 @@
use std::path::PathBuf;
use zerocopy::TryFromBytes;
pub struct MusicFile {
pub path: PathBuf,
pub soundtrack_index: u32,
pub index: u32,
}
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Database {
pub header: Header,
pub soundtrack: Soundtrack,
}
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Header {
pub magic: i32,
pub num_soundtracks: i32,
pub next_soundtrack_id: i32,
pub soundtrack_ids: [i32; 100],
pub next_song_id: i32,
pub padding: [char; 24],
}
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Soundtrack {
pub magic: i32,
pub id: i32,
pub num_songs: u32,
pub song_groups_ids: [i32; 84],
pub total_time_miliseconds: i32,
pub name: [[u8; 2]; 32],
pub padding: [char; 24],
}
#[derive(Debug, TryFromBytes)]
#[repr(C)]
pub struct Song {
pub magic: i32,
pub soundtrack_id: i32,
pub id: i32,
pub ipadding: i32,
pub song_id: [i32; 6],
pub song_time_miliseconds: [i32; 6],
pub song_name: [[u8; 2]; 192],
pub cpadding: [char; 16],
}