Эх сурвалжийг харах

Use a single youtube-dl call to get title and URL

Youtube search requests are very slow. Speed things up by getting both
video title and streaming URL in a single call.
Frans Bergman 3 жил өмнө
parent
commit
dc4fa59f38

+ 2 - 3
src/audio/song.rs

@@ -1,4 +1,4 @@
-use super::subprocess::{ytdl, ytdl_title};
+use super::subprocess::ytdl;
 
 pub struct SongMetadata {
     pub artist: Option<String>,
@@ -19,8 +19,7 @@ impl Song {
             format!("ytsearch:{}", query)
         };
 
-        let title = ytdl_title(&query).await?;
-        let url = ytdl(&query).await?;
+        let (title, url) = ytdl(&query).await?;
 
         let metadata = SongMetadata {
             artist: None,

+ 8 - 9
src/audio/subprocess.rs

@@ -6,24 +6,23 @@ use std::{
 use symphonia_core::io::ReadOnlySource;
 use tokio::process::Command as TokioCommand;
 
-pub async fn ytdl(query: &str) -> Result<String, std::io::Error> {
+pub async fn ytdl(query: &str) -> Result<(String, String), std::io::Error> {
     let mut cmd = TokioCommand::new("youtube-dl");
     let cmd = cmd
         .arg("-x")
         .arg("--skip-download")
+        .arg("--get-title")
         .arg("--get-url")
         .arg("--audio-quality")
         .arg("128k")
         .arg(query);
     let out = cmd.output().await?;
-    Ok(String::from_utf8(out.stdout).unwrap())
-}
-
-pub async fn ytdl_title(query: &str) -> Result<String, std::io::Error> {
-    let mut cmd = TokioCommand::new("youtube-dl");
-    let cmd = cmd.arg("--get-title").arg(query);
-    let out = cmd.output().await?;
-    Ok(String::from_utf8(out.stdout).unwrap())
+    let out = String::from_utf8(out.stdout).unwrap();
+    let mut lines = out.lines();
+    Ok((
+        lines.next().unwrap().to_string(),
+        lines.next().unwrap().to_string(),
+    ))
 }
 
 pub async fn ffmpeg_pcm(url: &str) -> Result<Box<dyn MediaSource + Send>, String> {