This commit is contained in:
nvrl committed 2026-03-22 12:37:30 +01:00
1 parent 1c701d1e61
commit 5c5dffc144
2 files changed
+24 -17

No files matched your search

+1 -4
View File
@@ -9,8 +9,5 @@ pub enum YtDlpError {
#[error("failed to parse JSON: {0}")] #[error("failed to parse JSON: {0}")]
JsonError(#[from] serde_json::Error), JsonError(#[from] serde_json::Error),
#[error("yt-dlp exited with status {code:?}: {stderr}")] #[error("yt-dlp exited with status {code:?}: {stderr}")]
Failure { Failure { code: Option<i32>, stderr: String },
code: Option<i32>,
stderr: String,
},
} }
+23 -13
View File
@@ -328,8 +328,11 @@ impl YtDlp {
let mut clone = self.clone(); let mut clone = self.clone();
clone = clone.arg("--newline"); clone = clone.arg("--newline");
let mut child = clone.spawn_yt_dlp(false).await?; let mut child = clone.spawn_yt_dlp(false).await?;
let stdout = child.stdout.take().map(|stdout| BufReader::new(stdout).lines()); let stdout = child
.stdout
.take()
.map(|stdout| BufReader::new(stdout).lines());
Ok(YtDlpChild { child, stdout }) Ok(YtDlpChild { child, stdout })
} }
@@ -337,11 +340,14 @@ impl YtDlp {
/// Executes yt-dlp and streams the raw media binary data to standard output. /// Executes yt-dlp and streams the raw media binary data to standard output.
/// This automatically sets `--output -` and gives you access to the async stdout reader. /// This automatically sets `--output -` and gives you access to the async stdout reader.
pub async fn download_to_stream(&self) -> Result<YtDlpStream> { pub async fn download_to_stream(&self) -> Result<YtDlpStream> {
info!("Starting binary stream download for links: {:?}", self.links); info!(
"Starting binary stream download for links: {:?}",
self.links
);
let mut clone = self.clone(); let mut clone = self.clone();
clone = clone.arg_with("--output", "-"); clone = clone.arg_with("--output", "-");
let mut child = clone.spawn_yt_dlp(false).await?; let mut child = clone.spawn_yt_dlp(false).await?;
let stdout = child.stdout.take().ok_or_else(|| { let stdout = child.stdout.take().ok_or_else(|| {
error!("Failed to capture stdout for binary stream"); error!("Failed to capture stdout for binary stream");
YtDlpError::Failure { YtDlpError::Failure {
@@ -426,9 +432,11 @@ mod tests {
async fn test_version() -> Result<()> { async fn test_version() -> Result<()> {
let ytd = YtDlp::new("").arg("--version"); let ytd = YtDlp::new("").arg("--version");
let result = ytd.download().await?; let result = ytd.download().await?;
assert!(regex::Regex::new(r"\d{4}\.\d{2}\.\d{2}") assert!(
.unwrap() regex::Regex::new(r"\d{4}\.\d{2}\.\d{2}")
.is_match(result.output())); .unwrap()
.is_match(result.output())
);
Ok(()) Ok(())
} }
@@ -443,18 +451,20 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_download_process() -> Result<()> { async fn test_download_process() -> Result<()> {
let mut process = YtDlp::new("").arg("--version").download_process().await?; let mut process = YtDlp::new("").arg("--version").download_process().await?;
let mut lines = Vec::new(); let mut lines = Vec::new();
while let Some(line) = process.next_line().await? { while let Some(line) = process.next_line().await? {
lines.push(line); lines.push(line);
} }
process.wait().await?; process.wait().await?;
assert!(!lines.is_empty()); assert!(!lines.is_empty());
assert!(regex::Regex::new(r"\d{4}\.\d{2}\.\d{2}") assert!(
.unwrap() regex::Regex::new(r"\d{4}\.\d{2}\.\d{2}")
.is_match(&lines[0])); .unwrap()
.is_match(&lines[0])
);
Ok(()) Ok(())
} }
} }