fix + add transcode

This commit is contained in:
JMARyA 2024-08-01 14:55:31 +02:00
parent 76e2641008
commit 95a2a71f25
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 50 additions and 4 deletions

View file

@ -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

View file

@ -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"

View file

@ -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;
}

View file

@ -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

View file

@ -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)

View file

@ -23,3 +23,9 @@ pub async fn track_audio_route(track_id: &str, lib: &State<Libary>) -> Option<Na
.await
.ok()
}
#[get("/track/<track_id>/audio/opus128")]
pub async fn track_audio_opus128_route(track_id: &str, lib: &State<Libary>) -> Option<NamedFile> {
let track = lib.get_track_by_id(track_id).await?;
NamedFile::open(track.get_opus(128)).await.ok()
}