diff --git a/.gitignore b/.gitignore index 99ec2e6..bbb9760 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target -/db \ No newline at end of file +/db +/media +/data \ No newline at end of file diff --git a/src/library/track.rs b/src/library/track.rs index f8047f7..62f1be9 100644 --- a/src/library/track.rs +++ b/src/library/track.rs @@ -41,27 +41,33 @@ impl Track { } /// Transcode audio to OPUS with `bitrate` - pub fn get_opus(&self, bitrate: u32) -> String { - let transcoded = format!("./data/transcode/opus/{}/{}", bitrate, self._id); + pub fn get_opus(&self, bitrate: u32) -> Option { + let transcoded = format!("./data/transcode/opus/{}/{}.opus", bitrate, self._id); if std::path::Path::new(&transcoded).exists() { - return transcoded; + return Some(transcoded); } + log::info!("Transcoding {} to OPUS {}", self._id, bitrate); + std::fs::create_dir_all(format!("./data/transcode/opus/{bitrate}")).unwrap(); - std::process::Command::new("ffmpeg") + let out = std::process::Command::new("ffmpeg") .arg("-i") .arg(&self.path) .arg("-c:a") .arg("libopus") .arg("-b:a") - .arg(bitrate.to_string()) + .arg(format!("{bitrate}k")) .arg(&transcoded) .output() .unwrap(); - transcoded + if !out.status.success() { + return None; + } + + Some(transcoded) } /// Find tracks with no album or artist diff --git a/src/route/track.rs b/src/route/track.rs index 35e7670..0c36507 100644 --- a/src/route/track.rs +++ b/src/route/track.rs @@ -28,5 +28,5 @@ 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() + NamedFile::open(track.get_opus(128)?).await.ok() }