diff --git a/Dockerfile b/Dockerfile index a1d7f66..71a6c9b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ RUN cargo build --release FROM debian:buster RUN apt update && apt upgrade -y -RUN apt install -y gnupg ca-certificates openssl +RUN apt install -y gnupg ca-certificates openssl ffmpeg COPY --from=builder /app/target/release/synthwave /synthwave diff --git a/docker-compose.yml b/docker-compose.yml index b41d6bd..36aa21a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,9 @@ services: # - "8080:8080" # depends_on: # - mongodb +# volumes: +# - ./data:/data +# - ./media:/media # environment: # - "DB_URI=mongodb://user:pass@mongodb:27017" # - "DB=synthwrld" diff --git a/src/library/mod.rs b/src/library/mod.rs index c9ff1af..4be72a1 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -106,6 +106,18 @@ impl Libary { .insert("meta".into(), metadata.0); } + if entry.as_object().unwrap().get("title").is_none() { + entry.as_object_mut().unwrap().insert( + "title".into(), + std::path::Path::new(path) + .file_stem() + .unwrap() + .to_str() + .unwrap() + .into(), + ); + } + Track::create(&entry.as_object().unwrap()).await; } diff --git a/src/library/track.rs b/src/library/track.rs index 7f43ff0..80fa6fe 100644 --- a/src/library/track.rs +++ b/src/library/track.rs @@ -36,6 +36,30 @@ impl Track { .unwrap(); } + /// Transcode audio to OPUS with `bitrate` + pub fn get_opus(&self, bitrate: u32) -> String { + let transcoded = format!("./data/transcode/opus/{}/{}", bitrate, self._id); + + if std::path::Path::new(&transcoded).exists() { + return transcoded; + } + + std::fs::create_dir_all(format!("./data/transcode/opus/{bitrate}")).unwrap(); + + std::process::Command::new("ffmpeg") + .arg("-i") + .arg(&self.path) + .arg("-c:a") + .arg("libopus") + .arg("-b:a") + .arg(bitrate.to_string()) + .arg(&transcoded) + .output() + .unwrap(); + + transcoded + } + pub async fn api(&self) -> serde_json::Value { let album_title = if let Some(album_ref) = &self.album_id { album_ref diff --git a/src/main.rs b/src/main.rs index 67754f7..6388d04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,9 @@ use library::Libary; mod library; mod route; +use library::user::{User, UserRole}; use rocket::routes; use rocket::{http::Method, launch}; -use library::user::{User, UserRole}; #[launch] async fn rocket() -> _ { @@ -22,7 +22,7 @@ async fn rocket() -> _ { .to_cors() .expect("error creating CORS options"); - let lib = Libary::new("/Users/angelo/Downloads/Music".into()).await; + let lib = Libary::new("/Users/angelo/Downloads/Music/syv".into()).await; lib.rescan().await; @@ -39,7 +39,8 @@ async fn rocket() -> _ { route::track::track_route, route::track::track_audio_route, route::album::album_cover_route, - route::user::login_route + route::user::login_route, + route::track::track_audio_opus128_route ], ) .manage(lib) diff --git a/src/route/track.rs b/src/route/track.rs index 827047e..8c73b35 100644 --- a/src/route/track.rs +++ b/src/route/track.rs @@ -23,3 +23,9 @@ pub async fn track_audio_route(track_id: &str, lib: &State) -> Option/audio/opus128")] +pub async fn track_audio_opus128_route(track_id: &str, lib: &State) -> Option { + let track = lib.get_track_by_id(track_id).await?; + NamedFile::open(track.get_opus(128)).await.ok() +}