2026-09-21 10:57:43 -04:00
|
|
|
use crate::models::{CookieSource, VideoMetadata};
|
|
|
|
|
use serde::Deserialize;
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
use std::collections::HashMap;
|
2026-09-21 10:57:43 -04:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct SubtitleFormat {
|
|
|
|
|
pub ext: Option<String>,
|
|
|
|
|
pub url: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-21 10:57:43 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct YtDlpJson {
|
|
|
|
|
pub title: String,
|
|
|
|
|
pub duration: Option<f64>,
|
|
|
|
|
pub fps: Option<f64>,
|
|
|
|
|
pub thumbnail: Option<String>,
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
/// Manually authored subtitles: language_code -> [formats]
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub subtitles: HashMap<String, Vec<SubtitleFormat>>,
|
|
|
|
|
/// Auto-generated subtitles: language_code -> [formats]
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub automatic_captions: HashMap<String, Vec<SubtitleFormat>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check if subtitles have a VTT format for English (en, en-US, en-GB, en-orig).
|
|
|
|
|
fn find_english_key(map: &HashMap<String, Vec<SubtitleFormat>>) -> Option<&str> {
|
|
|
|
|
// Prefer exact "en", then any en-* variant
|
|
|
|
|
for key in ["en", "en-US", "en-GB", "en-orig"] {
|
|
|
|
|
if map.contains_key(key) {
|
|
|
|
|
return Some(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Try any key starting with "en"
|
|
|
|
|
for key in map.keys() {
|
|
|
|
|
if key.starts_with("en") {
|
|
|
|
|
return Some(key.as_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Determine caption availability: (has_captions, is_auto)
|
|
|
|
|
pub fn detect_captions(meta: &YtDlpJson) -> (bool, bool) {
|
|
|
|
|
// Priority 1: manual English subtitles
|
|
|
|
|
if find_english_key(&meta.subtitles).is_some() {
|
|
|
|
|
return (true, false);
|
|
|
|
|
}
|
|
|
|
|
// Priority 2: auto-generated English captions
|
|
|
|
|
if find_english_key(&meta.automatic_captions).is_some() {
|
|
|
|
|
return (true, true);
|
|
|
|
|
}
|
|
|
|
|
(false, false)
|
2026-09-21 10:57:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn parse_ytdlp_json(json_str: &str) -> Result<YtDlpJson, String> {
|
|
|
|
|
serde_json::from_str::<YtDlpJson>(json_str)
|
|
|
|
|
.map_err(|e| format!("Failed to parse yt-dlp JSON: {e}"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn resolve(url: &str, cookie_source: &CookieSource) -> Result<VideoMetadata, String> {
|
|
|
|
|
let cookie_args = cookie_source.to_ytdlp_args();
|
|
|
|
|
|
|
|
|
|
let mut meta_cmd = Command::new("yt-dlp");
|
2026-09-21 12:03:26 -04:00
|
|
|
meta_cmd
|
|
|
|
|
.arg("--dump-json")
|
|
|
|
|
.arg("--no-download")
|
|
|
|
|
.arg("--remote-components")
|
|
|
|
|
.arg("ejs:github");
|
2026-09-21 10:57:43 -04:00
|
|
|
for arg in &cookie_args {
|
|
|
|
|
meta_cmd.arg(arg);
|
|
|
|
|
}
|
|
|
|
|
meta_cmd.arg(url);
|
|
|
|
|
|
|
|
|
|
let meta_output = meta_cmd
|
|
|
|
|
.output()
|
|
|
|
|
.map_err(|e| format!("Failed to run yt-dlp: {e}"))?;
|
|
|
|
|
|
|
|
|
|
if !meta_output.status.success() {
|
|
|
|
|
let stderr = String::from_utf8_lossy(&meta_output.stderr);
|
|
|
|
|
return Err(format!("yt-dlp metadata failed: {stderr}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let json_str = String::from_utf8_lossy(&meta_output.stdout);
|
|
|
|
|
let meta = parse_ytdlp_json(&json_str)?;
|
|
|
|
|
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
let (has_captions, captions_are_auto) = detect_captions(&meta);
|
|
|
|
|
|
2026-09-21 10:57:43 -04:00
|
|
|
let mut stream_cmd = Command::new("yt-dlp");
|
2026-09-21 12:03:26 -04:00
|
|
|
stream_cmd
|
|
|
|
|
.arg("-g")
|
|
|
|
|
.arg("--no-download")
|
|
|
|
|
.arg("--remote-components")
|
|
|
|
|
.arg("ejs:github");
|
2026-09-21 10:57:43 -04:00
|
|
|
for arg in &cookie_args {
|
|
|
|
|
stream_cmd.arg(arg);
|
|
|
|
|
}
|
|
|
|
|
stream_cmd.arg(url);
|
|
|
|
|
|
|
|
|
|
let stream_output = stream_cmd
|
|
|
|
|
.output()
|
|
|
|
|
.map_err(|e| format!("Failed to get stream URL: {e}"))?;
|
|
|
|
|
|
|
|
|
|
if !stream_output.status.success() {
|
|
|
|
|
let stderr = String::from_utf8_lossy(&stream_output.stderr);
|
|
|
|
|
return Err(format!("yt-dlp stream URL failed: {stderr}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let stream_url = String::from_utf8_lossy(&stream_output.stdout)
|
|
|
|
|
.lines()
|
|
|
|
|
.next()
|
|
|
|
|
.unwrap_or("")
|
|
|
|
|
.trim()
|
|
|
|
|
.to_string();
|
|
|
|
|
|
2026-09-21 10:59:10 -04:00
|
|
|
if stream_url.is_empty() {
|
|
|
|
|
return Err("yt-dlp returned an empty stream URL".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-21 10:57:43 -04:00
|
|
|
Ok(VideoMetadata {
|
|
|
|
|
url: url.to_string(),
|
|
|
|
|
title: meta.title,
|
|
|
|
|
duration: meta.duration.unwrap_or(0.0),
|
|
|
|
|
fps: meta.fps.unwrap_or(30.0),
|
|
|
|
|
thumbnail_url: meta.thumbnail,
|
|
|
|
|
stream_url,
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
has_captions,
|
|
|
|
|
captions_are_auto,
|
2026-09-21 10:57:43 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_ytdlp_json_valid() {
|
|
|
|
|
let json = r#"{"title": "Test Video", "duration": 125.5, "fps": 29.97, "thumbnail": "https://i.ytimg.com/vi/abc/hqdefault.jpg"}"#;
|
|
|
|
|
let result = parse_ytdlp_json(json).unwrap();
|
|
|
|
|
assert_eq!(result.title, "Test Video");
|
|
|
|
|
assert!((result.duration.unwrap() - 125.5).abs() < 0.001);
|
|
|
|
|
assert!((result.fps.unwrap() - 29.97).abs() < 0.001);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
result.thumbnail.unwrap(),
|
|
|
|
|
"https://i.ytimg.com/vi/abc/hqdefault.jpg"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_ytdlp_json_missing_optional_fields() {
|
|
|
|
|
let json = r#"{"title": "Minimal Video"}"#;
|
|
|
|
|
let result = parse_ytdlp_json(json).unwrap();
|
|
|
|
|
assert_eq!(result.title, "Minimal Video");
|
|
|
|
|
assert!(result.duration.is_none());
|
|
|
|
|
assert!(result.fps.is_none());
|
|
|
|
|
assert!(result.thumbnail.is_none());
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
assert!(result.subtitles.is_empty());
|
|
|
|
|
assert!(result.automatic_captions.is_empty());
|
2026-09-21 10:57:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_ytdlp_json_invalid() {
|
|
|
|
|
let result = parse_ytdlp_json("not json");
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
}
|
chore: stage all pending work — caption styling, media server, processing modal, docs, summaries
Includes:
- Extended caption styling (font, shadow, dimmed color, bg toggle)
- Media server, subtitle downloader, VTT parser, processing modal
- Waveform tiers, thumbnail/timeline improvements, transport controls
- Hybrid download model, dependency management, clip export enhancements
- 21 chat summaries, 2 implementation plans, 2 design specs
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-22 10:48:16 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_detect_captions_manual() {
|
|
|
|
|
let json = r#"{"title": "T", "subtitles": {"en": [{"ext": "vtt", "url": "http://x"}]}, "automatic_captions": {}}"#;
|
|
|
|
|
let meta = parse_ytdlp_json(json).unwrap();
|
|
|
|
|
let (has, is_auto) = detect_captions(&meta);
|
|
|
|
|
assert!(has);
|
|
|
|
|
assert!(!is_auto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_detect_captions_auto_only() {
|
|
|
|
|
let json = r#"{"title": "T", "subtitles": {}, "automatic_captions": {"en": [{"ext": "vtt", "url": "http://x"}]}}"#;
|
|
|
|
|
let meta = parse_ytdlp_json(json).unwrap();
|
|
|
|
|
let (has, is_auto) = detect_captions(&meta);
|
|
|
|
|
assert!(has);
|
|
|
|
|
assert!(is_auto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_detect_captions_none() {
|
|
|
|
|
let json = r#"{"title": "T"}"#;
|
|
|
|
|
let meta = parse_ytdlp_json(json).unwrap();
|
|
|
|
|
let (has, _) = detect_captions(&meta);
|
|
|
|
|
assert!(!has);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_detect_captions_en_variant() {
|
|
|
|
|
let json = r#"{"title": "T", "subtitles": {"en-US": [{"ext": "vtt"}]}}"#;
|
|
|
|
|
let meta = parse_ytdlp_json(json).unwrap();
|
|
|
|
|
let (has, is_auto) = detect_captions(&meta);
|
|
|
|
|
assert!(has);
|
|
|
|
|
assert!(!is_auto);
|
|
|
|
|
}
|
2026-09-21 10:57:43 -04:00
|
|
|
}
|