58 Commits
Author SHA1 Message Date
Craig Drummond 37947564fc Remove mention of temporary files, as this is no longer relevant. 2022-04-24 08:51:07 +01:00
Craig Drummond 8a5d9bc276 Don't need ffmpeg.exe for Windows builds, as bliss-rs does cue splitting 2022-04-24 08:35:27 +01:00
Craig Drummond 467e341cfb Update version of bliss-rs, this now handles CUE processing internally. 2022-04-18 20:20:32 +01:00
Craig Drummond 2788759829 More 'cargo fmt' reverts 2022-04-15 09:03:47 +01:00
Craig Drummond 0653e365b8 Undo some 'cargo fmt' changes. 2022-04-13 21:32:44 +01:00
Craig Drummond 985902325e Minor formatting mods 2022-04-13 21:07:09 +01:00
Craig Drummond f3903383c9 Update version of bliss-rs 2022-04-12 21:17:29 +01:00
Craig Drummond f13601f1c2 loft-rs 0.6.1 2022-04-10 20:27:29 +01:00
CDrummond dfb87881c9 Add colon 2022-04-06 13:23:41 +01:00
CDrummond b1f89050e2 Mention that must run from terminal window 2022-04-06 13:22:47 +01:00
CDrummond 94ef2e2c1f Show error if can't open/create database 2022-04-06 13:22:30 +01:00
CDrummond 7fb2e66c58 Update version of tag reader library, should now support ID3v2 in FLAC. 2022-04-06 13:10:05 +01:00
CDrummond 9f8ea6e490 Fix indentation 2022-03-23 20:16:27 +00:00
CDrummond 0aa10918ae Tidy up code, thanks to Serial-ATA 2022-03-23 20:15:54 +00:00
CraigD caa8f124a7 Merge pull request #1 from Serial-ATA/master
Cleanup
2022-03-23 07:34:44 +00:00
Serial dabbfdb206 More typos
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 19:08:50 -04:00
Serial 182d980395 Typos
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 19:07:59 -04:00
Serial c51de47696 Missing derive
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 18:05:39 -04:00
Serial f6edb983d3 Cleanup upload module
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:49:27 -04:00
Serial 625248fbc6 Cleanup tags module
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:44:40 -04:00
Serial 37d9f627ad Cleanup db module
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:41:02 -04:00
Serial b61aee0a6a Cleanup cue module
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:32:31 -04:00
Serial 51ce6cadc7 Cleanup analyse module
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:25:03 -04:00
Serial b097f06b5f Add Metadata::is_empty
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:05:33 -04:00
Serial e0a6759af5 fmt
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
2022-03-22 17:02:08 -04:00
Craig Drummond 0153b85b65 Track limit should span normal & cue and over folders 2022-03-16 15:34:23 +00:00
Craig Drummond 311c5e4242 Only output music path if more than 1 configured 2022-03-16 10:40:30 +00:00
Craig Drummond 4f7850bc40 Merge branch 'master' into multi-folder 2022-03-16 10:37:28 +00:00
Craig Drummond e88c4446d2 Update 2022-03-16 10:30:06 +00:00
Craig Drummond 4addd27329 Revert back to x86 only mac binaries 2022-03-16 09:54:15 +00:00
Craig Drummond 0db7d882c2 Build fat binary for macOS. 2022-03-16 08:55:29 +00:00
Craig Drummond 7a6317b28c Use git version of tag reader library. 2022-03-15 20:17:54 +00:00
Craig Drummond 7998278c9b Sort paths 2022-03-15 16:37:40 +00:00
CDrummond 08e43bf609 When performing a dry-run analysis (--dry-run) print paths of all tracks to be analysed and to be removed. 2022-03-15 12:08:15 +00:00
CDrummond 302b0f9483 Output list of (up to 100) tracks that failed to analyse. 2022-03-15 12:07:24 +00:00
CDrummond a6284c3761 Add support for multiple music folders 2022-03-14 11:24:16 +00:00
Craig Drummond cf729aaef3 Simplify 2022-03-13 11:47:01 +00:00
Craig Drummond 2ff95c27c5 OK, ffmpeg needs a file extension 2022-03-13 11:39:52 +00:00
Craig Drummond 4c40a0b41b Dont transcode CUE tracks to MP3, just copy in original format - *much*
faster
2022-03-13 11:32:35 +00:00
Craig Drummond 649ce31907 Add some info on CUE file handling 2022-03-13 11:13:01 +00:00
Craig Drummond 6d53ac16a9 Make bliss-analyser in linux and mac zips executable 2022-03-13 11:06:46 +00:00
Craig Drummond a859f7ac0f If album missing, then set to audio filename (without extension) 2022-03-13 10:41:36 +00:00
Craig Drummond f3a86414dc If track artist is empty then set this to album_artist (if it is not
also empty)
2022-03-13 10:28:46 +00:00
Craig Drummond e8614340f0 Another attempt to fix terminal echo 2022-03-13 08:26:15 +00:00
Craig Drummond b361948396 Copy ffmpeg.exe into windows zip, as will be needed for cue files 2022-03-13 07:55:39 +00:00
Craig Drummond 25c01f4434 Number cue tracks from 1, should match LMS track number 2022-03-13 06:54:09 +00:00
Craig Drummond 5fef00c24a No tag error for cue 2022-03-12 23:44:36 +00:00
Craig Drummond ef772ad30f Fix ffmpeg beaking terminal echo 2022-03-12 23:43:05 +00:00
Craig Drummond caa77bd847 Add support for analysing cue tracks 2022-03-12 23:07:56 +00:00
Craig Drummond 05532ec6cd Fix removing old tracks when run under Windows. 2022-03-07 20:27:31 +00:00
Craig Drummond ee468e723e Only try to remove files if there are some to remove 2022-03-07 20:15:21 +00:00
Craig Drummond 7996d9a525 If fail to remove old tracks from DB, then output more info. 2022-03-07 19:49:05 +00:00
Craig Drummond a35140c10e Use newer version of tag reader library. 2022-03-07 19:48:18 +00:00
Craig Drummond 2cb17e4dd4 Detail how to exclude specific genre, even if its not the only genre of
a track.
2022-03-06 11:55:35 +00:00
Craig Drummond e60a3c7b09 Update ignore syntax to allow adding SQL WHERE clauses. 2022-03-06 11:31:02 +00:00
Craig Drummond 6f8f3d52a9 Actually items in INI override commandline params 2022-03-06 10:58:18 +00:00
Craig Drummond 2771df83db Use ubuntu-20.04 to have consistent builds incase ubuntu-latest changes,
which might also change ffmpeg version.
2022-03-05 23:35:42 +00:00
Craig Drummond c5d3b37f67 Package vcruntime140.dll with Windows ZIP. 2022-03-05 23:22:28 +00:00
11 changed files with 644 additions and 403 deletions

No files matched your search

+5 -2
View File
@@ -8,7 +8,7 @@ env:
jobs:
Linux:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- name: Checkout
@@ -33,6 +33,7 @@ jobs:
name: bliss-analyser-linux
path: releases/
macOS:
runs-on: macos-11.0
@@ -47,7 +48,7 @@ jobs:
- name: Install deps
run: |
brew install ffmpeg@5
brew install ffmpeg@5
- name: Build
run: |
@@ -65,6 +66,7 @@ jobs:
name: bliss-analyser-mac
path: releases/
Windows:
runs-on: windows-2019
@@ -99,6 +101,7 @@ jobs:
cp UserGuide.md releases/README.md
cp LICENSE releases/
cp configs/windows.ini releases/config.ini
cp c:\Windows\system32\vcruntime140.dll releases
- name: Upload artifacts
uses: actions/upload-artifact@v2
Generated
+26 -11
View File
@@ -113,7 +113,7 @@ dependencies = [
[[package]]
name = "bliss-analyser"
version = "0.0.1"
version = "0.2.0"
dependencies = [
"anyhow",
"argparse",
@@ -122,6 +122,7 @@ dependencies = [
"configparser",
"dirs",
"env_logger",
"if_chain",
"indicatif",
"lofty",
"log",
@@ -133,8 +134,9 @@ dependencies = [
[[package]]
name = "bliss-audio"
version = "0.4.6"
source = "git+https://github.com/Polochon-street/bliss-rs?rev=5f366b0#5f366b0d809886265ac54398a61523431b44f8a0"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "947fa29cb8c27448497bdbcbab05a9eb3c1f046a5efd7b684060a6dc587bb761"
dependencies = [
"bliss-audio-aubio-rs",
"crossbeam",
@@ -148,6 +150,7 @@ dependencies = [
"noisy_float",
"num_cpus",
"rayon",
"rcue",
"ripemd160",
"rustfft",
"strum",
@@ -596,6 +599,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "if_chain"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed"
[[package]]
name = "indexmap"
version = "1.8.0"
@@ -686,9 +695,9 @@ dependencies = [
[[package]]
name = "lofty"
version = "0.5.0"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b98a3a1ca2f3054b802e5012e5800674d37dc4af7f99f3037fb57af6a2aeaa"
checksum = "45967792fc531850b544d0e50065ffda8ce814a0d2142a6cbe7009961964e17c"
dependencies = [
"base64",
"byteorder",
@@ -889,18 +898,18 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]]
name = "ogg_pager"
version = "0.3.0"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4507172566b8e1b4d4e3da7f33453055a223fdcf96bc7c5e5d67168f1c4b241b"
checksum = "2a846c4c0578b95d4568e6f48210dd6045e7a9572af2b020062d06b6dea049f4"
dependencies = [
"byteorder",
]
[[package]]
name = "once_cell"
version = "1.9.0"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "opaque-debug"
@@ -916,9 +925,9 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "paste"
version = "1.0.6"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5"
checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc"
[[package]]
name = "peeking_take_while"
@@ -1088,6 +1097,12 @@ dependencies = [
"num_cpus",
]
[[package]]
name = "rcue"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fca1481d62f18158646de2ec552dd63f8bdc5be6448389b192ba95c939df997e"
[[package]]
name = "redox_syscall"
version = "0.1.57"
+4 -3
View File
@@ -1,6 +1,6 @@
[package]
name = "bliss-analyser"
version = "0.0.1"
version = "0.2.0"
authors = ["Craig Drummond <craig.p.drummond@gmail.com>"]
edition = "2018"
license = "GPL-3.0-only"
@@ -10,17 +10,18 @@ keywords = ["audio", "song", "similarity"]
readme = "README.md"
[dependencies]
bliss-audio = { git = "https://github.com/Polochon-street/bliss-rs", rev = "5f366b0" }
bliss-audio = "0.5.0"
argparse = "0.2.2"
anyhow = "1.0.40"
rusqlite = { version = "0.25.0", features = ["bundled"] }
log = "0.4.14"
env_logger = "0.8.4"
indicatif = "0.16.2"
lofty = "0.5.0"
lofty = "0.6.1"
dirs = "1"
chrono = "0.4.19"
regex = "1"
substring = "1.4.5"
ureq = "2.4.0"
configparser = "3.0.0"
if_chain = "1.0.2"
+26
View File
@@ -1,3 +1,29 @@
0.2.0
-----
1. Tidy up code, thanks to Serial-ATA
2. Update version of tag reader library, should now support ID3v2 in FLAC.
3. Show error message if can't open, or create, database file.
4. Update version of bliss-rs, this now handles CUE processing internally.
0.1.0
-----
1. Add support for analysing CUE files.
2. Output list of (up to 100) tracks that failed to analyse.
3. When performing a dry-run analysis (--dry-run) print paths of all tracks to
be analysed and to be removed.
4. Use git version of tag reader library.
5. Support up to 5 music folders (music, music_1, music_2, music_3, and
music_4).
0.0.2
-----
1. Package vcruntime140.dll with Windows ZIP.
2. Update user docs.
3. Update ignore syntax to allow adding SQL WHERE clauses.
4. Use newer version of tag reader library.
5. If fail to remove old tracks from DB, then output more info.
6. Fix removing old tracks when run under Windows.
0.0.1
-----
1. Initial release.
+40 -12
View File
@@ -6,6 +6,8 @@ upload its database of music analysis to LMS. The `Bliss Mixer` LMS plugin can
then use this information to provide music mixes for LMS's `Don't Stop the Music`
feature.
**NOTE:** You must run this application from a terminal window (e.g. cmd.com or
PowerShell for Windows), as there is no graphical user interface.
Quick guide
@@ -102,7 +104,8 @@ ignore=ignore.txt
The following items are supported:
* `music` specifies the location of your music collection - e.g. `c:\Users\user\Music`
for windows. This default to `Music` within the user's home folder.
for windows. This default to `Music` within the user's home folder. Up to 4 other
music folders may be specified via `music_1`, `music_2`, `music_3`, and `music_4`
* `db` specifies the name and location of the database file used to store the
analysis results. This will default to `bliss.db` in the current folder.
* `lms` specifies the hostname, or IP address, of your LMS server. This is used
@@ -110,9 +113,6 @@ when uploading the database file to LMS. This defaults to `127.0.0.1`
* `ignore` specifies the name and location of a file containing items to ignore
in mixes. See the `Ignore` section later on for more details.
All configuration items can be supplied on the command-line, and if so those
specified override those from the config file.
Command-line parameters
@@ -135,8 +135,8 @@ tracks are to be analysed and how many old tracks are left in the database.
* `-L` / `--lms` Hostname, or IP address, of your LMS server.
* `-n` / `--numtracks` Specify maximum number of tracks to analyse.
If any of these are used, then they will override their equivalent from the INI
config file.
Equivalent items specied in the INI config file (detailed above) will override
any specified on the commandline.
`bliss-analyser` requires one extra parameter, which is used to determine the
required task. This takes the following values:
@@ -177,6 +177,14 @@ As a rough guide, a 2015-era i7 8-core laptop with SSD analyses around 14000
tracks/hour.
CUE files
---------
If the anlyser encounters an audio file with a matching CUE file (e.g.
`album.flac` and `album.cue` in same folder) then it will attempt to analyse the
individual tracks contained within.
Exclude folders
---------------
@@ -247,6 +255,10 @@ entries):
.\bliss-analyser.exe tags
```
*NOTE* Tag re-reading is not implemented for CUE tracks. Therefore if you update
your CUE files you will need to remove these from the database and re-analyse,
or manually edit the meta-data in `bliss.db` using an SQLite GUI.
Ignoring tracks in mixes
@@ -261,19 +273,35 @@ accomplished be setting the `Ignore` column to `1` for such tracks. To make this
easier, `bliss-analyser` can read a text file containing items to ignore and
will update the database as appropriate.
This `ignore` file is a plain text file where each line contains the unique
path to be ignored. i.e. it could contain the complete path (relative to your
music folder) of a track, an album name (to exclude a whole album), or an artist
name (to exclude all tracks by the artist). e.g.
This `ignore` file is a plain text file where each line contains either:
1. The unique path to be ignored. i.e. it could contain the complete path
(relative to your music folder) of a track, an album name (to exclude a whole
album), or an artist name (to exclude all tracks by the artist).
2. An SQL selector. If so, line must start "SQL:" followed by code that will be
run after WHERE
```
ABBA/Gold - Greatest Hits/01 Dancing Queen.mp3
AC-DC/Power Up/
The Police/
SQL:Genre='Blues'
SQL:Genre LIKE '%Dance%'
SQL:Genre='Rock'
SQL:Genre LIKE 'Rock;%'
SQL:Genre LIKE '%;Rock'
SQL:Genre LIKE '%;Rock;%'
```
This would exclude 'Dancing Queen' by ABBA, all of AC/DC's 'Power Up', and all
tracks by 'The Police'
This would exclude 'Dancing Queen' by ABBA, all of AC/DC's 'Power Up', all
tracks by 'The Police', all tracks with 'Genre' set to 'Blues', and all tracks
that have 'Dance' or 'Rock' as part of their 'Genre'.
The SQL LIKE lines do sub-string matching. So '%Dance%' will match any genre
string that contains 'Dance' - e.g. 'Classical Dance'. The 4 lines with 'Rock'
show how you can explicitly look for an exact match. The 1st line means 'Rock'
is the only genre, 2nd means 'Rock' is the first genre, 3rd means 'Rock' is the
last genre, and 4th means 'Rock' is amongs other genres.
Assuming `config.ini` is in the current folder and contains valid entries, this
is accomplished as follows:
+16 -2
View File
@@ -7,7 +7,7 @@
# MIT license.
#
import datetime, os, requests, sys, time
import datetime, os, requests, shutil, subprocess, sys, tempfile, time
GITHUB_TOKEN_FILE = "%s/.config/github-token" % os.path.expanduser('~')
GITHUB_REPO = "CDrummond/bliss-analyser"
@@ -67,6 +67,20 @@ def download_artifacts(version):
break
def make_executable(version):
cwd = os.getcwd()
for a in ["bliss-analyser-linux", "bliss-analyser-mac"]:
archive = "%s-%s.zip" % (a, version)
info("Making analyser executable in %s" % archive)
with tempfile.TemporaryDirectory() as td:
subprocess.call(["unzip", archive, "-d", td], shell=False)
os.remove(archive)
os.chdir(td)
subprocess.call(["chmod", "a+x", "%s/bliss-analyser" % td], shell=False)
shutil.make_archive("%s/%s-%s" % (cwd, a, version), "zip")
os.chdir(cwd)
def checkVersion(version):
try:
parts=version.split('.')
@@ -85,4 +99,4 @@ if version!="test":
checkVersion(version)
download_artifacts(version)
make_executable(version)
+201 -106
View File
@@ -6,161 +6,255 @@
*
**/
use anyhow::{Result};
use bliss_audio::{library::analyze_paths_streaming};
use indicatif::{ProgressBar, ProgressStyle};
use std::convert::TryInto;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use crate::db;
use crate::tags;
use anyhow::Result;
use bliss_audio::{analyze_paths};
use if_chain::if_chain;
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::HashSet;
use std::convert::TryInto;
use std::fs::{DirEntry, File};
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
const DONT_ANALYSE:&str = ".notmusic";
const MAX_TAG_ERRORS_TO_SHOW:usize = 25;
const DONT_ANALYSE: &str = ".notmusic";
const MAX_ERRORS_TO_SHOW: usize = 100;
const MAX_TAG_ERRORS_TO_SHOW: usize = 50;
const VALID_EXTENSIONS: [&str; 5] = ["m4a", "mp3", "ogg", "flac", "opus"];
fn get_file_list(db:&mut db::Db, mpath:&PathBuf, path:&PathBuf, track_paths:&mut Vec<String>) {
if path.is_dir() {
match path.read_dir() {
Ok(items) => {
for item in items {
match item {
Ok(entry) => {
let pb = entry.path().to_path_buf();
if entry.path().is_dir() {
let mut check = pb.clone();
check.push(PathBuf::from(DONT_ANALYSE));
if check.exists() {
log::info!("Skipping '{}', found '{}'", pb.to_string_lossy(), DONT_ANALYSE);
} else {
get_file_list(db, mpath, &entry.path(), track_paths);
}
} else if entry.path().is_file() {
let e = pb.extension();
if e.is_some() {
let ext = e.unwrap().to_string_lossy();
if ext=="m4a" || ext=="mp3" || ext=="ogg" || ext=="flac" || ext=="opus" {
match pb.strip_prefix(mpath) {
Ok(stripped) => {
let mut cue = pb.clone();
cue.set_extension("cue");
if cue.exists() {
log::warn!("Found CUE album '{}' - not currently handled!", pb.to_string_lossy());
} else {
let spb = stripped.to_path_buf();
let sname = String::from(spb.to_string_lossy());
match db.get_rowid(&sname) {
Ok(id) => {
if id<=0 {
track_paths.push(String::from(pb.to_string_lossy()));
}
},
Err(_) => { }
}
}
},
Err(_) => { }
}
}
}
}
},
Err(_) => { }
}
}
},
Err(_) => { }
fn get_file_list(db: &mut db::Db, mpath: &Path, path: &Path, track_paths: &mut Vec<String>) {
if !path.is_dir() {
return;
}
if let Ok(items) = path.read_dir() {
for item in items {
if let Ok(entry) = item {
check_dir_entry(db, mpath, entry, track_paths);
}
}
}
}
pub fn analyse_new_files(db:&db::Db, mpath: &PathBuf, track_paths:Vec<String>) -> Result<()> {
fn check_dir_entry(db: &mut db::Db, mpath: &Path, entry: DirEntry, track_paths: &mut Vec<String>) {
let pb = entry.path();
if pb.is_dir() {
let check = pb.join(DONT_ANALYSE);
if check.exists() {
log::info!("Skipping '{}', found '{}'", pb.to_string_lossy(), DONT_ANALYSE);
} else {
get_file_list(db, mpath, &pb, track_paths);
}
} else if pb.is_file() {
if_chain! {
if let Some(ext) = pb.extension();
let ext = ext.to_string_lossy();
if VALID_EXTENSIONS.contains(&&*ext);
if let Ok(stripped) = pb.strip_prefix(mpath);
then {
let sname = String::from(stripped.to_string_lossy());
let mut cue_file = pb.clone();
cue_file.set_extension("cue");
if cue_file.exists() {
// For cue files, check if first track is in DB
let mut track_path = pb.clone();
let ext = pb.extension().unwrap().to_string_lossy();
track_path.set_extension(format!("{}{}1", ext, db::CUE_MARKER));
let db_track = String::from(track_path.to_string_lossy());
if let Ok(id) = db.get_rowid(&db_track) {
if id<=0 {
track_paths.push(String::from(cue_file.to_string_lossy()));
}
}
} else {
if let Ok(id) = db.get_rowid(&sname) {
if id<=0 {
track_paths.push(String::from(pb.to_string_lossy()));
}
}
}
}
}
}
}
pub fn analyse_new_files(db: &db::Db, mpath: &PathBuf, track_paths: Vec<String>) -> Result<()> {
let total = track_paths.len();
let pb = ProgressBar::new(total.try_into().unwrap());
let style = ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}")
.progress_chars("=> ");
pb.set_style(style);
let progress = ProgressBar::new(total.try_into().unwrap()).with_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}")
.progress_chars("=> "),
);
let results = analyze_paths_streaming(track_paths)?;
let mut analysed = 0;
let mut failed = 0;
let mut tag_error:Vec<String> = Vec::new();
let mut failed: Vec<String> = Vec::new();
let mut tag_error: Vec<String> = Vec::new();
let mut reported_cue:HashSet<String> = HashSet::new();
log::info!("Analysing new tracks");
for (path, result) in results {
log::info!("Analysing new files");
for (path, result) in analyze_paths(track_paths) {
let pbuff = PathBuf::from(&path);
let stripped = pbuff.strip_prefix(mpath).unwrap();
let spbuff = stripped.to_path_buf();
let sname = String::from(spbuff.to_string_lossy());
pb.set_message(format!("{}", sname));
progress.set_message(format!("{}", sname));
let mut inc_progress = true; // Only want to increment progress once for cue tracks
match result {
Ok(track) => {
let cpath = String::from(path);
let meta = tags::read(&cpath);
if meta.title.is_empty() && meta.artist.is_empty() && meta.album.is_empty() && meta.genre.is_empty() {
tag_error.push(sname.clone());
}
match track.cue_info {
Some(cue) => {
match track.track_number {
Some(track_num) => {
let t_num = track_num.parse::<i32>().unwrap();
if reported_cue.contains(&cpath) {
inc_progress = false;
} else {
analysed += 1;
reported_cue.insert(cpath);
}
let meta = db::Metadata {
title: track.title.unwrap_or_default().to_string(),
artist: track.artist.unwrap_or_default().to_string(),
album: track.album.unwrap_or_default().to_string(),
album_artist: track.album_artist.unwrap_or_default().to_string(),
genre: track.genre.unwrap_or_default().to_string(),
duration: track.duration.as_secs() as u32
};
db.add_track(&sname, &meta, &track.analysis);
analysed += 1;
},
Err(_) => {
failed += 1;
// Remove prefix from audio_file_path
let pbuff = PathBuf::from(&cue.audio_file_path);
let stripped = pbuff.strip_prefix(mpath).unwrap();
let spbuff = stripped.to_path_buf();
let sname = String::from(spbuff.to_string_lossy());
let db_path = format!("{}{}{}", sname, db::CUE_MARKER, t_num);
db.add_track(&db_path, &meta, &track.analysis);
}
None => { failed.push(format!("{} - No track number?", sname)); }
}
}
None => {
// Use lofty to read tags here, and not bliss's, so that if update
// tags is ever used they are from the same source.
let mut meta = tags::read(&cpath);
if meta.is_empty() {
// Lofty failed? Try from bliss...
meta.title = track.title.unwrap_or_default().to_string();
meta.artist = track.artist.unwrap_or_default().to_string();
meta.album = track.album.unwrap_or_default().to_string();
meta.album_artist = track.album_artist.unwrap_or_default().to_string();
meta.genre = track.genre.unwrap_or_default().to_string();
meta.duration = track.duration.as_secs() as u32;
}
if meta.is_empty() {
tag_error.push(sname.clone());
}
db.add_track(&sname, &meta, &track.analysis);
analysed += 1;
}
}
}
Err(e) => { failed.push(format!("{} - {}", sname, e)); }
};
pb.inc(1);
if inc_progress {
progress.inc(1);
}
}
progress.finish_with_message(format!("{} Analysed. {} Failure(s).", analysed, failed.len()));
if !failed.is_empty() {
let total = failed.len();
failed.truncate(MAX_ERRORS_TO_SHOW);
log::error!("Failed to analyse the following file(s):");
for err in failed {
log::error!(" {}", err);
}
if total > MAX_ERRORS_TO_SHOW {
log::error!(" + {} other(s)", total - MAX_ERRORS_TO_SHOW);
}
}
pb.finish_with_message(format!("{} Analysed. {} Failure(s).", analysed, failed));
if !tag_error.is_empty() {
let total = tag_error.len();
tag_error.truncate(MAX_TAG_ERRORS_TO_SHOW);
log::error!("Failed to read tags of the folling track(s):");
log::error!("Failed to read tags of the following file(s):");
for err in tag_error {
log::error!(" {}", err);
}
if total>MAX_TAG_ERRORS_TO_SHOW {
if total > MAX_TAG_ERRORS_TO_SHOW {
log::error!(" + {} other(s)", total - MAX_TAG_ERRORS_TO_SHOW);
}
}
Ok(())
}
pub fn analyse_files(db_path: &str, mpath: &PathBuf, dry_run:bool, keep_old:bool, max_num_tracks:usize) {
let mut track_paths:Vec<String> = Vec::new();
pub fn analyse_files(db_path: &str, mpaths: &Vec<PathBuf>, dry_run: bool, keep_old: bool, max_num_tracks: usize) {
let mut db = db::Db::new(&String::from(db_path));
let cur = PathBuf::from(mpath);
let mut track_count_left = max_num_tracks;
db.init();
log::info!("Looking for new tracks");
get_file_list(&mut db, mpath, &cur, &mut track_paths);
log::info!("Num new tracks: {}", track_paths.len());
if !dry_run && max_num_tracks>0 && track_paths.len()>max_num_tracks {
log::info!("Only analysing {} tracks", max_num_tracks);
track_paths.truncate(max_num_tracks);
}
if !keep_old {
db.remove_old(mpath, dry_run);
db.remove_old(mpaths, dry_run);
}
if !dry_run {
if track_paths.len()>0 {
match analyse_new_files(&db, mpath, track_paths) {
Ok(_) => { },
Err(_) => { }
for path in mpaths {
let mpath = path.clone();
let cur = path.clone();
let mut track_paths: Vec<String> = Vec::new();
if mpaths.len() > 1 {
log::info!("Looking for new files in {}", mpath.to_string_lossy());
} else {
log::info!("Looking for new files");
}
get_file_list(&mut db, &mpath, &cur, &mut track_paths);
track_paths.sort();
log::info!("Num new files: {}", track_paths.len());
if dry_run {
if !track_paths.is_empty() {
log::info!("The following need to be analysed:");
for track in track_paths {
log::info!(" {}", track);
}
}
} else {
log::info!("No new tracks to analyse");
if max_num_tracks > 0 {
if track_paths.len() > track_count_left {
log::info!("Only analysing {} files", track_count_left);
track_paths.truncate(track_count_left);
}
track_count_left -= track_paths.len();
}
if !track_paths.is_empty() {
match analyse_new_files(&db, &mpath, track_paths) {
Ok(_) => { }
Err(e) => { log::error!("Analysis returned error: {}", e); }
}
} else {
log::info!("No new files to analyse");
}
if max_num_tracks > 0 && track_count_left <= 0 {
log::info!("File limit reached");
break;
}
}
}
db.close();
}
pub fn read_tags(db_path: &str, mpath: &PathBuf) {
pub fn read_tags(db_path: &str, mpaths: &Vec<PathBuf>) {
let db = db::Db::new(&String::from(db_path));
db.init();
db.update_tags(&mpath);
db.update_tags(&mpaths);
db.close();
}
@@ -171,11 +265,12 @@ pub fn update_ignore(db_path: &str, ignore_path: &PathBuf) {
db.init();
db.clear_ignore();
for (_index, line) in reader.lines().enumerate() {
let line = line.unwrap();
let mut lines = reader.lines();
while let Some(Ok(line)) = lines.next() {
if !line.is_empty() && !line.starts_with("#") {
db.set_ignore(&line);
}
}
db.close();
}
+187 -116
View File
@@ -6,47 +6,68 @@
*
**/
use crate::tags;
use bliss_audio::{Analysis, AnalysisIndex};
use indicatif::{ProgressBar, ProgressStyle};
use rusqlite::{Connection, params};
use rusqlite::{params, Connection};
use std::convert::TryInto;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::process;
use crate::tags;
pub const CUE_MARKER: &str = ".CUE_TRACK.";
pub struct FileMetadata {
pub rowid:usize,
pub file:String,
pub title:Option<String>,
pub artist:Option<String>,
pub album_artist:Option<String>,
pub album:Option<String>,
pub genre:Option<String>,
pub duration:u32
pub rowid: usize,
pub file: String,
pub title: Option<String>,
pub artist: Option<String>,
pub album_artist: Option<String>,
pub album: Option<String>,
pub genre: Option<String>,
pub duration: u32,
}
#[derive(Default, PartialEq)]
pub struct Metadata {
pub title:String,
pub artist:String,
pub album_artist:String,
pub album:String,
pub genre:String,
pub duration:u32
pub title: String,
pub artist: String,
pub album_artist: String,
pub album: String,
pub genre: String,
pub duration: u32,
}
impl Metadata {
pub fn is_empty(&self) -> bool {
self.title.is_empty()
&& self.artist.is_empty()
&& self.album_artist.is_empty()
&& self.album.is_empty()
&& self.genre.is_empty()
}
}
pub struct Db {
pub conn: Connection
pub conn: Connection,
}
impl Db {
pub fn new(path: &String) -> Self {
Self {
conn: Connection::open(path).unwrap(),
match Connection::open(path) {
Ok(conn) => {
Self {
conn: conn,
}
}
Err(e) => {
log::error!("Failed top open/create database. {}", e);
process::exit(-1);
}
}
}
pub fn init(&self) {
match self.conn.execute(
let cmd = self.conn.execute(
"CREATE TABLE IF NOT EXISTS Tracks (
File text primary key,
Title text,
@@ -76,39 +97,35 @@ impl Db {
Chroma8 real,
Chroma9 real,
Chroma10 real
);",[]) {
Ok(_) => { },
Err(_) => {
log::error!("Failed to create DB table");
process::exit(-1);
}
);",
[],
);
if cmd.is_err() {
log::error!("Failed to create DB table");
process::exit(-1);
}
match self.conn.execute("CREATE UNIQUE INDEX IF NOT EXISTS Tracks_idx ON Tracks(File)", []) {
Ok(_) => { },
Err(_) => {
log::error!("Failed to create DB index");
process::exit(-1);
}
let cmd = self.conn.execute("CREATE UNIQUE INDEX IF NOT EXISTS Tracks_idx ON Tracks(File)", []);
if cmd.is_err() {
log::error!("Failed to create DB index");
process::exit(-1);
}
}
pub fn close(self) {
match self.conn.close() {
Ok(_) => { },
Err(_) => { }
}
let _ = self.conn.close();
}
pub fn get_rowid(&self, path: &String) -> Result<usize, rusqlite::Error> {
let mut db_path = path.clone();
pub fn get_rowid(&self, path: &str) -> Result<usize, rusqlite::Error> {
let mut db_path = path.to_string();
if cfg!(windows) {
db_path = db_path.replace("\\", "/");
}
let mut stmt = self.conn.prepare("SELECT rowid FROM Tracks WHERE File=:path;")?;
let track_iter = stmt.query_map(&[(":path", &db_path)], |row| {
Ok(row.get(0)?)
}).unwrap();
let mut rowid:usize = 0;
let track_iter = stmt.query_map(&[(":path", &db_path)], |row| Ok(row.get(0)?)).unwrap();
let mut rowid: usize = 0;
for tr in track_iter {
rowid = tr.unwrap();
break;
@@ -116,63 +133,94 @@ impl Db {
Ok(rowid)
}
pub fn add_track(&self, path: &String, meta: &Metadata, analysis:&Analysis) {
pub fn add_track(&self, path: &String, meta: &Metadata, analysis: &Analysis) {
let mut db_path = path.clone();
if cfg!(windows) {
db_path = db_path.replace("\\", "/");
}
match self.get_rowid(&path) {
Ok(id) => {
if id<=0 {
if id <= 0 {
match self.conn.execute("INSERT INTO Tracks (File, Title, Artist, AlbumArtist, Album, Genre, Duration, Ignore, Tempo, Zcr, MeanSpectralCentroid, StdDevSpectralCentroid, MeanSpectralRolloff, StdDevSpectralRolloff, MeanSpectralFlatness, StdDevSpectralFlatness, MeanLoudness, StdDevLoudness, Chroma1, Chroma2, Chroma3, Chroma4, Chroma5, Chroma6, Chroma7, Chroma8, Chroma9, Chroma10) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
params![db_path, meta.title, meta.artist, meta.album_artist, meta.album, meta.genre, meta.duration, 0,
analysis[AnalysisIndex::Tempo], analysis[AnalysisIndex::Zcr], analysis[AnalysisIndex::MeanSpectralCentroid], analysis[AnalysisIndex::StdDeviationSpectralCentroid], analysis[AnalysisIndex::MeanSpectralRolloff],
analysis[AnalysisIndex::StdDeviationSpectralRolloff], analysis[AnalysisIndex::MeanSpectralFlatness], analysis[AnalysisIndex::StdDeviationSpectralFlatness], analysis[AnalysisIndex::MeanLoudness], analysis[AnalysisIndex::StdDeviationLoudness],
analysis[AnalysisIndex::Chroma1], analysis[AnalysisIndex::Chroma2], analysis[AnalysisIndex::Chroma3], analysis[AnalysisIndex::Chroma4], analysis[AnalysisIndex::Chroma5],
analysis[AnalysisIndex::Chroma6], analysis[AnalysisIndex::Chroma7], analysis[AnalysisIndex::Chroma8], analysis[AnalysisIndex::Chroma9], analysis[AnalysisIndex::Chroma10]]) {
Ok(_) => { },
Ok(_) => { }
Err(e) => { log::error!("Failed to insert '{}' into database. {}", path, e); }
}
} else {
match self.conn.execute("UPDATE Tracks SET Title=?, Artist=?, AlbumArtist=?, Album=?, Genre=?, Duration=?, Tempo=?, Zcr=?, MeanSpectralCentroid=?, StdDevSpectralCentroid=?, MeanSpectralRolloff=?, StdDevSpectralRolloff=?, MeanSpectralFlatness=?, StdDevSpectralFlatness=?, MeanLoudness=?, StdDevLoudness=?, Chroma1=?, Chroma2=?, Chroma3=?, Chroma4=?, Chroma5=?, Chroma6=?, Chroma7=?, Chroma8=?, Chroma9=?, Chroma10=? WHERE rowid=?);",
match self.conn.execute("UPDATE Tracks SET Title=?, Artist=?, AlbumArtist=?, Album=?, Genre=?, Duration=?, Tempo=?, Zcr=?, MeanSpectralCentroid=?, StdDevSpectralCentroid=?, MeanSpectralRolloff=?, StdDevSpectralRolloff=?, MeanSpectralFlatness=?, StdDevSpectralFlatness=?, MeanLoudness=?, StdDevLoudness=?, Chroma1=?, Chroma2=?, Chroma3=?, Chroma4=?, Chroma5=?, Chroma6=?, Chroma7=?, Chroma8=?, Chroma9=?, Chroma10=? WHERE rowid=?;",
params![meta.title, meta.artist, meta.album_artist, meta.album, meta.genre, meta.duration,
analysis[AnalysisIndex::Tempo], analysis[AnalysisIndex::Zcr], analysis[AnalysisIndex::MeanSpectralCentroid], analysis[AnalysisIndex::StdDeviationSpectralCentroid], analysis[AnalysisIndex::MeanSpectralRolloff],
analysis[AnalysisIndex::StdDeviationSpectralRolloff], analysis[AnalysisIndex::MeanSpectralFlatness], analysis[AnalysisIndex::StdDeviationSpectralFlatness], analysis[AnalysisIndex::MeanLoudness], analysis[AnalysisIndex::StdDeviationLoudness],
analysis[AnalysisIndex::Chroma1], analysis[AnalysisIndex::Chroma2], analysis[AnalysisIndex::Chroma3], analysis[AnalysisIndex::Chroma4], analysis[AnalysisIndex::Chroma5],
analysis[AnalysisIndex::Chroma6], analysis[AnalysisIndex::Chroma7], analysis[AnalysisIndex::Chroma8], analysis[AnalysisIndex::Chroma9], analysis[AnalysisIndex::Chroma10], id]) {
Ok(_) => { },
Ok(_) => { }
Err(e) => { log::error!("Failed to update '{}' in database. {}", path, e); }
}
}
},
}
Err(_) => { }
}
}
pub fn remove_old(&self, mpath:&Path, dry_run:bool) {
log::info!("Looking for non-existant tracks");
pub fn remove_old(&self, mpaths: &Vec<PathBuf>, dry_run: bool) {
log::info!("Looking for non-existent tracks");
let mut stmt = self.conn.prepare("SELECT File FROM Tracks;").unwrap();
let track_iter = stmt.query_map([], |row| {
Ok((row.get(0)?,))
}).unwrap();
let mut to_remove:Vec<String> = Vec::new();
let track_iter = stmt.query_map([], |row| Ok((row.get(0)?,))).unwrap();
let mut to_remove: Vec<String> = Vec::new();
for tr in track_iter {
let mut db_path:String = tr.unwrap().0;
let mut db_path: String = tr.unwrap().0;
let orig_path = db_path.clone();
match orig_path.find(CUE_MARKER) {
Some(s) => {
db_path.truncate(s);
}
None => {}
}
if cfg!(windows) {
db_path = db_path.replace("/", "\\");
}
let path = mpath.join(PathBuf::from(db_path.clone()));
let mut exists = false;
if !path.exists() {
to_remove.push(db_path);
for mpath in mpaths {
let path = mpath.join(PathBuf::from(db_path.clone()));
//log::debug!("Check if '{}' exists.", path.to_string_lossy());
if path.exists() {
exists = true;
break;
}
}
if !exists {
to_remove.push(orig_path);
}
}
log::info!("Num non-existant tracks: {}", to_remove.len());
if !dry_run {
for t in to_remove {
match self.conn.execute("DELETE FROM Tracks WHERE File = ?;", params![t]) {
Ok(_) => { },
Err(_) => { }
let num_to_remove = to_remove.len();
log::info!("Num non-existent tracks: {}", num_to_remove);
if num_to_remove > 0 {
if dry_run {
log::info!("The following need to be removed from database:");
for t in to_remove {
log::info!(" {}", t);
}
} else {
let count_before = self.get_track_count();
for t in to_remove {
//log::debug!("Remove '{}'", t);
let cmd = self.conn.execute("DELETE FROM Tracks WHERE File = ?;", params![t]);
if let Err(e) = cmd {
log::error!("Failed to remove '{}' - {}", t, e)
}
}
let count_now = self.get_track_count();
if (count_now + num_to_remove) != count_before {
log::error!("Failed to remove all tracks. Count before: {}, wanted to remove: {}, count now: {}", count_before, num_to_remove, count_now);
}
}
}
@@ -180,10 +228,8 @@ impl Db {
pub fn get_track_count(&self) -> usize {
let mut stmt = self.conn.prepare("SELECT COUNT(*) FROM Tracks;").unwrap();
let track_iter = stmt.query_map([], |row| {
Ok(row.get(0)?)
}).unwrap();
let mut count:usize = 0;
let track_iter = stmt.query_map([], |row| Ok(row.get(0)?)).unwrap();
let mut count: usize = 0;
for tr in track_iter {
count = tr.unwrap();
break;
@@ -191,69 +237,94 @@ impl Db {
count
}
pub fn update_tags(&self, mpath:&PathBuf) {
pub fn update_tags(&self, mpaths: &Vec<PathBuf>) {
let total = self.get_track_count();
if total>0 {
let pb = ProgressBar::new(total.try_into().unwrap());
let style = ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}")
.progress_chars("=> ");
pb.set_style(style);
if total > 0 {
let progress = ProgressBar::new(total.try_into().unwrap()).with_style(
ProgressStyle::default_bar()
.template(
"[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}",
)
.progress_chars("=> "),
);
let mut stmt = self.conn.prepare("SELECT rowid, File, Title, Artist, AlbumArtist, Album, Genre, Duration FROM Tracks ORDER BY File ASC;").unwrap();
let track_iter = stmt.query_map([], |row| {
Ok(FileMetadata {
rowid: row.get(0)?,
file: row.get(1)?,
title: row.get(2)?,
artist: row.get(3)?,
album_artist: row.get(4)?,
album: row.get(5)?,
genre: row.get(6)?,
duration: row.get(7)?,
let track_iter = stmt
.query_map([], |row| {
Ok(FileMetadata {
rowid: row.get(0)?,
file: row.get(1)?,
title: row.get(2)?,
artist: row.get(3)?,
album_artist: row.get(4)?,
album: row.get(5)?,
genre: row.get(6)?,
duration: row.get(7)?,
})
})
}).unwrap();
.unwrap();
let mut updated = 0;
for tr in track_iter {
let dbtags = tr.unwrap();
let dtags = Metadata{
title:dbtags.title.unwrap_or(String::new()),
artist:dbtags.artist.unwrap_or(String::new()),
album_artist:dbtags.album_artist.unwrap_or(String::new()),
album:dbtags.album.unwrap_or(String::new()),
genre:dbtags.genre.unwrap_or(String::new()),
duration:dbtags.duration
};
pb.set_message(format!("{}", dbtags.file));
let path = String::from(mpath.join(&dbtags.file).to_string_lossy());
let ftags = tags::read(&path);
if ftags.title.is_empty() && ftags.artist.is_empty() && ftags.album_artist.is_empty() && ftags.album.is_empty() && ftags.genre.is_empty() {
log::error!("Failed to read tags of '{}'", dbtags.file);
} else if ftags.duration!=dtags.duration || ftags.title!=dtags.title || ftags.artist!=dtags.artist || ftags.album_artist!=dtags.album_artist || ftags.album!=dtags.album || ftags.genre!=dtags.genre {
match self.conn.execute("UPDATE Tracks SET Title=?, Artist=?, AlbumArtist=?, Album=?, Genre=?, Duration=? WHERE rowid=?;",
params![ftags.title, ftags.artist, ftags.album_artist, ftags.album, ftags.genre, ftags.duration, dbtags.rowid]) {
Ok(_) => { updated += 1; },
Err(e) => { log::error!("Failed to update tags of '{}'. {}", dbtags.file, e); }
if !dbtags.file.contains(CUE_MARKER) {
let dtags = Metadata {
title: dbtags.title.unwrap_or_default(),
artist: dbtags.artist.unwrap_or_default(),
album_artist: dbtags.album_artist.unwrap_or_default(),
album: dbtags.album.unwrap_or_default(),
genre: dbtags.genre.unwrap_or_default(),
duration: dbtags.duration,
};
progress.set_message(format!("{}", dbtags.file));
for mpath in mpaths {
let track_path = mpath.join(&dbtags.file);
if track_path.exists() {
let path = String::from(track_path.to_string_lossy());
let ftags = tags::read(&path);
if ftags.is_empty() {
log::error!("Failed to read tags of '{}'", dbtags.file);
} else if ftags != dtags {
match self.conn.execute("UPDATE Tracks SET Title=?, Artist=?, AlbumArtist=?, Album=?, Genre=?, Duration=? WHERE rowid=?;",
params![ftags.title, ftags.artist, ftags.album_artist, ftags.album, ftags.genre, ftags.duration, dbtags.rowid]) {
Ok(_) => { updated += 1; }
Err(e) => { log::error!("Failed to update tags of '{}'. {}", dbtags.file, e); }
}
}
break;
}
}
}
pb.inc(1);
progress.inc(1);
}
pb.finish_with_message(format!("{} Updated.", updated))
progress.finish_with_message(format!("{} Updated.", updated))
}
}
pub fn clear_ignore(&self) {
match self.conn.execute("UPDATE Tracks SET Ignore=0;", []) {
Ok(_) => { },
Err(e) => { log::error!("Failed clear Ignore column. {}", e); }
let cmd = self.conn.execute("UPDATE Tracks SET Ignore=0;", []);
if let Err(e) = cmd {
log::error!("Failed clear Ignore column. {}", e);
}
}
pub fn set_ignore(&self, like:&str) {
log::info!("Ignore: {}", like);
match self.conn.execute(&format!("UPDATE Tracks SET Ignore=1 WHERE File LIKE \"{}%\"", like), []) {
Ok(_) => { },
Err(e) => { log::error!("Failed set Ignore column for '{}'. {}", like, e); }
pub fn set_ignore(&self, line: &str) {
log::info!("Ignore: {}", line);
if line.starts_with("SQL:") {
let sql = &line[4..];
let cmd = self.conn.execute(&format!("UPDATE Tracks Set Ignore=1 WHERE {}", sql), []);
if let Err(e) = cmd {
log::error!("Failed set Ignore column for '{}'. {}", line, e);
}
} else {
let cmd = self.conn.execute(&format!("UPDATE Tracks SET Ignore=1 WHERE File LIKE \"{}%\"", line), []);
if let Err(e) = cmd {
log::error!("Failed set Ignore column for '{}'. {}", line, e);
}
}
}
}
}
+46 -31
View File
@@ -5,6 +5,7 @@
* GPLv3 license.
*
**/
use argparse::{ArgumentParser, Store, StoreTrue};
use chrono::Local;
use configparser::ini::Ini;
@@ -18,9 +19,8 @@ mod db;
mod tags;
mod upload;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const TOP_LEVEL_INI_TAG:&str = "Bliss";
const TOP_LEVEL_INI_TAG: &str = "Bliss";
fn main() {
let mut config_file = "config.ini".to_string();
@@ -28,15 +28,18 @@ fn main() {
let mut logging = "info".to_string();
let mut music_path = ".".to_string();
let mut ignore_file = "ignore.txt".to_string();
let mut keep_old:bool = false;
let mut dry_run:bool = false;
let mut keep_old: bool = false;
let mut dry_run: bool = false;
let mut task = "".to_string();
let mut lms_host = "127.0.0.1".to_string();
let mut max_num_tracks:usize = 0;
let mut max_num_files: usize = 0;
let mut music_paths: Vec<PathBuf> = Vec::new();
match dirs::home_dir() {
Some(path) => { music_path = String::from(path.join("Music").to_string_lossy()); }
None => { }
Some(path) => {
music_path = String::from(path.join("Music").to_string_lossy());
}
None => {}
}
{
@@ -44,7 +47,7 @@ fn main() {
let music_path_help = format!("Music folder (default: {})", &music_path);
let db_path_help = format!("Database location (default: {})", &db_path);
let logging_help = format!("Log level; trace, debug, info, warn, error. (default: {})", logging);
let ignore_file_help = format!("File containg items to mark as ignored. (default: {})", ignore_file);
let ignore_file_help = format!("File contains items to mark as ignored. (default: {})", ignore_file);
let lms_host_help = format!("LMS hostname or IP address (default: {})", &lms_host);
let description = format!("Bliss Analyser v{}", VERSION);
@@ -56,29 +59,33 @@ fn main() {
arg_parse.refer(&mut music_path).add_option(&["-m", "--music"], Store, &music_path_help);
arg_parse.refer(&mut db_path).add_option(&["-d", "--db"], Store, &db_path_help);
arg_parse.refer(&mut logging).add_option(&["-l", "--logging"], Store, &logging_help);
arg_parse.refer(&mut keep_old).add_option(&["-k", "--keep-old"], StoreTrue, "Don't remove tracks from DB if they don't exist (used with analyse task)");
arg_parse.refer(&mut keep_old).add_option(&["-k", "--keep-old"], StoreTrue, "Don't remove files from DB if they don't exist (used with analyse task)");
arg_parse.refer(&mut dry_run).add_option(&["-r", "--dry-run"], StoreTrue, "Dry run, only show what needs to be done (used with analyse task)");
arg_parse.refer(&mut ignore_file).add_option(&["-i", "--ignore"], Store, &ignore_file_help);
arg_parse.refer(&mut lms_host).add_option(&["-L", "--lms"], Store, &lms_host_help);
arg_parse.refer(&mut max_num_tracks).add_option(&["-n", "--numtracks"], Store, "Maximum number of tracks to analyse");
arg_parse.refer(&mut max_num_files).add_option(&["-n", "--numfiles"], Store, "Maximum number of files to analyse");
arg_parse.refer(&mut task).add_argument("task", Store, "Task to perform; analyse, tags, ignore, upload, stopmixer.");
arg_parse.parse_args_or_exit();
}
if !(logging.eq_ignore_ascii_case("trace") || logging.eq_ignore_ascii_case("debug") || logging.eq_ignore_ascii_case("info") || logging.eq_ignore_ascii_case("warn") || logging.eq_ignore_ascii_case("error")) {
if !(logging.eq_ignore_ascii_case("trace") || logging.eq_ignore_ascii_case("debug") || logging.eq_ignore_ascii_case("info")
|| logging.eq_ignore_ascii_case("warn") || logging.eq_ignore_ascii_case("error")) {
logging = String::from("info");
}
let mut builder = env_logger::Builder::from_env(env_logger::Env::default().filter_or("XXXXXXXX", logging));
builder.filter(Some("bliss_audio"), LevelFilter::Error);
builder.format(|buf, record| writeln!(buf, "[{} {:.1}] {}", Local::now().format("%Y-%m-%d %H:%M:%S"), record.level(), record.args()));
builder.format(|buf, record| {
writeln!(buf, "[{} {:.1}] {}", Local::now().format("%Y-%m-%d %H:%M:%S"), record.level(), record.args())
});
builder.init();
if task.is_empty() {
if task.is_empty() {
log::error!("No task specified, please choose from; analyse, tags, ignore, upload");
process::exit(-1);
}
if !task.eq_ignore_ascii_case("analyse") && !task.eq_ignore_ascii_case("tags") && !task.eq_ignore_ascii_case("ignore") && !task.eq_ignore_ascii_case("upload") && !task.eq_ignore_ascii_case("stopmixer") {
if !task.eq_ignore_ascii_case("analyse") && !task.eq_ignore_ascii_case("tags") && !task.eq_ignore_ascii_case("ignore")
&& !task.eq_ignore_ascii_case("upload") && !task.eq_ignore_ascii_case("stopmixer") {
log::error!("Invalid task ({}) supplied", task);
process::exit(-1);
}
@@ -89,23 +96,26 @@ fn main() {
let mut config = Ini::new();
match config.load(&config_file) {
Ok(_) => {
match config.get(TOP_LEVEL_INI_TAG, "music") {
Some(val) => { music_path = val; },
None => { }
let path_keys: [&str; 5] = ["music", "music_1", "music_2", "music_3", "music_4"];
for key in &path_keys {
match config.get(TOP_LEVEL_INI_TAG, key) {
Some(val) => { music_paths.push(PathBuf::from(&val)); }
None => { }
}
}
match config.get(TOP_LEVEL_INI_TAG, "db") {
Some(val) => { db_path = val; },
Some(val) => { db_path = val; }
None => { }
}
match config.get(TOP_LEVEL_INI_TAG, "lms") {
Some(val) => { lms_host = val; },
Some(val) => { lms_host = val; }
None => { }
}
match config.get(TOP_LEVEL_INI_TAG, "ignore") {
Some(val) => { ignore_file = val; },
Some(val) => { ignore_file = val; }
None => { }
}
},
}
Err(e) => {
log::error!("Failed to load config file. {}", e);
process::exit(-1);
@@ -114,6 +124,10 @@ fn main() {
}
}
if music_paths.is_empty() {
music_paths.push(PathBuf::from(&music_path));
}
if task.eq_ignore_ascii_case("stopmixer") {
upload::stop_mixer(&lms_host);
} else {
@@ -136,18 +150,19 @@ fn main() {
process::exit(-1);
}
} else {
let mpath = PathBuf::from(&music_path);
if !mpath.exists() {
log::error!("Music path ({}) does not exist", music_path);
process::exit(-1);
}
if !mpath.is_dir() {
log::error!("Music path ({}) is not a directory", music_path);
process::exit(-1);
for mpath in &music_paths {
if !mpath.exists() {
log::error!("Music path ({}) does not exist", mpath.to_string_lossy());
process::exit(-1);
}
if !mpath.is_dir() {
log::error!("Music path ({}) is not a directory", mpath.to_string_lossy());
process::exit(-1);
}
}
if task.eq_ignore_ascii_case("tags") {
analyse::read_tags(&db_path, &mpath);
analyse::read_tags(&db_path, &music_paths);
} else if task.eq_ignore_ascii_case("ignore") {
let ignore_path = PathBuf::from(&ignore_file);
if !ignore_path.exists() {
@@ -160,7 +175,7 @@ fn main() {
}
analyse::update_ignore(&db_path, &ignore_path);
} else {
analyse::analyse_files(&db_path, &mpath, dry_run, keep_old, max_num_tracks);
analyse::analyse_files(&db_path, &music_paths, dry_run, keep_old, max_num_files);
}
}
}
+53 -63
View File
@@ -6,82 +6,72 @@
*
**/
use lofty::{Accessor, ItemKey, Probe};
use crate::db;
use lofty::{Accessor, AudioFile, ItemKey};
use regex::Regex;
use std::path::Path;
use substring::Substring;
use crate::db;
const MAX_GENRE_VAL:usize = 192;
const MAX_GENRE_VAL: usize = 192;
pub fn read(track:&String) -> db::Metadata {
let mut meta = db::Metadata{
title:String::new(),
artist:String::new(),
album:String::new(),
album_artist:String::new(),
genre:String::new(),
duration:180
pub fn read(track: &String) -> db::Metadata {
let mut meta = db::Metadata {
duration: 180,
..db::Metadata::default()
};
let path = Path::new(track);
match Probe::open(path) {
Ok(probe) => {
match probe.read(true) {
Ok(file) => {
let tag = match file.primary_tag() {
Some(primary_tag) => primary_tag,
None => file.first_tag().expect("Error: No tags found!"),
};
meta.title=tag.title().unwrap_or("").to_string();
meta.artist=tag.artist().unwrap_or("").to_string();
meta.album=tag.album().unwrap_or("").to_string();
meta.album_artist=tag.get_string(&ItemKey::AlbumArtist).unwrap_or("").to_string();
meta.genre=tag.genre().unwrap_or("").to_string();
// Check whether MP3 as numeric genre, and if so covert to text
if file.file_type().eq(&lofty::FileType::MP3) {
match tag.genre() {
Some(genre) => {
let test = &genre.parse::<u8>();
match test {
Ok(val) => {
let idx:usize = *val as usize;
if idx<MAX_GENRE_VAL {
meta.genre=lofty::id3::v1::GENRES[idx].to_string();
}
},
Err(_) => {
// Check for "(number)text"
let re = Regex::new(r"^\([0-9]+\)").unwrap();
if re.is_match(&genre) {
match genre.find(")") {
Some(end) => {
let test = &genre.to_string().substring(1, end).parse::<u8>();
match test {
Ok(val) => {
let idx:usize = *val as usize;
if idx<MAX_GENRE_VAL {
meta.genre=lofty::id3::v1::GENRES[idx].to_string();
}
},
Err(_) => { }
}
},
None => { }
if let Ok(file) = lofty::read_from_path(Path::new(track), true) {
let tag = match file.primary_tag() {
Some(primary_tag) => primary_tag,
None => file.first_tag().expect("Error: No tags found!"),
};
meta.title = tag.title().unwrap_or_default().to_string();
meta.artist = tag.artist().unwrap_or_default().to_string();
meta.album = tag.album().unwrap_or_default().to_string();
meta.album_artist = tag.get_string(&ItemKey::AlbumArtist).unwrap_or_default().to_string();
meta.genre = tag.genre().unwrap_or_default().to_string();
// Check whether MP3 as numeric genre, and if so covert to text
if file.file_type().eq(&lofty::FileType::MP3) {
match tag.genre() {
Some(genre) => {
let test = genre.parse::<u8>();
match test {
Ok(val) => {
let idx: usize = val as usize;
if idx < MAX_GENRE_VAL {
meta.genre = lofty::id3::v1::GENRES[idx].to_string();
}
}
Err(_) => {
// Check for "(number)text"
let re = Regex::new(r"^\([0-9]+\)").unwrap();
if re.is_match(&genre) {
match genre.find(")") {
Some(end) => {
let test = genre.to_string().substring(1, end).parse::<u8>();
if let Ok(val) = test {
let idx: usize = val as usize;
if idx < MAX_GENRE_VAL {
meta.genre =
lofty::id3::v1::GENRES[idx].to_string();
}
}
}
None => { }
}
},
None => {}
}
}
}
meta.duration=file.properties().duration().as_secs() as u32;
},
Err(_) => { }
}
None => { }
}
},
Err(_) => { }
}
meta.duration = file.properties().duration().as_secs() as u32;
}
meta
}
}
+40 -57
View File
@@ -12,93 +12,76 @@ use std::process;
use substring::Substring;
use ureq;
fn fail(msg:&str) {
fn fail(msg: &str) {
log::error!("{}", msg);
process::exit(-1);
}
pub fn stop_mixer(lms:&String) {
pub fn stop_mixer(lms: &String) {
let stop_req = "{\"id\":1, \"method\":\"slim.request\",\"params\":[\"\",[\"blissmixer\",\"stop\"]]}";
log::info!("Asking plugin to stop mixer");
match ureq::post(&format!("http://{}:9000/jsonrpc.js", lms)).send_string(&stop_req) {
Ok(_) => { },
Err(e) => { log::error!("Failed to ask plugin to stop mixer. {}", e); }
let req = ureq::post(&format!("http://{}:9000/jsonrpc.js", lms)).send_string(&stop_req);
if let Err(e) = req {
log::error!("Failed to ask plugin to stop mixer. {}", e);
}
}
pub fn upload_db(db_path:&String, lms:&String) {
pub fn upload_db(db_path: &String, lms: &String) {
// First tell LMS to restart the mixer in upload mode
let start_req = "{\"id\":1, \"method\":\"slim.request\",\"params\":[\"\",[\"blissmixer\",\"start-upload\"]]}";
let mut port:u16 = 0;
let mut port: u16 = 0;
log::info!("Requesting LMS plugin to allow uploads");
match ureq::post(&format!("http://{}:9000/jsonrpc.js", lms)).send_string(&start_req) {
Ok(resp) => {
match resp.into_string() {
Ok(text) => {
match text.find("\"port\":") {
Some(s) => {
let txt = text.to_string().substring(s+7, text.len()).to_string();
match txt.find("}") {
Some(e) => {
let p = txt.substring(0, e);
let test = p.parse::<u16>();
match test {
Ok(val) => {
port = val;
},
Err(_) => { fail("Could not parse resp (cast)"); }
}
},
None => { fail("Could not parse resp (closing)"); }
Ok(resp) => match resp.into_string() {
Ok(text) => match text.find("\"port\":") {
Some(s) => {
let txt = text.to_string().substring(s + 7, text.len()).to_string();
match txt.find("}") {
Some(e) => {
let p = txt.substring(0, e);
let test = p.parse::<u16>();
match test {
Ok(val) => { port = val; }
Err(_) => { fail("Could not parse resp (cast)"); }
}
},
None => { fail("Could not parse resp (no port)"); }
}
None => { fail("Could not parse resp (closing)"); }
}
},
Err(_) => { fail("No text?")}
}
None => { fail("Could not parse resp (no port)"); }
}
},
Err(e) => {
fail(&format!("Failed to ask LMS plugin to allow upload. {}", e));
Err(_) => fail("No text?"),
}
Err(e) => { fail(&format!("Failed to ask LMS plugin to allow upload. {}", e)); }
}
if port<=0 {
if port == 0 {
fail("Invalid port");
}
// Now we have port number, do the actual upload...
log::info!("Uploading {}", db_path);
match File::open(db_path) {
Ok(file) => {
match file.metadata() {
Ok(meta) => {
let buffered_reader = BufReader::new(file);
log::info!("Length: {}", meta.len());
match ureq::put(&format!("http://{}:{}/upload", lms, port))
.set("Content-Length", &meta.len().to_string())
.set("Content-Type", "application/octet-stream")
.send(buffered_reader) {
Ok(_) => {
log::info!("Database uploaded");
stop_mixer(lms);
},
Err(e) => {
fail(&format!("Failed to upload database. {}", e));
}
Ok(file) => match file.metadata() {
Ok(meta) => {
let buffered_reader = BufReader::new(file);
log::info!("Length: {}", meta.len());
match ureq::put(&format!("http://{}:{}/upload", lms, port))
.set("Content-Length", &meta.len().to_string())
.set("Content-Type", "application/octet-stream")
.send(buffered_reader) {
Ok(_) => {
log::info!("Database uploaded");
stop_mixer(lms);
}
},
Err(e) => {
fail(&format!("Failed to open database. {}", e));
Err(e) => { fail(&format!("Failed to upload database. {}", e)); }
}
}
},
Err(e) => {
fail(&format!("Failed to open database. {}", e));
Err(e) => { fail(&format!("Failed to open database. {}", e)); }
}
Err(e) => { fail(&format!("Failed to open database. {}", e)); }
}
}
}