librmo 0.2.7

A library to manage media files and play them
Documentation
# libRMO - Rust Media ORM

Provides an ORM to structure referencing media files and metadata. Leveraging this information we can validate the metadata for presence.

From a single entry point of defining at least 1 root folder to start from, generate a full media library and run useful functions off them.

# Usage

```toml
[dependencies]
# Add to dependencies
librmo = "0.1.0"
```

Initialise the library, this generates the config `%XDG_CONFIG_HOME/liborm` and cache `%XDG_CACHE_HOME/liborm` folders, creates the sqlite DB and applies migrations

```rust
use librmo::migration;

migration::run().await;
```

Now the backend is ready, we can then provide a library directory to start collection from:

```rust
use librmo::media_manager as MM;

let lib_name = "MyMusic";
let lib_path = "/path/to/music";
MM::insert_library(lib_name, lib_path).await?;
```

We can confirm this library exists:

```rust
use librmo::media_manager as MM;
use librmo::entity::library::Model as LibraryModel

let libraries: Vec<LibraryModel> = MM::get_library().await?;
println!("{:?}", libraries);
// [Model { id: 1, name: "MyMusic", path: "/path/to/music", files: "" }]
```

Now we have a root directory to start from we set off the manager. This process is a simple walkdir for each file and we store them away in the database with their full path, modified time, and matching against an interal list of extension enums.\
This should be a fast process, ~12s for ~16k files

```rust
use librmo::media_manager as MM;

MM::collect_media().await?;
```
Then taking each media file in the directory structure we need to work out which music fles we could care about\
~20s for ~12k files

```rust
use librmo::media_checker as MC;

MC::check_media().await?;
```

Now onto populating the remainder of the ORM. This is a much more demanding function, currently generates all the valid `track` and `album` records and the album art thumbnail. This is where we read all the tag metadata present on the music records\
~30mins for ~12k valid tracks\
(~5mins without album art generation)

```rust
use librmo::media_checker as MC;

MC::check_albums().await?;
```

So a full process can be
```rust
migration::run().await?;
let lib_name = "MyMusic";
let lib_path = "/home/samuel/Music";
MM::insert_library(lib_name, lib_path).await?;
MM::collect_media().await?;
MC::check_media().await?;
MC::check_albums().await?;
```

SeaORM handles repeated migration so its safe to have this function run at every startup of your implementation. Since the entity model library enforces uniqueness on the data an insert of the same library entry *can* be done without panicing. The checking also leverages the modified time of the files within the directory to only run the expensive tag retrieval on files that have changed since the last run.

We can then query the `report` table to find entries that do not have enough tag information

```rust
use librmo::media_validator as MV;

let reports = MV::get_reports().await?;
println!("{} Reports - {:?}", reports.len(), reports);
//14 Reports - [ReportList { report_type: "ALBUMID", path: "/home/samuel/Music/sample_mp3.mp3" }, ... }
```
Here we see an entry is created for each tag the file is missing alongside the path to the file.

Album art can be generated from the `Front Cover` entry via the mb_release_id property on either a `track` or `album`
```rust
let tracks = MC::get_tracks().await?;
//or
let albums = MC::get_albums().await?;
// Use your favourite method of selecting a record
let release_id = tracks[albums.len() - 1].mb_release_id.clone();
MC::generate_album_art_by_release_id(release_id)
MC::generate_album_art_full_by_release_id(release_id)
```

The resulting jpg images will be in `%XDG_CACHE_HOME/liborm/album_art` or `%XDG_CACHE_HOME/liborm/album_art_full` with the filename `<mb_release_id>.jpg`

# Build

Fedora
```bash
sudo dnf install gcc pkg-config perl-FindBin openssl-devel alsa-lib-devel
```

# Future features/improvements

## Bugs and Performance

-   check_media should first get all `music` records and compare there rather than 1 by 1
-   ~~Seperate album art thumbnail generation out into async function, also allow it to be called on demand~~
-   Reset DB, currently slow if you delete the library record
-   ~~Allow clearing down or `report` table~~
-   ~~Delete media records that are no longer present in the library~~
-   ~~Delete albums if no tracks remain after clearing deleted media~~
-   Speed up report insert operation
-   Better description of missing tags in report entries

## Features

-   Allow override of where DB and cache storage is set
-   Allow choice of artwork thumbnail file format, currently locked at jpg
-   Adjust resolution of output file, locked at 300x300
-   Check size of artwork and add `report` entry if too small
-   Logger settings and useful messages