Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

feat: add omniget-cli binary crate

Hanakob9f39a4cb7df
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index 3d73398f..10524a5a 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -70,6 +70,56 @@ dependencies = [
"libc",
]
+[[package]]
+name = "anstream"
+version = "0.6.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
+dependencies = [
+ "anstyle",
+ "anstyle-parse",
+ "anstyle-query",
+ "anstyle-wincon",
+ "colorchoice",
+ "is_terminal_polyfill",
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle"
+version = "1.0.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
+
+[[package]]
+name = "anstyle-parse"
+version = "0.2.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
+dependencies = [
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle-query"
+version = "1.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
+dependencies = [
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "anstyle-wincon"
+version = "3.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
+dependencies = [
+ "anstyle",
+ "once_cell_polyfill",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "anyhow"
version = "1.0.102"
@@ -874,6 +924,46 @@ dependencies = [
"libloading 0.8.9",
]
+[[package]]
+name = "clap"
+version = "4.5.60"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2797f34da339ce31042b27d23607e051786132987f595b02ba4f6a6dffb7030a"
+dependencies = [
+ "clap_builder",
+ "clap_derive",
+]
+
+[[package]]
+name = "clap_builder"
+version = "4.5.60"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "24a241312cea5059b13574bb9b3861cabf758b879c15190b37b6d6fd63ab6876"
+dependencies = [
+ "anstream",
+ "anstyle",
+ "clap_lex",
+ "strsim 0.11.1",
+]
+
+[[package]]
+name = "clap_derive"
+version = "4.5.55"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5"
+dependencies = [
+ "heck 0.5.0",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "clap_lex"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
+
[[package]]
name = "clipboard-win"
version = "5.4.1"
@@ -883,6 +973,12 @@ dependencies = [
"error-code",
]
+[[package]]
+name = "colorchoice"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
+
[[package]]
name = "combine"
version = "4.6.7"
@@ -938,6 +1034,19 @@ dependencies = [
"crossbeam-utils",
]
+[[package]]
+name = "console"
+version = "0.15.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8"
+dependencies = [
+ "encode_unicode",
+ "libc",
+ "once_cell",
+ "unicode-width",
+ "windows-sys 0.59.0",
+]
+
[[package]]
name = "const-random"
version = "0.1.18"
@@ -1544,6 +1653,12 @@ version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7"
+[[package]]
+name = "encode_unicode"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
+
[[package]]
name = "encoding_rs"
version = "0.8.35"
@@ -2796,6 +2911,19 @@ dependencies = [
"serde_core",
]
+[[package]]
+name = "indicatif"
+version = "0.17.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "183b3088984b400f4cfac3620d5e076c84da5364016b4f49473de574b2586235"
+dependencies = [
+ "console",
+ "number_prefix",
+ "portable-atomic",
+ "unicode-width",
+ "web-time",
+]
+
[[package]]
name = "infer"
version = "0.19.0"
@@ -2879,6 +3007,12 @@ dependencies = [
"once_cell",
]
+[[package]]
+name = "is_terminal_polyfill"
+version = "1.70.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
+
[[package]]
name = "itertools"
version = "0.14.0"
@@ -3450,6 +3584,15 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "matchers"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
+dependencies = [
+ "regex-automata",
+]
+
[[package]]
name = "matches"
version = "0.1.10"
@@ -3822,6 +3965,12 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "number_prefix"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
+
[[package]]
name = "objc"
version = "0.2.7"
@@ -4125,6 +4274,23 @@ dependencies = [
"zip 2.4.2",
]
+[[package]]
+name = "omniget-cli"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "clap",
+ "directories",
+ "indicatif",
+ "omniget-core",
+ "serde",
+ "serde_json",
+ "tokio",
+ "tokio-util",
+ "tracing",
+ "tracing-subscriber",
+]
+
[[package]]
name = "omniget-core"
version = "0.2.16"
@@ -4176,6 +4342,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
+[[package]]
+name = "once_cell_polyfill"
+version = "1.70.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
+
[[package]]
name = "open"
version = "5.3.3"
@@ -7032,10 +7204,14 @@ version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e"
dependencies = [
+ "matchers",
"nu-ansi-term",
+ "once_cell",
+ "regex-automata",
"sharded-slab",
"smallvec",
"thread_local",
+ "tracing",
"tracing-core",
"tracing-log",
]
@@ -7176,6 +7352,12 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
+[[package]]
+name = "unicode-width"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
+
[[package]]
name = "unicode-xid"
version = "0.2.6"
@@ -7237,6 +7419,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
+[[package]]
+name = "utf8parse"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
+
[[package]]
name = "uuid"
version = "0.8.2"
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 17213d69..a7ba290c 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
-members = [".", "omniget-core", "omniget-plugin-sdk"]
+members = [".", "omniget-core", "omniget-plugin-sdk", "omniget-cli"]
resolver = "2"
[package]
diff --git a/src-tauri/omniget-cli/Cargo.toml b/src-tauri/omniget-cli/Cargo.toml
index e69de29b..eafcaa60 100644
--- a/src-tauri/omniget-cli/Cargo.toml
+++ b/src-tauri/omniget-cli/Cargo.toml
@@ -0,0 +1,23 @@
+[package]
+name = "omniget-cli"
+version = "0.1.0"
+edition = "2021"
+description = "CLI for Omniget media downloader"
+
+[dependencies]
+omniget-core = { path = "../omniget-core" }
+tokio = { version = "1", features = ["full"] }
+tokio-util = { version = "0.7", features = ["rt"] }
+clap = { version = "4", features = ["derive"] }
+indicatif = "0.17"
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
+anyhow = "1"
+tracing = "0.1"
+tracing-subscriber = { version = "0.3", features = ["env-filter"] }
+directories = "6"
+
+[profile.release]
+lto = "thin"
+codegen-units = 1
+strip = true
diff --git a/src-tauri/omniget-cli/src/commands/batch.rs b/src-tauri/omniget-cli/src/commands/batch.rs
new file mode 100644
index 00000000..d1df0d36
--- /dev/null
+++ b/src-tauri/omniget-cli/src/commands/batch.rs
@@ -0,0 +1,70 @@
+use anyhow::{Context, Result};
+use std::sync::Arc;
+use tokio::sync::Semaphore;
+
+use crate::output;
+
+pub async fn execute(file: String, max_concurrent: usize, output_dir: Option<String>) -> Result<()> {
+ let content = tokio::fs::read_to_string(&file)
+ .await
+ .with_context(|| format!("Failed to read {}", file))?;
+
+ let urls: Vec<String> = content
+ .lines()
+ .map(|s| s.trim().to_string())
+ .filter(|s| !s.is_empty() && !s.starts_with('#'))
+ .collect();
+
+ if urls.is_empty() {
+ println!("No URLs found in {}", file);
+ return Ok(());
+ }
+
+ let total = urls.len();
+ if !output::is_json_mode() {
+ println!("Batch downloading {} URLs (max concurrent: {})", total, max_concurrent);
+ }
+
+ let semaphore = Arc::new(Semaphore::new(max_concurrent));
+ let mut handles = Vec::new();
+
+ for url in &urls {
+ let permit = semaphore.clone().acquire_owned().await?;
+ let dir = output_dir.clone();
+ let url = url.clone();
+
+ handles.push(tokio::spawn(async move {
+ let _permit = permit;
+ crate::commands::download::execute(url, None, dir, false, None, None).await
+ }));
+ }
+
+ let mut success = 0;
+ let mut failed = 0;
+
+ for handle in handles {
+ match handle.await {
+ Ok(Ok(())) => success += 1,
+ Ok(Err(e)) => {
+ eprintln!("Download error: {}", e);
+ failed += 1;
+ }
+ Err(e) => {
+ eprintln!("Task panicked: {}", e);
+ failed += 1;
+ }
+ }
+ }
+
+ if output::is_json_mode() {
+ output::print_json(&serde_json::json!({
+ "total": total,
+ "success": success,
+ "failed": failed,
+ }));
+ } else {
+ println!("Batch complete: {} succeeded, {} failed", success, failed);
+ }
+
+ Ok(())
+}
diff --git a/src-tauri/omniget-cli/src/commands/download.rs b/src-tauri/omniget-cli/src/commands/download.rs
new file mode 100644
index 00000000..42e2d14c
--- /dev/null
+++ b/src-tauri/omniget-cli/src/commands/download.rs
@@ -0,0 +1,102 @@
+use anyhow::Result;
+use std::path::PathBuf;
+use std::sync::Arc;
+use tokio::sync::mpsc;
+use tokio_util::sync::CancellationToken;
+
+use omniget_core::core::ytdlp;
+use omniget_core::models::progress::ProgressUpdate;
+
+use crate::output;
+use crate::reporter::{self, CliReporter};
+
+pub async fn execute(
+ url: String,
+ quality: Option<u32>,
+ output_dir: Option<String>,
+ audio_only: bool,
+ _subs: Option<String>,
+ format: Option<String>,
+) -> Result<()> {
+ let reporter = Arc::new(CliReporter::new(output::is_json_mode()));
+
+ // 1. Find yt-dlp binary
+ reporter.message("Searching for yt-dlp...");
+ let ytdlp = reporter::find_yt_dlp().await?;
+
+ // 2. Determine output directory
+ let output_path = match output_dir {
+ Some(dir) => PathBuf::from(dir),
+ None => reporter::default_output_dir(),
+ };
+ tokio::fs::create_dir_all(&output_path).await.ok();
+
+ // 3. Set up progress channel
+ let (tx, mut rx) = mpsc::channel::<ProgressUpdate>(100);
+ let reporter_arc = reporter.clone();
+ let progress_handle = tokio::spawn(async move {
+ while let Some(update) = rx.recv().await {
+ reporter_arc.update(&update);
+ }
+ });
+
+ // 4. Determine download mode
+ let download_mode = if audio_only { Some("audio") } else { None };
+
+ // 5. Set up cancellation
+ let cancel_token = CancellationToken::new();
+
+ // 6. Execute download
+ reporter.message(&format!("Starting download from: {}", url));
+ reporter.message(&format!("Output: {}", output_path.display()));
+
+ let result = ytdlp::download_video(
+ &ytdlp,
+ &url,
+ &output_path,
+ quality,
+ tx.clone(),
+ download_mode,
+ format.as_deref(),
+ None, // filename_template
+ None, // referer
+ cancel_token,
+ reporter::default_cookie_path().as_deref(),
+ 4, // concurrent_fragments
+ false, // download_subtitles
+ &[], // extra_flags
+ None, // audio_format
+ )
+ .await;
+
+ drop(tx);
+ progress_handle.await.ok();
+
+ match result {
+ Ok(dl_result) => {
+ reporter.finish(
+ true,
+ &format!("Downloaded to: {}", dl_result.file_path.display()),
+ );
+ if output::is_json_mode() {
+ output::print_json(&serde_json::json!({
+ "success": true,
+ "file_path": dl_result.file_path,
+ "size": dl_result.file_size_bytes,
+ "duration": dl_result.duration_seconds,
+ }));
+ }
+ Ok(())
+ }
+ Err(e) => {
+ reporter.finish(false, &format!("Download failed: {}", e));
+ if output::is_json_mode() {
+ output::print_json(&serde_json::json!({
+ "success": false,
+ "error": format!("{}", e),
+ }));
+ }
+ Err(e)
+ }
+ }
+}
diff --git a/src-tauri/omniget-cli/src/commands/info.rs b/src-tauri/omniget-cli/src/commands/info.rs
new file mode 100644
index 00000000..8deb8562
--- /dev/null
+++ b/src-tauri/omniget-cli/src/commands/info.rs
@@ -0,0 +1,56 @@
+use anyhow::Result;
+
+use omniget_core::core::ytdlp;
+
+use crate::output;
+use crate::reporter::find_yt_dlp;
+
+pub async fn execute(url: String) -> Result<()> {
+ let ytdlp = find_yt_dlp().await?;
+
+ let info = ytdlp::get_video_info(&ytdlp, &url, &[]).await?;
+ let formats = ytdlp::parse_formats(&info);
+
+ if output::is_json_mode() {
+ output::print_json(&serde_json::json!({
+ "url": url,
+ "title": info.get("title").and_then(|v| v.as_str()).unwrap_or(""),
+ "duration": info.get("duration").and_then(|v| v.as_f64()).unwrap_or(0.0),
+ "uploader": info.get("uploader").and_then(|v| v.as_str()).unwrap_or(""),
+ "view_count": info.get("view_count").and_then(|v| v.as_u64()),
+ "thumbnail": info.get("thumbnail").and_then(|v| v.as_str()).unwrap_or(""),
+ "formats": formats,
+ }));
+ } else {
+ println!(
+ "Title: {}",
+ info.get("title").and_then(|v| v.as_str()).unwrap_or("")
+ );
+ println!(
+ "Duration: {:.0}s",
+ info.get("duration").and_then(|v| v.as_f64()).unwrap_or(0.0)
+ );
+ println!(
+ "Uploader: {}",
+ info.get("uploader").and_then(|v| v.as_str()).unwrap_or("")
+ );
+ println!("\nAvailable formats:");
+ for f in &formats {
+ let res = f.resolution.as_deref().unwrap_or("?");
+ let note = f.format_note.as_deref().unwrap_or("");
+ println!(
+ " {:<10} {:<8} {:>5} fps={:<4} {:<10} {}",
+ f.format_id,
+ f.ext,
+ res,
+ f.fps
+ .map(|v| format!("{:.0}", v))
+ .unwrap_or_else(|| "-".to_string()),
+ if f.has_audio { "audio" } else { "video-only" },
+ note,
+ );
+ }
+ }
+
+ Ok(())
+}
diff --git a/src-tauri/omniget-cli/src/commands/mod.rs b/src-tauri/omniget-cli/src/commands/mod.rs
new file mode 100644
index 00000000..ec30ea93
--- /dev/null
+++ b/src-tauri/omniget-cli/src/commands/mod.rs
@@ -0,0 +1,3 @@
+pub mod download;
+pub mod info;
+pub mod batch;
diff --git a/src-tauri/omniget-cli/src/main.rs b/src-tauri/omniget-cli/src/main.rs
index e69de29b..08688823 100644
--- a/src-tauri/omniget-cli/src/main.rs
+++ b/src-tauri/omniget-cli/src/main.rs
@@ -0,0 +1,93 @@
+use clap::Parser;
+
+mod commands;
+mod output;
+mod reporter;
+
+#[derive(Parser)]
+#[command(name = "omniget", version, about = "Download media from 1800+ sites", long_about = None)]
+struct Cli {
+ #[arg(long, global = true, help = "Output in JSON format")]
+ json: bool,
+
+ #[command(subcommand)]
+ command: Commands,
+}
+
+#[derive(clap::Subcommand)]
+enum Commands {
+ /// Download a video/audio from URL
+ Download {
+ url: String,
+
+ #[arg(short, long, help = "Video quality height (e.g. 720, 1080)")]
+ quality: Option<u32>,
+
+ #[arg(short, long, help = "Output directory")]
+ output: Option<String>,
+
+ #[arg(long, help = "Download audio only")]
+ audio_only: bool,
+
+ #[arg(long, help = "Subtitle languages (e.g. en,zh-Hans)")]
+ subs: Option<String>,
+
+ #[arg(long, help = "Format preference (mp4/mkv/webm)")]
+ format: Option<String>,
+ },
+ /// Preview media info without downloading
+ Info {
+ url: String,
+ },
+ /// Batch download from a file (one URL per line)
+ Batch {
+ file: String,
+
+ #[arg(short, long, default_value = "3")]
+ max_concurrent: usize,
+
+ #[arg(short, long)]
+ output: Option<String>,
+ },
+}
+
+#[tokio::main]
+async fn main() -> anyhow::Result<()> {
+ tracing_subscriber::fmt()
+ .with_env_filter(
+ tracing_subscriber::EnvFilter::try_from_default_env()
+ .unwrap_or_else(|_| "omniget_cli=info".into()),
+ )
+ .init();
+
+ let cli = Cli::parse();
+
+ if cli.json {
+ output::set_json_mode(true);
+ }
+
+ match cli.command {
+ Commands::Download {
+ url,
+ quality,
+ output,
+ audio_only,
+ subs,
+ format,
+ } => {
+ commands::download::execute(url, quality, output, audio_only, subs, format).await?;
+ }
+ Commands::Info { url } => {
+ commands::info::execute(url).await?;
+ }
+ Commands::Batch {
+ file,
+ max_concurrent,
+ output,
+ } => {
+ commands::batch::execute(file, max_concurrent, output).await?;
+ }
+ }
+
+ Ok(())
+}
diff --git a/src-tauri/omniget-cli/src/output.rs b/src-tauri/omniget-cli/src/output.rs
new file mode 100644
index 00000000..b949242a
--- /dev/null
+++ b/src-tauri/omniget-cli/src/output.rs
@@ -0,0 +1,24 @@
+use std::sync::atomic::{AtomicBool, Ordering};
+
+static JSON_MODE: AtomicBool = AtomicBool::new(false);
+
+pub fn set_json_mode(enabled: bool) {
+ JSON_MODE.store(enabled, Ordering::SeqCst);
+}
+
+pub fn is_json_mode() -> bool {
+ JSON_MODE.load(Ordering::SeqCst)
+}
+
+pub fn print_json<T: serde::Serialize>(value: &T) {
+ match serde_json::to_string_pretty(value) {
+ Ok(json) => println!("{}", json),
+ Err(e) => eprintln!("JSON serialization error: {}", e),
+ }
+}
+
+pub fn print_text(text: &str) {
+ if !is_json_mode() {
+ println!("{}", text);
+ }
+}
diff --git a/src-tauri/omniget-cli/src/reporter.rs b/src-tauri/omniget-cli/src/reporter.rs
new file mode 100644
index 00000000..7341dbfe
--- /dev/null
+++ b/src-tauri/omniget-cli/src/reporter.rs
@@ -0,0 +1,93 @@
+use anyhow::Result;
+use indicatif::{ProgressBar, ProgressStyle};
+use std::path::PathBuf;
+
+use omniget_core::core::dependencies::find_tool;
+use omniget_core::core::paths::app_data_dir;
+use omniget_core::models::progress::ProgressUpdate;
+
+const SPINNER_STYLE: &str = "{spinner:.cyan} {msg:.dim}";
+const PROGRESS_STYLE: &str = "{spinner:.cyan} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, ETA {eta})";
+
+pub struct CliReporter {
+ pb: ProgressBar,
+ json_mode: bool,
+}
+
+impl CliReporter {
+ pub fn new(json_mode: bool) -> Self {
+ let pb = ProgressBar::new(100);
+ pb.set_style(ProgressStyle::default_bar().template(SPINNER_STYLE).unwrap());
+ Self { pb, json_mode }
+ }
+
+ pub fn message(&self, msg: &str) {
+ if self.json_mode {
+ println!(r#"{{"type":"message","content":"{}"}}"#, msg.replace('"', "\\\""));
+ } else {
+ self.pb.set_message(msg.to_string());
+ }
+ }
+
+ pub fn update(&self, update: &ProgressUpdate) {
+ if self.json_mode {
+ let json = serde_json::json!({
+ "type": "progress",
+ "percent": update.percent,
+ "downloaded_bytes": update.downloaded_bytes,
+ "total_bytes": update.total_bytes,
+ "speed_bps": update.speed_bps,
+ "eta_seconds": update.eta_seconds,
+ });
+ println!("{}", json);
+ } else {
+ if update.percent >= 0.0 {
+ self.pb.set_position((update.percent * 100.0) as u64);
+ self.pb.set_style(
+ ProgressStyle::default_bar()
+ .template(PROGRESS_STYLE)
+ .unwrap(),
+ );
+ }
+ }
+ }
+
+ pub fn finish(&self, success: bool, message: &str) {
+ if self.json_mode {
+ let json = serde_json::json!({
+ "type": "complete",
+ "success": success,
+ "message": message,
+ });
+ println!("{}", json);
+ } else {
+ if success {
+ self.pb.finish_with_message(format!("✓ {}", message));
+ } else {
+ self.pb.finish_with_message(format!("✗ {}", message));
+ }
+ }
+ }
+}
+
+pub async fn find_yt_dlp() -> Result<PathBuf> {
+ find_tool("yt-dlp")
+ .await
+ .ok_or_else(|| anyhow::anyhow!("yt-dlp not found in PATH or app data dir"))
+}
+
+pub fn default_output_dir() -> PathBuf {
+ directories::UserDirs::new()
+ .and_then(|d| d.download_dir().map(PathBuf::from))
+ .unwrap_or_else(|| std::env::current_dir().unwrap_or_default())
+}
+
+pub fn default_cookie_path() -> Option<PathBuf> {
+ let app_dir = app_data_dir()?;
+ let cookie_file = app_dir.join("cookies").join("cookies.txt");
+ if cookie_file.exists() {
+ Some(cookie_file)
+ } else {
+ None
+ }
+}