From 84a63e1f7e3cce6304754d088e9a40aca1302a23 Mon Sep 17 00:00:00 2001 From: Guillem Castro Date: Fri, 10 Feb 2023 18:08:17 +0100 Subject: [PATCH 1/3] Update release.yml --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 78a12b5..393034f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,7 +27,7 @@ jobs: build: runs-on: ${{ matrix.platform }} - + needs: create_release strategy: fail-fast: false matrix: From 8a22a6659de7d43cea95a83e0a4f41932631875c Mon Sep 17 00:00:00 2001 From: schreifuchs Date: Fri, 12 Jan 2024 12:41:26 +0100 Subject: [PATCH 2/3] add option for compression --- .gitignore | 2 ++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 9 ++++++--- src/file_sink.rs | 40 +++++++++++++++++++++++++++++++--------- src/main.rs | 12 ++++++++++++ 6 files changed, 53 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 6c6fd4a..f8d4e5d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ target/ # These are backup files generated by rustfmt **/*.rs.bk + +.DS_Store diff --git a/Cargo.lock b/Cargo.lock index f1682f8..68f4071 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1916,7 +1916,7 @@ dependencies = [ [[package]] name = "spotify-dl" -version = "0.1.0" +version = "0.1.1" dependencies = [ "audiotags", "flac-bound", diff --git a/Cargo.toml b/Cargo.toml index 46ab953..396755d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "spotify-dl" -version = "0.1.0" +version = "0.1.1" authors = ["Guillem Castro "] edition = "2018" #links = "FLAC" diff --git a/README.md b/README.md index be0e129..febf70e 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ brew install flac ## Usage ``` -spotify-dl 0.1.0 +spotify-dl 0.1.1 A commandline utility to download music directly from Spotify USAGE: @@ -36,10 +36,13 @@ USAGE: FLAGS: -h, --help Prints help information - -o, --ordered Download songs in the order they are in the playlist, prfixing the filename with the track number + -o, --ordered Prefixing the filename with its index in the playlist -V, --version Prints version information OPTIONS: + -c, --compression Setting the flac compression level from 0 (fastest, least compression) to + 8 (slowest, most compression). A value larger than 8 will be reated as 8. + Defaults to 4. -d, --destination The directory where the songs will be downloaded [default: .] -p, --password Your Spotify password -u, --username Your Spotify username @@ -56,4 +59,4 @@ The usage of this software may infringe Spotify's ToS and/or your local legislat ## License -spotify-dl is licensed under the MIT license. See [LICENSE](LICENSE). +spotify-dl is lic:ewensed under the MIT license. See [LICENSE](LICENSE). diff --git a/src/file_sink.rs b/src/file_sink.rs index 8cfd812..221e562 100644 --- a/src/file_sink.rs +++ b/src/file_sink.rs @@ -1,24 +1,33 @@ use std::path::Path; use audiotags::{Tag, TagType}; -use librespot::{playback::{audio_backend::{Open, Sink, SinkError}, config::AudioFormat, decoder::AudioPacket, convert::Converter}}; +use librespot::playback::{ + audio_backend::{Open, Sink, SinkError}, + config::AudioFormat, + convert::Converter, + decoder::AudioPacket, +}; // extern crate flac_bound; -use flac_bound::{FlacEncoder}; +use flac_bound::FlacEncoder; use crate::TrackMetadata; pub struct FileSink { sink: String, content: Vec, - metadata: Option + metadata: Option, + compression: u32, } impl FileSink { pub fn add_metadata(&mut self, meta: TrackMetadata) { self.metadata = Some(meta); } + pub fn set_compression(&mut self, compression: u32) { + self.compression = compression; + } } impl Open for FileSink { @@ -28,7 +37,8 @@ impl Open for FileSink { FileSink { sink: file, content: Vec::new(), - metadata: None + metadata: None, + compression: 4, } } else { panic!(); @@ -42,21 +52,33 @@ impl Sink for FileSink { } fn stop(&mut self) -> Result<(), SinkError> { - let mut encoder = FlacEncoder::new().unwrap().channels(2).bits_per_sample(16).compression_level(4).init_file(&self.sink).unwrap(); - encoder.process_interleaved(self.content.as_slice(), (self.content.len()/2) as u32).unwrap(); + let mut encoder = FlacEncoder::new() + .unwrap() + .channels(2) + .bits_per_sample(16) + .compression_level(*&self.compression) + .init_file(&self.sink) + .unwrap(); + encoder + .process_interleaved(self.content.as_slice(), (self.content.len() / 2) as u32) + .unwrap(); encoder.finish().unwrap(); match &self.metadata { Some(meta) => { - let mut tag = Tag::new().with_tag_type(TagType::Flac).read_from_path(Path::new(&self.sink)).unwrap(); + let mut tag = Tag::new() + .with_tag_type(TagType::Flac) + .read_from_path(Path::new(&self.sink)) + .unwrap(); tag.set_album_title(&meta.album); for artist in &meta.artists { tag.add_artist(artist); } tag.set_title(&meta.track_name); - tag.write_to_path(&self.sink).expect("Failed to write metadata"); - }, + tag.write_to_path(&self.sink) + .expect("Failed to write metadata"); + } None => (), } Ok(()) diff --git a/src/main.rs b/src/main.rs index 754c28c..23c2cf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,13 @@ struct Opt { help = "Prefixing the filename with its index in the playlist" )] ordered: bool, + #[structopt( + short = "c", + long = "compression", + help = "Setting the flac compression level from 0 (fastest, least compression) to +8 (slowest, most compression). A value larger than 8 will be reated as 8. Defaults to 4." + )] + compression: Option, } #[derive(Clone)] @@ -67,6 +74,7 @@ async fn download_tracks( destination: PathBuf, tracks: Vec, ordered: bool, + compression: Option, ) { let player_config = PlayerConfig::default(); let bar_style = ProgressStyle::default_bar() @@ -116,6 +124,9 @@ async fn download_tracks( librespot::playback::config::AudioFormat::S16, ); file_sink.add_metadata(metadata); + if let Some(compression) = compression { + file_sink.set_compression(compression); + } let (mut player, _) = Player::new(player_config.clone(), session.clone(), None, move || { Box::new(file_sink) @@ -184,6 +195,7 @@ async fn main() { PathBuf::from(opt.destination), tracks, opt.ordered, + opt.compression, ) .await; } From 4c24d60ada7e8ddc6428a4ece45540311f82cb03 Mon Sep 17 00:00:00 2001 From: schreifuchs Date: Fri, 12 Jan 2024 13:07:14 +0100 Subject: [PATCH 3/3] fixed spelling --- README.md | 7 +++---- src/main.rs | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index febf70e..d11273b 100644 --- a/README.md +++ b/README.md @@ -41,15 +41,14 @@ FLAGS: OPTIONS: -c, --compression Setting the flac compression level from 0 (fastest, least compression) to - 8 (slowest, most compression). A value larger than 8 will be reated as 8. - Defaults to 4. + 8 (slowest, most compression). A value larger than 8 will be Treated as 8. + Default is 4. -d, --destination The directory where the songs will be downloaded [default: .] -p, --password Your Spotify password -u, --username Your Spotify username ARGS: - ... A list of Spotify URIs (songs, podcasts or playlists) -``` + ... A list of Spotify URIs (songs, podcasts or playlists)``` Songs and playlists must be passed as Spotify URIs or URLs (e.g. `spotify:track:123456789abcdefghABCDEF` for songs and `spotify:playlist:123456789abcdefghABCDEF` for playlists or `https://open.spotify.com/playlist/123456789abcdefghABCDEF?si=1234567890`). diff --git a/src/main.rs b/src/main.rs index 23c2cf6..06f3dbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ struct Opt { short = "c", long = "compression", help = "Setting the flac compression level from 0 (fastest, least compression) to -8 (slowest, most compression). A value larger than 8 will be reated as 8. Defaults to 4." +8 (slowest, most compression). A value larger than 8 will be Treated as 8. Default is 4." )] compression: Option, }