Bump mp3 encoding to 320kbps, attach album cover and artist/album/title metadata to the file, handle file collision gracefully (useful for syncing playlists - download only missing files)

This commit is contained in:
Erik Kaju 2025-06-19 23:52:37 +03:00
parent afafa36263
commit dbf1db040b
7 changed files with 85 additions and 24 deletions

View file

@ -1,9 +1,6 @@
use flacenc::bitsink::ByteSink;
use flacenc::component::BitRepr;
use flacenc::error::Verify;
use super::execute_with_result;
use super::EncodedStream;
use super::Encoder;
use super::Samples;
@ -12,7 +9,9 @@ pub struct FlacEncoder;
#[async_trait::async_trait]
impl Encoder for FlacEncoder {
async fn encode(&self, samples: Samples) -> anyhow::Result<EncodedStream> {
async fn encode(&self, samples: &Samples, metadata: &crate::track::TrackMetadata, output_path: &str) -> anyhow::Result<()> {
let file_name = &metadata.track_name;
tracing::info!("Writing track: {:?} to file: {}", file_name, output_path);
let source = flacenc::source::MemSource::from_samples(
&samples.samples,
samples.channels as usize,
@ -26,7 +25,7 @@ impl Encoder for FlacEncoder {
let (tx, rx) = tokio::sync::oneshot::channel();
rayon::spawn(execute_with_result(
rayon::spawn(super::execute_with_result(
move || {
let flac_stream = flacenc::encode_with_fixed_block_size(
&config,
@ -35,7 +34,7 @@ impl Encoder for FlacEncoder {
)
.map_err(|e| anyhow::anyhow!("Failed to encode flac: {:?}", e))?;
let mut byte_sink = ByteSink::new();
let mut byte_sink = flacenc::bitsink::ByteSink::new();
flac_stream
.write(&mut byte_sink)
.map_err(|e| anyhow::anyhow!("Failed to write flac stream: {:?}", e))?;
@ -47,6 +46,7 @@ impl Encoder for FlacEncoder {
let byte_sink: Vec<u8> = rx.await??;
Ok(EncodedStream::new(byte_sink))
let stream = super::EncodedStream::new(byte_sink);
stream.write_to_file(output_path).await
}
}