add youtube download
This commit is contained in:
parent
584b36820a
commit
90e34b8108
6 changed files with 58 additions and 1383 deletions
1356
Cargo.lock
generated
1356
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -7,4 +7,3 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
rodio = "0.17.3"
|
||||
rustube = { version = "0.6.0", features = ["blocking"] }
|
||||
|
|
|
|||
25
src/download.rs
Normal file
25
src/download.rs
Normal 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()))
|
||||
}
|
||||
20
src/lib.rs
20
src/lib.rs
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
|||
26
src/main.rs
26
src/main.rs
|
|
@ -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
13
src/track.rs
Normal 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue