MP3 Support (#14)

This commit is contained in:
Guillem Castro 2024-05-17 18:06:31 +02:00 committed by GitHub
parent 61d6268524
commit 9be6396112
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 529 additions and 81 deletions

View file

@ -4,6 +4,9 @@ on:
push: push:
branches: branches:
- '*' - '*'
pull_request:
branches:
- '*'
env: env:
CARGO_TERM_COLOR: always CARGO_TERM_COLOR: always

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
/target /target
.vscode .vscode
*.flac *.flac
*.mp3
bin bin
debug/ debug/

126
Cargo.lock generated
View file

@ -98,7 +98,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -138,6 +138,15 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "autotools"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef941527c41b0fc0dd48511a8154cd5fc7e29200a0ff8b7203c5d777dbc795cf"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.71" version = "0.3.71"
@ -184,7 +193,7 @@ dependencies = [
"regex", "regex",
"rustc-hash", "rustc-hash",
"shlex", "shlex",
"syn 2.0.63", "syn 2.0.64",
"which", "which",
] ]
@ -369,6 +378,25 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.19" version = "0.8.19"
@ -446,9 +474,9 @@ dependencies = [
[[package]] [[package]]
name = "either" name = "either"
version = "1.11.0" version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b"
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
@ -595,7 +623,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -649,9 +677,9 @@ dependencies = [
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.15" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
@ -1000,9 +1028,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.154" version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -1228,9 +1256,9 @@ dependencies = [
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.13" version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -1342,6 +1370,27 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "mp3lame-encoder"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64622e64e2f9ee2a2fee97a6f53bd8b0301fa0fd15f2f4152f910091949689c7"
dependencies = [
"libc",
"mp3lame-sys",
]
[[package]]
name = "mp3lame-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99896fd43ed58e957c164c5f4a1e7d8e770f358835cc6920b2eab7ab6d1a7106"
dependencies = [
"autotools",
"cc",
"libc",
]
[[package]] [[package]]
name = "mp4ameta" name = "mp4ameta"
version = "0.11.0" version = "0.11.0"
@ -1577,7 +1626,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -1697,6 +1746,26 @@ dependencies = [
"rand", "rand",
] ]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "readme-rustdocifier" name = "readme-rustdocifier"
version = "0.1.1" version = "0.1.1"
@ -1836,9 +1905,9 @@ dependencies = [
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.16" version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "092474d1a01ea8278f69e6a358998405fae5b8b963ddaeb2b0b04a128bf1dfb0" checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]] [[package]]
name = "ryu" name = "ryu"
@ -1866,22 +1935,22 @@ checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.201" version = "1.0.202"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c" checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.201" version = "1.0.202"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -2002,10 +2071,13 @@ dependencies = [
"audiotags", "audiotags",
"dirs", "dirs",
"flacenc", "flacenc",
"futures",
"indicatif", "indicatif",
"lazy_static", "lazy_static",
"librespot", "librespot",
"machine-uid", "machine-uid",
"mp3lame-encoder",
"rayon",
"regex", "regex",
"rpassword 7.3.1", "rpassword 7.3.1",
"structopt", "structopt",
@ -2063,9 +2135,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.63" version = "2.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2119,7 +2191,7 @@ checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -2185,7 +2257,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -2237,7 +2309,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]
[[package]] [[package]]
@ -2403,7 +2475,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2425,7 +2497,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2655,5 +2727,5 @@ checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn 2.0.64",
] ]

View file

@ -25,3 +25,10 @@ tracing-subscriber = { version = "0.3", features = ["env-filter", "registry"] }
lazy_static = "1.4" lazy_static = "1.4"
async-trait = "0.1" async-trait = "0.1"
dirs = "5.0" dirs = "5.0"
mp3lame-encoder = { version = "0.1.5", optional = true }
futures = "0.3"
rayon = "1.10"
[features]
default = ["mp3"]
mp3 = ["dep:mp3lame-encoder"]

82
src/channel_sink.rs Normal file
View file

@ -0,0 +1,82 @@
use librespot::playback::audio_backend::Sink;
use librespot::playback::audio_backend::SinkError;
use librespot::playback::convert::Converter;
use librespot::playback::decoder::AudioPacket;
use crate::track::TrackMetadata;
pub enum SinkEvent {
Write { bytes: usize, total: usize, content: Vec<i32> },
Finished,
}
pub type SinkEventChannel = tokio::sync::mpsc::UnboundedReceiver<SinkEvent>;
pub struct ChannelSink {
sender: tokio::sync::mpsc::UnboundedSender<SinkEvent>,
bytes_total: usize,
bytes_sent: usize,
}
impl ChannelSink {
pub fn new(track: TrackMetadata) -> (Self, SinkEventChannel) {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
(
ChannelSink {
sender: tx,
bytes_sent: 0,
bytes_total: Self::convert_track_duration_to_size(&track),
},
rx,
)
}
fn convert_track_duration_to_size(metadata: &TrackMetadata) -> usize {
let duration = metadata.duration / 1000;
let sample_rate = 44100;
let channels = 2;
let bits_per_sample = 16;
let bytes_per_sample = bits_per_sample / 8;
(duration as usize) * sample_rate * channels * bytes_per_sample * 2
}
pub fn get_approximate_size(&self) -> usize {
self.bytes_total
}
}
impl Sink for ChannelSink {
fn start(&mut self) -> Result<(), SinkError> {
Ok(())
}
fn stop(&mut self) -> Result<(), SinkError> {
tracing::info!("Finished sending song");
self.sender
.send(SinkEvent::Finished)
.map_err(|_| SinkError::OnWrite("Failed to send finished event".to_string()))?;
Ok(())
}
fn write(&mut self, packet: AudioPacket, converter: &mut Converter) -> Result<(), SinkError> {
let data = converter.f64_to_s16(
packet
.samples()
.map_err(|_| SinkError::OnWrite("Failed to get samples".to_string()))?,
);
let data32: Vec<i32> = data.iter().map(|el| i32::from(*el)).collect();
self.bytes_sent += data32.len() * std::mem::size_of::<i32>();
self.sender
.send(SinkEvent::Write {
bytes: self.bytes_sent,
total: self.bytes_total,
content: data32,
})
.map_err(|_| SinkError::OnWrite("Failed to send event".to_string()))?;
Ok(())
}
}

View file

@ -1,8 +1,10 @@
use std::fmt::Write; use std::fmt::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::time::Duration;
use anyhow::Result; use anyhow::Result;
use futures::StreamExt;
use futures::TryStreamExt;
use indicatif::MultiProgress; use indicatif::MultiProgress;
use indicatif::ProgressBar; use indicatif::ProgressBar;
use indicatif::ProgressState; use indicatif::ProgressState;
@ -13,8 +15,10 @@ use librespot::playback::mixer::NoOpVolume;
use librespot::playback::mixer::VolumeGetter; use librespot::playback::mixer::VolumeGetter;
use librespot::playback::player::Player; use librespot::playback::player::Player;
use crate::file_sink::FileSink; use crate::channel_sink::ChannelSink;
use crate::file_sink::SinkEvent; use crate::encoder::Format;
use crate::encoder::Samples;
use crate::channel_sink::SinkEvent;
use crate::track::Track; use crate::track::Track;
use crate::track::TrackMetadata; use crate::track::TrackMetadata;
@ -29,16 +33,18 @@ pub struct DownloadOptions {
pub destination: PathBuf, pub destination: PathBuf,
pub compression: Option<u32>, pub compression: Option<u32>,
pub parallel: usize, pub parallel: usize,
pub format: Format,
} }
impl DownloadOptions { impl DownloadOptions {
pub fn new(destination: Option<String>, compression: Option<u32>, parallel: usize) -> Self { pub fn new(destination: Option<String>, compression: Option<u32>, parallel: usize, format: Format) -> Self {
let destination = let destination =
destination.map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from); destination.map_or_else(|| std::env::current_dir().unwrap(), PathBuf::from);
DownloadOptions { DownloadOptions {
destination, destination,
compression, compression,
parallel, parallel,
format
} }
} }
} }
@ -57,23 +63,13 @@ impl Downloader {
tracks: Vec<Track>, tracks: Vec<Track>,
options: &DownloadOptions, options: &DownloadOptions,
) -> Result<()> { ) -> Result<()> {
let this = Arc::new(self); futures::stream::iter(tracks)
.map(|track| {
let chunks = tracks.chunks(options.parallel); self.download_track(track, options)
for chunk in chunks { })
let mut tasks = Vec::new(); .buffer_unordered(options.parallel)
for track in chunk { .try_collect::<Vec<_>>()
let t = track.clone(); .await?;
let downloader = this.clone();
let options = options.clone();
tasks.push(tokio::spawn(async move {
downloader.download_track(t, &options).await
}));
}
for task in tasks {
task.await??;
}
}
Ok(()) Ok(())
} }
@ -87,23 +83,24 @@ impl Downloader {
let path = options let path = options
.destination .destination
.join(file_name.clone()) .join(file_name.clone())
.with_extension("flac") .with_extension(options.format.extension())
.to_str() .to_str()
.ok_or(anyhow::anyhow!("Could not set the output path"))? .ok_or(anyhow::anyhow!("Could not set the output path"))?
.to_string(); .to_string();
let (file_sink, mut sink_channel) = FileSink::new(path.to_string(), metadata); let (sink, mut sink_channel) = ChannelSink::new(metadata);
let file_size = file_sink.get_approximate_size(); let file_size = sink.get_approximate_size();
let (mut player, _) = Player::new( let (mut player, _) = Player::new(
self.player_config.clone(), self.player_config.clone(),
self.session.clone(), self.session.clone(),
self.volume_getter(), self.volume_getter(),
move || Box::new(file_sink), move || Box::new(sink),
); );
let pb = self.progress_bar.add(ProgressBar::new(file_size as u64)); let pb = self.progress_bar.add(ProgressBar::new(file_size as u64));
pb.enable_steady_tick(Duration::from_millis(100));
pb.set_style(ProgressStyle::with_template("{spinner:.green} {msg} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")? pb.set_style(ProgressStyle::with_template("{spinner:.green} {msg} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")?
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-")); .progress_chars("#>-"));
@ -111,25 +108,38 @@ impl Downloader {
player.load(track.id, true, 0); player.load(track.id, true, 0);
let name = file_name.clone(); let mut samples = Vec::<i32>::new();
tokio::spawn(async move { tokio::spawn(async move {
while let Some(event) = sink_channel.recv().await { player.await_end_of_track().await;
match event { player.stop();
SinkEvent::Written { bytes, total } => {
tracing::trace!("Written {} bytes out of {}", bytes, total);
pb.set_position(bytes as u64);
}
SinkEvent::Finished => {
pb.finish_with_message(format!("Downloaded {}", name));
}
}
}
}); });
player.await_end_of_track().await; while let Some(event) = sink_channel.recv().await {
player.stop(); match event {
SinkEvent::Write { bytes, total, mut content } => {
tracing::trace!("Written {} bytes out of {}", bytes, total);
pb.set_position(bytes as u64);
samples.append(&mut content);
}
SinkEvent::Finished => {
tracing::info!("Finished downloading track: {:?}", file_name);
break;
}
}
}
tracing::info!("Downloaded track: {:?}", file_name); tracing::info!("Encoding track: {:?}", file_name);
pb.set_message(format!("Encoding {}", &file_name));
let samples = Samples::new(samples, 44100, 2, 16);
let encoder = crate::encoder::get_encoder(options.format);
let stream = encoder.encode(samples).await?;
pb.set_message(format!("Writing {}", &file_name));
tracing::info!("Writing track: {:?} to file: {}", file_name, &path);
stream.write_to_file(&path).await?;
pb.finish_with_message(format!("Downloaded {}", &file_name));
Ok(()) Ok(())
} }

52
src/encoder/flac.rs Normal file
View file

@ -0,0 +1,52 @@
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;
#[derive(Debug)]
pub struct FlacEncoder;
#[async_trait::async_trait]
impl Encoder for FlacEncoder {
async fn encode(&self, samples: Samples) -> anyhow::Result<EncodedStream> {
let source = flacenc::source::MemSource::from_samples(
&samples.samples,
samples.channels as usize,
samples.bits_per_sample as usize,
samples.sample_rate as usize,
);
let config = flacenc::config::Encoder::default()
.into_verified()
.map_err(|e| anyhow::anyhow!("Failed to verify encoder config: {:?}", e))?;
let (tx, rx) = tokio::sync::oneshot::channel();
rayon::spawn(execute_with_result(
move || {
let flac_stream = flacenc::encode_with_fixed_block_size(
&config,
source,
config.block_size,
)
.map_err(|e| anyhow::anyhow!("Failed to encode flac: {:?}", e))?;
let mut byte_sink = ByteSink::new();
flac_stream
.write(&mut byte_sink)
.map_err(|e| anyhow::anyhow!("Failed to write flac stream: {:?}", e))?;
Ok(byte_sink.into_inner())
},
tx,
));
let byte_sink: Vec<u8> = rx.await??;
Ok(EncodedStream::new(byte_sink))
}
}

111
src/encoder/mod.rs Normal file
View file

@ -0,0 +1,111 @@
mod flac;
#[cfg(feature = "mp3")]
mod mp3;
use std::{path::Path, str::FromStr};
use anyhow::Result;
use tokio::sync::oneshot::Sender;
use self::{flac::FlacEncoder, mp3::Mp3Encoder};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Format {
Flac,
#[cfg(feature = "mp3")]
Mp3,
}
impl FromStr for Format {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"flac" => Ok(Format::Flac),
#[cfg(feature = "mp3")]
"mp3" => Ok(Format::Mp3),
_ => Err(anyhow::anyhow!("Unsupported format")),
}
}
}
impl Format {
pub fn extension(&self) -> &'static str {
match self {
Format::Flac => "flac",
#[cfg(feature = "mp3")]
Format::Mp3 => "mp3",
}
}
}
const FLAC_ENCODER: &FlacEncoder = &FlacEncoder;
#[cfg(feature = "mp3")]
const MP3_ENCODER: &Mp3Encoder = &Mp3Encoder;
pub fn get_encoder(format: Format) -> &'static dyn Encoder {
match format {
Format::Flac => FLAC_ENCODER,
#[cfg(feature = "mp3")]
Format::Mp3 => MP3_ENCODER,
}
}
#[async_trait::async_trait]
pub trait Encoder {
async fn encode(&self, samples: Samples) -> Result<EncodedStream>;
}
pub struct Samples {
pub samples: Vec<i32>,
pub sample_rate: u32,
pub channels: u32,
pub bits_per_sample: u32,
}
impl Samples {
pub fn new(samples: Vec<i32>, sample_rate: u32, channels: u32, bits_per_sample: u32) -> Self {
Samples {
samples,
sample_rate,
channels,
bits_per_sample,
}
}
}
pub struct EncodedStream {
pub stream: Vec<u8>,
}
impl EncodedStream {
pub fn new(stream: Vec<u8>) -> Self {
EncodedStream { stream }
}
pub async fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
if !path.as_ref().exists() {
tokio::fs::create_dir_all(
path.as_ref()
.parent()
.ok_or(anyhow::anyhow!("Could not create path"))?,
).await?;
}
tokio::fs::write(path, &self.stream).await?;
Ok(())
}
}
pub fn execute_with_result<F, T>(func: F, tx: Sender<anyhow::Result<T>>) -> impl FnOnce()
where
F: FnOnce() -> anyhow::Result<T> + Send + 'static,
T: Send + 'static,
{
move || {
let result = func();
// Ignore the error if the receiver has been dropped
let _ = tx.send(result);
}
}

72
src/encoder/mp3.rs Normal file
View file

@ -0,0 +1,72 @@
use anyhow::anyhow;
use anyhow::Ok;
use mp3lame_encoder::Builder;
use mp3lame_encoder::FlushNoGap;
use mp3lame_encoder::InterleavedPcm;
use super::execute_with_result;
use super::EncodedStream;
use super::Encoder;
use super::Samples;
pub struct Mp3Encoder;
impl Mp3Encoder {
fn build_encoder(
&self,
sample_rate: u32,
channels: u32,
) -> anyhow::Result<mp3lame_encoder::Encoder> {
let mut builder = Builder::new().ok_or(anyhow::anyhow!("Failed to create mp3 encoder"))?;
builder
.set_sample_rate(sample_rate)
.map_err(|e| anyhow::anyhow!("Failed to set sample rate for mp3 encoder: {}", e))?;
builder.set_num_channels(channels as u8).map_err(|e| {
anyhow::anyhow!("Failed to set number of channels for mp3 encoder: {}", e)
})?;
builder
.set_brate(mp3lame_encoder::Birtate::Kbps160)
.map_err(|e| anyhow::anyhow!("Failed to set bitrate for mp3 encoder: {}", e))?;
builder
.build()
.map_err(|e| anyhow::anyhow!("Failed to build mp3 encoder: {}", e))
}
}
#[async_trait::async_trait]
impl Encoder for Mp3Encoder {
async fn encode(&self, samples: Samples) -> anyhow::Result<EncodedStream> {
let mut mp3_encoder = self.build_encoder(samples.sample_rate, samples.channels)?;
let (tx, rx) = tokio::sync::oneshot::channel();
rayon::spawn(execute_with_result(
move || {
let samples: Vec<i16> = samples.samples.iter().map(|&x| x as i16).collect();
let input = InterleavedPcm(samples.as_slice());
let mut mp3_out_buffer = Vec::with_capacity(mp3lame_encoder::max_required_buffer_size(samples.len()));
let encoded_size = mp3_encoder
.encode(input, mp3_out_buffer.spare_capacity_mut())
.map_err(|e| anyhow!("Failed to encode mp3: {}", e))?;
unsafe {
mp3_out_buffer.set_len(mp3_out_buffer.len().wrapping_add(encoded_size));
}
let encoded_size = mp3_encoder
.flush::<FlushNoGap>(mp3_out_buffer.spare_capacity_mut())
.map_err(|e| anyhow!("Failed to flush mp3 encoder: {}", e))?;
unsafe {
mp3_out_buffer.set_len(mp3_out_buffer.len().wrapping_add(encoded_size));
}
Ok(mp3_out_buffer)
},
tx,
));
let mp3_out_buffer = rx.await??;
Ok(EncodedStream::new(mp3_out_buffer))
}
}

View file

@ -9,6 +9,8 @@ use librespot::playback::audio_backend::SinkError;
use librespot::playback::convert::Converter; use librespot::playback::convert::Converter;
use librespot::playback::decoder::AudioPacket; use librespot::playback::decoder::AudioPacket;
use crate::encoder::get_encoder;
use crate::encoder::Samples;
use crate::track::TrackMetadata; use crate::track::TrackMetadata;
pub enum SinkEvent { pub enum SinkEvent {
@ -67,18 +69,42 @@ impl Sink for FileSink {
fn stop(&mut self) -> Result<(), SinkError> { fn stop(&mut self) -> Result<(), SinkError> {
tracing::info!("Writing to file: {:?}", &self.sink); tracing::info!("Writing to file: {:?}", &self.sink);
let config = flacenc::config::Encoder::default() // let config = flacenc::config::Encoder::default()
.into_verified() // .into_verified()
.map_err(|_| SinkError::OnWrite("Failed to create flac encoder".to_string()))?; // .map_err(|_| SinkError::OnWrite("Failed to create flac encoder".to_string()))?;
let source = flacenc::source::MemSource::from_samples(&self.content, 2, 16, 44100); // let source = flacenc::source::MemSource::from_samples(&self.content, 2, 16, 44100);
let flac_stream = flacenc::encode_with_fixed_block_size(&config, source, config.block_size) // let flac_stream = flacenc::encode_with_fixed_block_size(&config, source, config.block_size)
.map_err(|_| SinkError::OnWrite("Failed to encode flac".to_string()))?; // .map_err(|_| SinkError::OnWrite("Failed to encode flac".to_string()))?;
let mut sink = flacenc::bitsink::ByteSink::new(); // let mut sink = flacenc::bitsink::ByteSink::new();
flac_stream // flac_stream
.write(&mut sink) // .write(&mut sink)
.map_err(|_| SinkError::OnWrite("Failed to write flac to sink".to_string()))?; // .map_err(|_| SinkError::OnWrite("Failed to write flac to sink".to_string()))?;
std::fs::write(&self.sink, sink.as_slice()) // std::fs::write(&self.sink, sink.as_slice())
// .map_err(|_| SinkError::OnWrite("Failed to write flac to file".to_string()))?;
let flac_enc = get_encoder(crate::encoder::Format::Flac)
.map_err(|_| SinkError::OnWrite("Failed to get flac encoder".to_string()))?;
let mp3_enc = get_encoder(crate::encoder::Format::Mp3)
.map_err(|_| SinkError::OnWrite("Failed to get mp3 encoder".to_string()))?;
let flac_stream = flac_enc.encode(Samples::new(
self.content.clone(),
44100,
2,
16,
))
.map_err(|_| SinkError::OnWrite("Failed to encode flac".to_string()))?;
let mp3_stream = mp3_enc.encode(Samples::new(
self.content.clone(),
44100,
2,
16,
))
.map_err(|_| SinkError::OnWrite("Failed to encode mp3".to_string()))?;
flac_stream.write_to_file(&self.sink)
.map_err(|_| SinkError::OnWrite("Failed to write flac to file".to_string()))?; .map_err(|_| SinkError::OnWrite("Failed to write flac to file".to_string()))?;
mp3_stream.write_to_file(&self.sink)
.map_err(|_| SinkError::OnWrite("Failed to write mp3 to file".to_string()))?;
let mut tag = Tag::new() let mut tag = Tag::new()
.with_tag_type(TagType::Flac) .with_tag_type(TagType::Flac)

View file

@ -1,4 +1,5 @@
pub mod download; pub mod download;
pub mod file_sink; pub mod channel_sink;
pub mod session; pub mod session;
pub mod track; pub mod track;
pub mod encoder;

View file

@ -1,4 +1,5 @@
use spotify_dl::download::{DownloadOptions, Downloader}; use spotify_dl::download::{DownloadOptions, Downloader};
use spotify_dl::encoder::Format;
use spotify_dl::session::create_session; use spotify_dl::session::create_session;
use spotify_dl::track::get_tracks; use spotify_dl::track::get_tracks;
use structopt::StructOpt; use structopt::StructOpt;
@ -41,6 +42,13 @@ struct Opt {
default_value = "5" default_value = "5"
)] )]
parallel: usize, parallel: usize,
#[structopt(
short = "f",
long = "format",
help = "The format to download the tracks in. Default is flac.",
default_value = "flac"
)]
format: Format
} }
pub fn configure_logger() { pub fn configure_logger() {
@ -84,7 +92,7 @@ async fn main() -> anyhow::Result<()> {
downloader downloader
.download_tracks( .download_tracks(
track, track,
&DownloadOptions::new(opt.destination, opt.compression, opt.parallel), &DownloadOptions::new(opt.destination, opt.compression, opt.parallel, opt.format),
) )
.await .await
} }

View file

@ -14,6 +14,7 @@ trait TrackCollection {
pub async fn get_tracks(spotify_ids: Vec<String>, session: &Session) -> Result<Vec<Track>> { pub async fn get_tracks(spotify_ids: Vec<String>, session: &Session) -> Result<Vec<Track>> {
let mut tracks: Vec<Track> = Vec::new(); let mut tracks: Vec<Track> = Vec::new();
for id in spotify_ids { for id in spotify_ids {
tracing::debug!("Getting tracks for: {}", id);
let id = parse_uri_or_url(&id).ok_or(anyhow::anyhow!("Invalid track"))?; let id = parse_uri_or_url(&id).ok_or(anyhow::anyhow!("Invalid track"))?;
let new_tracks = match id.audio_type { let new_tracks = match id.audio_type {
librespot::core::spotify_id::SpotifyAudioType::Track => vec![Track::from_id(id)], librespot::core::spotify_id::SpotifyAudioType::Track => vec![Track::from_id(id)],
@ -39,7 +40,9 @@ fn parse_uri_or_url(track: &str) -> Option<SpotifyId> {
} }
fn parse_uri(track_uri: &str) -> Option<SpotifyId> { fn parse_uri(track_uri: &str) -> Option<SpotifyId> {
SpotifyId::from_uri(track_uri).ok() let res = SpotifyId::from_uri(track_uri);
tracing::info!("Parsed URI: {:?}", res);
res.ok()
} }
fn parse_url(track_url: &str) -> Option<SpotifyId> { fn parse_url(track_url: &str) -> Option<SpotifyId> {