trying parse different url, but failed
This commit is contained in:
@@ -46,18 +46,22 @@ pub struct ProgressEvent {
|
||||
pub status: String, // "downloading", "processing", "finished", "error"
|
||||
}
|
||||
|
||||
pub async fn fetch_metadata(app: &AppHandle, url: &str) -> Result<MetadataResult> {
|
||||
pub async fn fetch_metadata(app: &AppHandle, url: &str, parse_mix_playlist: bool) -> Result<MetadataResult> {
|
||||
let ytdlp_path = ytdlp::get_ytdlp_path(app)?;
|
||||
|
||||
// Use std::process for simple output capture if it's short, but tokio is safer for async.
|
||||
let output = Command::new(ytdlp_path)
|
||||
.arg("--dump-single-json")
|
||||
.arg("--flat-playlist")
|
||||
.arg(url)
|
||||
// Stop errors from cluttering
|
||||
.stderr(Stdio::piped())
|
||||
.output()
|
||||
.await?;
|
||||
let mut cmd = Command::new(ytdlp_path);
|
||||
cmd.arg("--dump-single-json")
|
||||
.arg("--flat-playlist")
|
||||
.arg("--no-warnings");
|
||||
|
||||
if parse_mix_playlist {
|
||||
cmd.arg("--playlist-end").arg("20");
|
||||
}
|
||||
|
||||
cmd.arg(url);
|
||||
cmd.stderr(Stdio::piped());
|
||||
|
||||
let output = cmd.output().await?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
@@ -90,10 +94,18 @@ pub async fn fetch_metadata(app: &AppHandle, url: &str) -> Result<MetadataResult
|
||||
}
|
||||
|
||||
fn parse_video_metadata(json: &serde_json::Value) -> VideoMetadata {
|
||||
let id = json["id"].as_str().unwrap_or("").to_string();
|
||||
|
||||
// Thumbnail fallback logic
|
||||
let thumbnail = match json.get("thumbnail").and_then(|t| t.as_str()) {
|
||||
Some(t) if !t.is_empty() => t.to_string(),
|
||||
_ => format!("https://i.ytimg.com/vi/{}/mqdefault.jpg", id),
|
||||
};
|
||||
|
||||
VideoMetadata {
|
||||
id: json["id"].as_str().unwrap_or("").to_string(),
|
||||
id,
|
||||
title: json["title"].as_str().unwrap_or("Unknown Title").to_string(),
|
||||
thumbnail: json["thumbnail"].as_str().unwrap_or("").to_string(), // Note: thumbnails might be an array sometimes, usually string in flat-playlist
|
||||
thumbnail,
|
||||
duration: json["duration"].as_f64(),
|
||||
uploader: json["uploader"].as_str().map(|s| s.to_string()),
|
||||
}
|
||||
@@ -182,4 +194,4 @@ pub async fn download_video(
|
||||
}).ok();
|
||||
Err(anyhow!("Download process failed"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user