Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.
Rust downloadmanagerguihacktoberfesthomebrewlightweightlinuxlinux-distributionlocal-firstmacosmacos-packagemedia-toolboxopen-sourcerustsveltetauritrendingtrending-repositorieswindowswindows-desktopytdlp
feat: refactor YouTubeDownloader to use yt-dlp for video and playlist downloads
Tonho8a00aa333049
diff --git a/src-tauri/src/platforms/youtube/mod.rs b/src-tauri/src/platforms/youtube/mod.rsindex 72bb0473..bed38625 100644--- a/src-tauri/src/platforms/youtube/mod.rs+++ b/src-tauri/src/platforms/youtube/mod.rs@@ -1,12 +1,10 @@use std::collections::HashSet;-use std::io::Write;-use std::path::PathBuf;use anyhow::anyhow;use async_trait::async_trait;use tokio::sync::mpsc;-use crate::core::ffmpeg;+use crate::core::ytdlp;use crate::models::media::{DownloadOptions, DownloadResult, MediaInfo, MediaType, VideoQuality as MediaVideoQuality,};@@ -44,66 +42,114 @@ impl YouTubeDownloader {None}- fn is_bot_detection_error(err: &rusty_ytdl::VideoError) -> bool {- let msg = format!("{}", err);- msg.contains("Sign in to confirm")- || msg.contains("bot")- || msg.contains("429")- || msg.contains("consent")- || msg.contains("CAPTCHA")- }-- async fn is_ytdlp_available() -> bool {- tokio::process::Command::new("yt-dlp")- .arg("--version")- .output()- .await- .map(|o| o.status.success())- .unwrap_or(false)+ fn is_playlist_url(url: &str) -> bool {+ if let Ok(parsed) = url::Url::parse(url) {+ if parsed.path().starts_with("/playlist") {+ return true;+ }+ if parsed.query_pairs().any(|(k, _)| k == "list") {+ return true;+ }+ }+ false}- async fn get_info_ytdlp(video_id: &str) -> anyhow::Result<MediaInfo> {- let output = tokio::process::Command::new("yt-dlp")- .args([- "--dump-json",- "--no-playlist",- &format!("https://www.youtube.com/watch?v={}", video_id),- ])- .output()- .await- .map_err(|e| anyhow!("Falha ao executar yt-dlp: {}", e))?;-- if !output.status.success() {- let stderr = String::from_utf8_lossy(&output.stderr);- return Err(anyhow!("yt-dlp falhou: {}", stderr.trim()));+ fn extract_quality_height(quality_str: &str) -> Option<u32> {+ let s = quality_str.trim().to_lowercase();+ if s == "best" || s == "highest" {+ return None;}+ s.trim_end_matches('p').parse::<u32>().ok()+ }- let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;++ fn parse_video_info(json: &serde_json::Value) -> anyhow::Result<MediaInfo> {+ let video_id = json+ .get("id")+ .and_then(|v| v.as_str())+ .unwrap_or("unknown")+ .to_string();- let title = json.get("title")+ let title = json+ .get("title").and_then(|v| v.as_str()).unwrap_or("unknown").to_string();- let author = json.get("uploader")+ let author = json+ .get("uploader")+ .or_else(|| json.get("channel")).and_then(|v| v.as_str()).unwrap_or("unknown").to_string();let duration = json.get("duration").and_then(|v| v.as_f64());- let thumbnail = json.get("thumbnail")+ let thumbnail = json+ .get("thumbnail").and_then(|v| v.as_str()).map(|s| s.to_string());- // yt-dlp handles quality selection itself, just offer "best"- let qualities = vec![MediaVideoQuality {- label: "best".to_string(),- width: 0,- height: 0,- url: video_id.to_string(),- format: "ytdlp".to_string(),- }];+ let is_live = json+ .get("is_live")+ .and_then(|v| v.as_bool())+ .unwrap_or(false);++ if is_live {+ return Err(anyhow!("Livestreams não suportados"));+ }++ let mut qualities: Vec<MediaVideoQuality> = Vec::new();+ let mut seen_heights: HashSet<u32> = HashSet::new();++ if let Some(formats) = json.get("formats").and_then(|v| v.as_array()) {+ for f in formats {+ let height = f.get("height").and_then(|v| v.as_u64()).unwrap_or(0) as u32;+ let width = f.get("width").and_then(|v| v.as_u64()).unwrap_or(0) as u32;+ let vcodec = f+ .get("vcodec")+ .and_then(|v| v.as_str())+ .unwrap_or("none");+ let acodec = f+ .get("acodec")+ .and_then(|v| v.as_str())+ .unwrap_or("none");++ if vcodec == "none" || height == 0 {+ continue;+ }++ let has_audio = acodec != "none";++ if seen_heights.insert(height) {+ let label = if has_audio {+ format!("{}p", height)+ } else {+ format!("{}p (HD)", height)+ };++ qualities.push(MediaVideoQuality {+ label,+ width,+ height,+ url: format!("https://www.youtube.com/watch?v={}", video_id),+ format: "ytdlp".to_string(),+ });+ }+ }+ }++ qualities.sort_by(|a, b| b.height.cmp(&a.height));++ if qualities.is_empty() {+ qualities.push(MediaVideoQuality {+ label: "best".to_string(),+ width: 0,+ height: 0,+ url: format!("https://www.youtube.com/watch?v={}", video_id),+ format: "ytdlp".to_string(),+ });+ }Ok(MediaInfo {title: format!("youtube_{}", video_id),@@ -138,142 +184,49 @@ impl PlatformDownloader for YouTubeDownloader {}async fn get_media_info(&self, url: &str) -> anyhow::Result<MediaInfo> {- let video_id = Self::extract_video_id(url)- .ok_or_else(|| anyhow!("Não foi possível extrair o ID do vídeo YouTube"))?;-- let video = rusty_ytdl::Video::new(&video_id).map_err(|e| anyhow!("{}", e))?;-- match video.get_basic_info().await {- Ok(info) => {- let details = &info.video_details;-- if details.is_private {- return Err(anyhow!("Vídeo privado"));- }-- if details.age_restricted {- return Err(anyhow!("Conteúdo restrito por idade"));- }-- let title = details.title.clone();- let author = details- .author- .as_ref()- .map(|a| a.name.clone())- .unwrap_or_else(|| details.owner_channel_name.clone());-- let duration = details.length_seconds.parse::<f64>().ok();-- let thumbnail = details.thumbnails.last().map(|t| t.url.clone());-- let mut qualities: Vec<MediaVideoQuality> = Vec::new();- let mut seen_heights: HashSet<u32> = HashSet::new();-- let has_live_only = info- .formats- .iter()- .all(|f| f.is_live || (!f.has_video && !f.has_audio));- if has_live_only && details.is_live_content {- return Err(anyhow!("Livestreams não suportados"));- }-- let mut combined: Vec<&rusty_ytdl::VideoFormat> = info- .formats- .iter()- .filter(|f| f.has_video && f.has_audio && !f.is_live)- .collect();- combined.sort_by(|a, b| b.height.unwrap_or(0).cmp(&a.height.unwrap_or(0)));-- for f in &combined {- let height = f.height.unwrap_or(0) as u32;- let width = f.width.unwrap_or(0) as u32;- let label = f- .quality_label- .clone()- .unwrap_or_else(|| format!("{}p", height));-- if height > 0 && seen_heights.insert(height) {- qualities.push(MediaVideoQuality {- label: label.clone(),- width,- height,- url: video_id.clone(),- format: "mp4".to_string(),- });- }- }-- let mut adaptive_video: Vec<&rusty_ytdl::VideoFormat> = info- .formats- .iter()- .filter(|f| f.has_video && !f.has_audio && !f.is_live)- .filter(|f| {- f.mime_type.container == "mp4"- || f.mime_type.container == "webm"- })- .collect();- adaptive_video.sort_by(|a, b| b.height.unwrap_or(0).cmp(&a.height.unwrap_or(0)));-- for f in &adaptive_video {- let height = f.height.unwrap_or(0) as u32;- let width = f.width.unwrap_or(0) as u32;- let label = f- .quality_label- .clone()- .unwrap_or_else(|| format!("{}p", height));-- if height > 0 && !seen_heights.contains(&height) {- seen_heights.insert(height);- qualities.push(MediaVideoQuality {- label: format!("{} (HD)", label),- width,- height,- url: video_id.clone(),- format: "mp4+mux".to_string(),- });- }- }-- qualities.sort_by(|a, b| b.height.cmp(&a.height));-- if qualities.is_empty() {- qualities.push(MediaVideoQuality {- label: "best".to_string(),- width: 0,- height: 0,- url: video_id.clone(),- format: "mp4".to_string(),- });- }+ let ytdlp_path = ytdlp::ensure_ytdlp().await.map_err(|e| {+ anyhow!(+ "YouTube requer yt-dlp para funcionar. Falha ao obter yt-dlp: {}",+ e+ )+ })?;++ if Self::is_playlist_url(url) {+ let (playlist_title, entries) =+ ytdlp::get_playlist_info(&ytdlp_path, url).await?;++ if entries.is_empty() {+ return Err(anyhow!("Playlist vazia ou indisponível"));+ }- Ok(MediaInfo {- title: format!("youtube_{}", video_id),- author: format!("{} - {}", title, author),- platform: "youtube".to_string(),- duration_seconds: duration,- thumbnail_url: thumbnail,- available_qualities: qualities,- media_type: MediaType::Video,+ let qualities: Vec<MediaVideoQuality> = entries+ .into_iter()+ .enumerate()+ .map(|(i, entry)| MediaVideoQuality {+ label: format!("{}. {}", i + 1, entry.title),+ width: 0,+ height: 0,+ url: entry.url,+ format: "ytdlp_playlist".to_string(),})- }- Err(e) => {- // Map known errors- match &e {- rusty_ytdl::VideoError::VideoIsPrivate => return Err(anyhow!("Vídeo privado")),- rusty_ytdl::VideoError::VideoNotFound => return Err(anyhow!("Vídeo não encontrado")),- rusty_ytdl::VideoError::LiveStreamNotSupported => return Err(anyhow!("Livestreams não suportados")),- _ => {}- }+ .collect();++ return Ok(MediaInfo {+ title: sanitize_filename::sanitize(&playlist_title),+ author: playlist_title,+ platform: "youtube".to_string(),+ duration_seconds: None,+ thumbnail_url: None,+ available_qualities: qualities,+ media_type: MediaType::Playlist,+ });+ }- // Try yt-dlp fallback for bot detection errors- if Self::is_bot_detection_error(&e) && Self::is_ytdlp_available().await {- tracing::info!("YouTube bot detection, falling back to yt-dlp");- return Self::get_info_ytdlp(&video_id).await;- }+ let _video_id = Self::extract_video_id(url)+ .ok_or_else(|| anyhow!("Não foi possível extrair o ID do vídeo YouTube"))?;- Err(anyhow!("YouTube: {}", e))- }- }+ let json = ytdlp::get_video_info(&ytdlp_path, url).await?;+ Self::parse_video_info(&json)}async fn download(@@ -284,13 +237,19 @@ impl PlatformDownloader for YouTubeDownloader {) -> anyhow::Result<DownloadResult> {let _ = progress.send(0.0).await;- let video_id = info+ let ytdlp_path = ytdlp::ensure_ytdlp().await?;++ if info.media_type == MediaType::Playlist {+ return self+ .download_playlist(info, opts, progress, &ytdlp_path)+ .await;+ }++ let first = info.available_qualities.first()- .map(|q| q.url.clone()).ok_or_else(|| anyhow!("Nenhuma qualidade disponível"))?;- let first = info.available_qualities.first().unwrap();let selected = if let Some(ref wanted) = opts.quality {info.available_qualities.iter()@@ -300,209 +259,100 @@ impl PlatformDownloader for YouTubeDownloader {first};- let filename = format!(- "{}_{}.mp4",- sanitize_filename::sanitize(&info.author),- sanitize_filename::sanitize(&selected.label)- );- let output_path = opts.output_dir.join(&filename);-- tokio::fs::create_dir_all(&opts.output_dir).await?;-- // yt-dlp download path- if selected.format == "ytdlp" {- return download_ytdlp(&video_id, &output_path, progress.clone()).await;- }-- let needs_mux =- selected.format == "mp4+mux" && ffmpeg::is_ffmpeg_available().await;-- let file_size = if needs_mux {- download_muxed(&video_id, &output_path, progress.clone()).await?- } else {- match download_combined(&video_id, &output_path, progress.clone()).await {- Ok(size) => size,- Err(e) => {- // If download fails and yt-dlp is available, try fallback- if YouTubeDownloader::is_ytdlp_available().await {- tracing::info!("rusty_ytdl download failed ({}), trying yt-dlp", e);- return download_ytdlp(&video_id, &output_path, progress).await;- }- return Err(e);- }- }- };-- Ok(DownloadResult {- file_path: output_path,- file_size_bytes: file_size,- duration_seconds: info.duration_seconds.unwrap_or(0.0),- })- }-}--async fn download_combined(- video_id: &str,- output: &PathBuf,- progress: mpsc::Sender<f64>,-) -> anyhow::Result<u64> {- let options = rusty_ytdl::VideoOptions {- quality: rusty_ytdl::VideoQuality::Highest,- filter: rusty_ytdl::VideoSearchOptions::VideoAudio,- ..Default::default()- };-- let video = rusty_ytdl::Video::new_with_options(video_id, options)- .map_err(|e| anyhow!("{}", e))?;-- let stream = video- .stream()- .await- .map_err(|e| anyhow!("Erro ao iniciar stream: {}", e))?;-- let content_length = stream.content_length() as u64;+ let quality_height = Self::extract_quality_height(&selected.label);+ let video_url = &selected.url;- let mut file =- std::fs::File::create(output).map_err(|e| anyhow!("Erro ao criar arquivo: {}", e))?;-- let mut downloaded: u64 = 0;+ tracing::info!(+ "YouTube download: url={}, quality={:?}",+ video_url,+ quality_height+ );- while let Some(chunk) = stream- .chunk()+ ytdlp::download_video(+ &ytdlp_path,+ video_url,+ &opts.output_dir,+ quality_height,+ progress,+ ).await- .map_err(|e| anyhow!("Erro no stream: {}", e))?- {- file.write_all(&chunk)- .map_err(|e| anyhow!("Erro ao escrever: {}", e))?;- downloaded += chunk.len() as u64;-- if content_length > 0 {- let percent = (downloaded as f64 / content_length as f64) * 100.0;- let _ = progress.send(percent.min(99.0)).await;- }}-- let _ = progress.send(100.0).await;- Ok(downloaded)}-async fn download_muxed(- video_id: &str,- output: &PathBuf,- progress: mpsc::Sender<f64>,-) -> anyhow::Result<u64> {- let temp_dir = tempfile::tempdir()?;- let video_path = temp_dir.path().join("video.mp4");- let audio_path = temp_dir.path().join("audio.m4a");-- // Download video stream- let video_opts = rusty_ytdl::VideoOptions {- quality: rusty_ytdl::VideoQuality::HighestVideo,- filter: rusty_ytdl::VideoSearchOptions::Video,- ..Default::default()- };-- let video = rusty_ytdl::Video::new_with_options(video_id, video_opts)- .map_err(|e| anyhow!("{}", e))?;-- let stream = video- .stream()- .await- .map_err(|e| anyhow!("Erro ao iniciar stream de vídeo: {}", e))?;-- let video_cl = stream.content_length() as u64;- let mut file = std::fs::File::create(&video_path)?;- let mut downloaded: u64 = 0;-- while let Some(chunk) = stream- .chunk()- .await- .map_err(|e| anyhow!("Erro no stream de vídeo: {}", e))?- {- file.write_all(&chunk)?;- downloaded += chunk.len() as u64;-- if video_cl > 0 {- let percent = (downloaded as f64 / video_cl as f64) * 45.0;- let _ = progress.send(percent).await;- }- }-- let _ = progress.send(45.0).await;-- // Download audio stream- let audio_opts = rusty_ytdl::VideoOptions {- quality: rusty_ytdl::VideoQuality::HighestAudio,- filter: rusty_ytdl::VideoSearchOptions::Audio,- ..Default::default()- };+impl YouTubeDownloader {+ async fn download_playlist(+ &self,+ info: &MediaInfo,+ opts: &DownloadOptions,+ progress: mpsc::Sender<f64>,+ ytdlp_path: &std::path::Path,+ ) -> anyhow::Result<DownloadResult> {+ let playlist_dir = opts.output_dir.join(sanitize_filename::sanitize(&info.title));+ tokio::fs::create_dir_all(&playlist_dir).await?;- let audio = rusty_ytdl::Video::new_with_options(video_id, audio_opts)- .map_err(|e| anyhow!("{}", e))?;+ let total = info.available_qualities.len();+ let mut total_bytes = 0u64;+ let mut last_path = playlist_dir.clone();- let stream = audio- .stream()- .await- .map_err(|e| anyhow!("Erro ao iniciar stream de áudio: {}", e))?;+ tracing::info!(+ "YouTube playlist download: {} vídeos em {:?}",+ total,+ playlist_dir+ );- let audio_cl = stream.content_length() as u64;- let mut file = std::fs::File::create(&audio_path)?;- let mut downloaded: u64 = 0;+ for (i, entry) in info.available_qualities.iter().enumerate() {+ tracing::info!(+ "YouTube playlist [{}/{}]: {}",+ i + 1,+ total,+ entry.url+ );++ let (video_tx, mut video_rx) = mpsc::channel::<f64>(16);+ let progress_tx = progress.clone();+ let video_idx = i;+ let video_total = total;+ let forwarder = tokio::spawn(async move {+ while let Some(pct) = video_rx.recv().await {+ let overall =+ (video_idx as f64 / video_total as f64) * 100.0+ + (pct / video_total as f64);+ let _ = progress_tx.send(overall).await;+ }+ });++ match ytdlp::download_video(+ ytdlp_path,+ &entry.url,+ &playlist_dir,+ None,+ video_tx,+ )+ .await+ {+ Ok(result) => {+ total_bytes += result.file_size_bytes;+ last_path = result.file_path;+ }+ Err(e) => {+ tracing::warn!(+ "YouTube playlist [{}/{}] falhou: {}",+ i + 1,+ total,+ e+ );+ }+ }- while let Some(chunk) = stream- .chunk()- .await- .map_err(|e| anyhow!("Erro no stream de áudio: {}", e))?- {- file.write_all(&chunk)?;- downloaded += chunk.len() as u64;-- if audio_cl > 0 {- let percent = 45.0 + (downloaded as f64 / audio_cl as f64) * 45.0;- let _ = progress.send(percent).await;+ let _ = forwarder.await;}- }-- let _ = progress.send(90.0).await;-- ffmpeg::mux_video_audio(&video_path, &audio_path, output).await?;-- let _ = progress.send(100.0).await;- let meta = tokio::fs::metadata(output).await?;- Ok(meta.len())-}--async fn download_ytdlp(- video_id: &str,- output: &PathBuf,- progress: mpsc::Sender<f64>,-) -> anyhow::Result<DownloadResult> {- let output_template = output.to_string_lossy().to_string();-- let result = tokio::process::Command::new("yt-dlp")- .args([- "-f", "bv*+ba/b",- "--merge-output-format", "mp4",- "--no-playlist",- "-o", &output_template,- &format!("https://www.youtube.com/watch?v={}", video_id),- ])- .output()- .await- .map_err(|e| anyhow!("Falha ao executar yt-dlp: {}", e))?;+ let _ = progress.send(100.0).await;- if !result.status.success() {- let stderr = String::from_utf8_lossy(&result.stderr);- return Err(anyhow!("yt-dlp falhou: {}", stderr.trim()));+ Ok(DownloadResult {+ file_path: last_path,+ file_size_bytes: total_bytes,+ duration_seconds: 0.0,+ })}-- let _ = progress.send(100.0).await;-- let meta = tokio::fs::metadata(output).await?;- Ok(DownloadResult {- file_path: output.clone(),- file_size_bytes: meta.len(),- duration_seconds: 0.0,- })}