1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use anyhow::Result;
use clap::{Parser, Subcommand};
use rekord_ripper::analysis::{self, CopyOpts};
use rekord_ripper::db::{self, MasterDb, SafetyOpts};
use rekord_ripper::dump;
use rekord_ripper::tui;
#[derive(Parser)]
#[command(name = "rekord-ripper", version, about = "Rekordbox analysis utility")]
struct Cli {
/// Bypass the "rekordbox is running" hard refuse on any mutating command.
#[arg(
long = "i-know-rekordbox-is-open-and-may-corrupt-my-data",
global = true
)]
bypass_rekordbox_check: bool,
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
/// Dump analysis state for tracks. With no query, lists every track.
///
/// A numeric query is matched against djmdContent.ID; anything else is
/// matched as a substring against Title and Artist name.
Dump {
/// Track ID, or substring of title/artist. Omit to dump everything.
query: Option<String>,
/// Maximum number of tracks to print. Defaults to 10 when searching by
/// substring; unlimited when listing all (no query).
#[arg(short, long)]
limit: Option<u32>,
},
/// Copy analysis (cues, beat grid, BPM, key, mixer params) from one track
/// onto one or more destinations.
Cp {
/// Source track ID — the analyzed track to read from.
src: String,
/// Destination track IDs — receive a copy of `src`'s analysis.
#[arg(required = true)]
dst: Vec<String>,
/// Overwrite cues on destinations that already have some.
#[arg(long)]
replace: bool,
/// After copying, set bit 7 of djmdContent.Analysed on each destination
/// so rekordbox won't re-analyze and lose your copied state.
#[arg(long)]
lock: bool,
/// Print the plan without applying it.
#[arg(long)]
dry_run: bool,
},
/// Interactive two-column TUI. Source on the left, destinations on the
/// right, with search bars, multi-select, fuzzy-match-from-source toggle,
/// and an auto-mode filter for unanalyzed destinations.
Tui,
/// Batch-match unanalyzed (or unlocked) tracks to a similar analyzed source
/// by normalized title + artist + duration, then copy. Default = dry-run;
/// pass --apply to write.
Auto {
/// Maximum number of matched plans to consider.
#[arg(short, long)]
limit: Option<u32>,
/// Actually apply matched plans. Without this, prints proposals only.
#[arg(long)]
apply: bool,
/// Overwrite cues on destinations that already have some.
#[arg(long)]
replace: bool,
/// Set the lock bit on destinations after copy.
#[arg(long)]
lock: bool,
/// Tolerance on track length difference (in integer seconds).
#[arg(long, default_value_t = 1)]
duration_tol_secs: i64,
/// Allow destinations that already have cues (still gated by --replace).
#[arg(long)]
include_cued: bool,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
let mut db = MasterDb::open()?;
let safety = SafetyOpts {
bypass_rekordbox_check: cli.bypass_rekordbox_check,
};
match cli.cmd {
Cmd::Dump { query, limit } => dump::run(&db, query.as_deref(), limit)?,
Cmd::Tui => tui::run(db, safety)?,
Cmd::Cp {
src,
dst,
replace,
lock,
dry_run,
} => run_cp(
&mut db,
&src,
&dst,
CopyOpts { replace, lock },
dry_run,
safety,
)?,
Cmd::Auto {
limit,
apply,
replace,
lock,
duration_tol_secs,
include_cued,
} => run_auto(
&mut db,
AutoArgs {
limit,
apply,
opts: CopyOpts { replace, lock },
duration_tol_secs,
include_cued,
safety,
},
)?,
}
Ok(())
}
struct AutoArgs {
limit: Option<u32>,
apply: bool,
opts: CopyOpts,
duration_tol_secs: i64,
include_cued: bool,
safety: SafetyOpts,
}
fn run_auto(db: &mut MasterDb, args: AutoArgs) -> Result<()> {
let matches = analysis::find_auto_matches(
db,
analysis::AutoFilter {
duration_tol_secs: args.duration_tol_secs,
include_cued: args.include_cued,
limit: args.limit,
},
)?;
if matches.is_empty() {
println!("No matches found.");
return Ok(());
}
let mut plans = Vec::new();
let mut failed = Vec::new();
for m in &matches {
match analysis::build_plan(db, &m.src_id, &m.dst_id, &args.opts) {
Ok(plan) => plans.push(plan),
Err(e) => failed.push((m.clone(), e)),
}
}
for plan in &plans {
println!("{}", plan.render());
}
for (m, e) in &failed {
eprintln!(
"skip {} ← {}: {e}",
m.dst_id, m.src_id
);
}
if !args.apply {
eprintln!(
"{} matched, {} eligible, {} failed validation. Dry-run; pass --apply to write.",
matches.len(),
plans.len(),
failed.len()
);
return Ok(());
}
db::safety_preflight(args.safety)?;
for (i, plan) in plans.iter().enumerate() {
let backup = analysis::apply_plan(db, plan)?;
if i == 0 {
eprintln!("backed up to: {}", backup.display());
}
eprintln!("applied: {} → {}", plan.src.id, plan.dst.id);
}
Ok(())
}
fn run_cp(
db: &mut MasterDb,
src: &str,
dsts: &[String],
opts: CopyOpts,
dry_run: bool,
safety: SafetyOpts,
) -> Result<()> {
// Build every plan up front; abort the batch if any fails validation.
let plans = dsts
.iter()
.map(|dst| analysis::build_plan(db, src, dst, &opts))
.collect::<Result<Vec<_>>>()?;
for plan in &plans {
println!("{}", plan.render());
}
if dry_run {
eprintln!("dry-run: no changes applied.");
return Ok(());
}
db::safety_preflight(safety)?;
for (i, plan) in plans.iter().enumerate() {
let backup = analysis::apply_plan(db, plan)?;
if i == 0 {
eprintln!("backed up to: {}", backup.display());
}
eprintln!("applied: {} → {}", plan.src.id, plan.dst.id);
}
Ok(())
}