add youtube download

This commit is contained in:
Oleg Sobolev 2024-03-23 22:55:48 +07:00
parent 584b36820a
commit 90e34b8108
6 changed files with 58 additions and 1383 deletions

1356
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -7,4 +7,3 @@ edition = "2021"
[dependencies]
rodio = "0.17.3"
rustube = { version = "0.6.0", features = ["blocking"] }

25
src/download.rs Normal file
View file

@ -0,0 +1,25 @@
use crate::track::TrackInfo;
use std::{path::Path, process::Command};
pub fn download_from_youtube(url: &str) -> TrackInfo {
let output = Command::new("yt-dlp")
.args([
"-o",
"%(id)s",
"--extract-audio",
"--audio-format",
"mp3",
"--print",
"%(id)s",
url,
])
.output()
.unwrap();
let filename = std::str::from_utf8(output.stdout.as_slice())
.unwrap()
.replace('\n', "")
+ ".mp3";
TrackInfo::new(&Path::new(filename.as_str()))
}

View file

@ -1,4 +1,8 @@
mod track;
pub mod channel;
pub mod download;
pub use track::TrackInfo;
use channel::{Requester, Responder, TryRecvError};
use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink};
@ -6,24 +10,10 @@ use std::{
collections::VecDeque,
fs::File,
io::BufReader,
path::{Path, PathBuf},
thread,
thread::JoinHandle,
};
#[derive(Debug, Clone)]
pub struct TrackInfo {
pub path: PathBuf,
}
impl TrackInfo {
pub fn new(path: &Path) -> TrackInfo {
TrackInfo {
path: PathBuf::from(path),
}
}
}
#[derive(Debug, Clone)]
enum WorkerRequest {
AddTrack(TrackInfo),
@ -57,7 +47,7 @@ struct Worker {
}
fn get_source(track: TrackInfo) -> Decoder<BufReader<File>> {
let file = BufReader::new(File::open(track.path).unwrap());
let file = BufReader::new(File::open(&track.path).unwrap());
Decoder::new(file).unwrap()
}

View file

@ -1,27 +1,15 @@
use music_bot::{MusicPlayer, TrackInfo};
use std::{path::Path, thread, time::Duration};
use rustube::{blocking::Video, url::Url};
use music_bot::{download, MusicPlayer, TrackInfo};
use std::{path::Path, process::Command, thread, time::Duration};
fn main() {
// let url = "https://www.youtube.com/watch?v=UnIhRpIT7nc";
// let url = Url::parse(url).unwrap();
// let video = Video::from_url(&url).unwrap();
// let stream = video.best_audio().unwrap();
// let path = stream.blocking_download().unwrap();
// let path = path.to_str().unwrap();
// println!("{path}")
let url = "https://www.youtube.com/watch?v=8bB0FNGlrEs";
let track = download::download_from_youtube(url);
let mut player = MusicPlayer::new();
player.enqueue(TrackInfo::new(&Path::new("music.mp3")));
player.enqueue(TrackInfo::new(&Path::new("music.mp3")));
// MusicPlayer::play(TrackInfo::new(&Path::new("music.mp3")));
for track in player.list_tracks() {
println!("{}", track.path.into_os_string().into_string().unwrap())
}
player.enqueue(track.clone());
player.enqueue(track.clone());
thread::sleep(Duration::from_secs(5));
player.skip_one();
thread::sleep(Duration::from_secs(4 * 60 + 13))
thread::sleep(Duration::from_secs(600));
}

13
src/track.rs Normal file
View file

@ -0,0 +1,13 @@
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct TrackInfo {
pub path: PathBuf,
}
impl TrackInfo {
pub fn new(path: &Path) -> TrackInfo {
TrackInfo {
path: PathBuf::from(path),
}
}
}