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
use crate::models::progress::ProgressUpdate;
use crate::platforms::traits::PlatformDownloader;
use std::collections::HashSet;
use anyhow::anyhow;
use async_trait::async_trait;
use tokio::sync::mpsc;
use crate::core::ytdlp;
use crate::models::media::{
DownloadOptions, DownloadResult, MediaInfo, MediaType, VideoQuality as MediaVideoQuality,
};
pub struct VimeoDownloader;
impl Default for VimeoDownloader {
fn default() -> Self {
Self::new()
}
}
impl VimeoDownloader {
pub fn new() -> Self {
Self
}
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()
}
fn parse_video_info(json: &serde_json::Value) -> anyhow::Result<MediaInfo> {
let title = json
.get("title")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
.to_string();
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")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let webpage_url = json
.get("webpage_url")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
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");
if vcodec == "none" || height == 0 {
continue;
}
if seen_heights.insert(height) {
qualities.push(MediaVideoQuality {
label: format!("{}p", height),
width,
height,
url: webpage_url.clone(),
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: webpage_url,
format: "ytdlp".to_string(),
});
}
Ok(MediaInfo {
title,
author,
platform: "vimeo".to_string(),
duration_seconds: duration,
thumbnail_url: thumbnail,
available_qualities: qualities,
media_type: MediaType::Video,
file_size_bytes: None,
})
}
}
#[async_trait]
impl PlatformDownloader for VimeoDownloader {
fn name(&self) -> &str {
"vimeo"
}
fn can_handle(&self, url: &str) -> bool {
if let Ok(parsed) = url::Url::parse(url) {
if let Some(host) = parsed.host_str() {
let host = host.to_lowercase();
return host == "vimeo.com" || host.ends_with(".vimeo.com");
}
}
false
}
async fn get_media_info(&self, url: &str) -> anyhow::Result<MediaInfo> {
let ytdlp_path = ytdlp::ensure_ytdlp()
.await
.map_err(|e| anyhow!("Vimeo requires yt-dlp. Failed to get yt-dlp: {}", e))?;
let json = ytdlp::get_video_info(&ytdlp_path, url, &[]).await?;
Self::parse_video_info(&json)
}
async fn download(
&self,
info: &MediaInfo,
opts: &DownloadOptions,
progress: mpsc::Sender<ProgressUpdate>,
) -> anyhow::Result<DownloadResult> {
let _ = progress.send(ProgressUpdate::percent(0.0)).await;
let ytdlp_path = ytdlp::ensure_ytdlp().await?;
let first = info
.available_qualities
.first()
.ok_or_else(|| anyhow!("No quality available"))?;
let requested_height = opts
.quality
.as_deref()
.and_then(Self::extract_quality_height);
let selected = match requested_height {
Some(h) => info
.available_qualities
.iter()
.filter(|q| q.height > 0 && q.height <= h)
.max_by_key(|q| q.height)
.unwrap_or(first),
None => {
if let Some(ref wanted) = opts.quality {
info.available_qualities
.iter()
.find(|q| q.label == *wanted)
.unwrap_or(first)
} else {
first
}
}
};
let quality_height =
requested_height.or_else(|| Self::extract_quality_height(&selected.label));
let video_url = &selected.url;
ytdlp::download_video(
&ytdlp_path,
video_url,
&opts.output_dir,
quality_height,
progress,
opts.download_mode.as_deref(),
opts.format_id.as_deref(),
opts.filename_template.as_deref(),
opts.referer.as_deref(),
opts.cancel_token.clone(),
None,
opts.concurrent_fragments,
false,
&[],
opts.audio_format.as_deref(),
)
.await
}
}
#[cfg(test)]
mod tests {
use super::VimeoDownloader;
use crate::models::media::VideoQuality;
#[test]
fn extract_quality_height_parses_4k_and_2k_and_best() {
assert_eq!(VimeoDownloader::extract_quality_height("2160p"), Some(2160));
assert_eq!(VimeoDownloader::extract_quality_height("1440p"), Some(1440));
assert_eq!(VimeoDownloader::extract_quality_height("1080p"), Some(1080));
assert_eq!(VimeoDownloader::extract_quality_height("best"), None);
}
#[test]
fn upper_bound_selection_falls_back_to_max_available_when_video_is_1080p() {
let qualities = [
VideoQuality {
label: "1080p".to_string(),
width: 1920,
height: 1080,
url: "https://vimeo.com/1".to_string(),
format: "ytdlp".to_string(),
},
VideoQuality {
label: "720p".to_string(),
width: 1280,
height: 720,
url: "https://vimeo.com/1".to_string(),
format: "ytdlp".to_string(),
},
];
let requested_height = VimeoDownloader::extract_quality_height("2160p").unwrap();
let selected = qualities
.iter()
.filter(|q| q.height > 0 && q.height <= requested_height)
.max_by_key(|q| q.height)
.unwrap();
assert_eq!(selected.height, 1080);
}
}