Skip to content

Commit

Permalink
yt-dlp downloaded if not found (work only for linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaalloc committed Jan 9, 2024
1 parent 1c0c1c3 commit 80a9eb3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rusqlite = { version = "0.29.0", features = ["bundled"] }
rand = "0.8.5"
env_logger = "0.10.0"
log="0.4.20"
lazy_static = "1.4.0"
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev", features = ["colored"] }
futures = "0.3.29"
youtube_dl = { version = "0.9.0", features = ["tokio", "downloader-native-tls"] }
Expand Down
18 changes: 12 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
use std::sync::Arc;
mod db;
mod music;
use crate::{api::*, music::player::Player};
use crate::{
api::*,
music::{player::Player, ytdlp_wrapper},
};
use db::{database, state::DbState};
use log::info;
use music::{
Expand Down Expand Up @@ -108,13 +111,13 @@ fn main() {
*app_state.db.lock().unwrap() = Some(db);

// check if ytdlp is installed
match music::ytdlp_wrapper::check_ytdl_bin() {
let app_dir = handle
.path_resolver()
.app_data_dir()
.expect("The app data directory should exist.");
match music::ytdlp_wrapper::check_ytdl_bin(app_dir.as_os_str()) {
Ok(_) => {}
Err(_) => {
let app_dir = handle
.path_resolver()
.app_data_dir()
.expect("The app data directory should exist.");
info!(
"yt-dlp not found, downloading it at {:?}",
app_dir.as_os_str()
Expand All @@ -124,6 +127,9 @@ fn main() {
.await
.unwrap();
assert!(path.is_file(), "downloaded file should exist");
info!("yt-dlp downloaded at {:?}", path);
let mut str = ytdlp_wrapper::YT_DLP_BIN_PATH.write().await;
*str = path.to_str().unwrap().to_string();
});
}
}
Expand Down
42 changes: 36 additions & 6 deletions src-tauri/src/music/ytdlp_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::process::Command;
use lazy_static::lazy_static;
use log::info;
use std::{ffi::OsStr, process::Command};
use tokio::sync::RwLock;

use futures::StreamExt;
use youtube_dl::{Error, YoutubeDl, YoutubeDlOutput};
Expand All @@ -12,6 +15,10 @@ pub enum ResultFromDownload {
Error(ErrorItem),
}

lazy_static! {
pub static ref YT_DLP_BIN_PATH: RwLock<String> = RwLock::new("yt-dlp".to_string());
}

#[derive(serde::Serialize, Clone)]
pub struct TotalItem {
pub total: usize,
Expand All @@ -36,9 +43,10 @@ impl std::fmt::Display for MusicItem {
}
}

fn retrieve_possible_links(url: &str) -> Result<Vec<MusicItem>, Error> {
fn retrieve_possible_links(url: &str, yt_dlp_path: &str) -> Result<Vec<MusicItem>, Error> {
let mut links: Vec<MusicItem> = Vec::new();
let retrieved = YoutubeDl::new(url)
.youtube_dl_path(yt_dlp_path)
.socket_timeout("15")
.flat_playlist(true)
.run();
Expand Down Expand Up @@ -66,7 +74,9 @@ fn retrieve_possible_links(url: &str) -> Result<Vec<MusicItem>, Error> {
}

async fn download_audio(url: &str, path: &str) -> Result<YoutubeDlOutput, Error> {
let yt_dlp_path = YT_DLP_BIN_PATH.read().await;
YoutubeDl::new(url)
.youtube_dl_path(yt_dlp_path.as_str())
.socket_timeout("15")
.extract_audio(true)
.extra_arg("-x")
Expand All @@ -86,13 +96,32 @@ async fn download_audio(url: &str, path: &str) -> Result<YoutubeDlOutput, Error>
.run_async().await
}

pub fn check_ytdl_bin() -> Result<bool, String> {
pub fn check_ytdl_bin(app_dir_path: &OsStr) -> Result<bool, String> {
// we check if user has yt-dlp installed
info!("Checking yt-dlp binary in PATH and at {:?}", app_dir_path);
match Command::new("yt-dlp").arg("--version").output() {
Ok(_) => Ok(true),
Ok(_) => {
info!("yt-dlp found in PATH");
Ok(true)
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Err("yt-dlp not found".to_string())
match Command::new(format!("{}/yt-dlp", app_dir_path.to_str().unwrap()))
.arg("--version")
.output()
{
Ok(_) => {
info!("yt-dlp found at {:?}", app_dir_path);
Ok(true)
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Err("yt-dlp not found".to_string())
} else {
Err(e.to_string())
}
}
}
} else {
Err(e.to_string())
}
Expand All @@ -106,7 +135,8 @@ pub async fn download_audio_from_links(
path: String,
state: tauri::State<'_, AsyncProcInputTx>,
) -> Result<(), String> {
let res_links = retrieve_possible_links(&url);
let yt_dlp_path = YT_DLP_BIN_PATH.read().await;
let res_links = retrieve_possible_links(&url, yt_dlp_path.as_str());
let links = match res_links {
Ok(links) => links,
Err(e) => {
Expand Down

0 comments on commit 80a9eb3

Please sign in to comment.