Browse Source

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 years ago
parent
commit
dc4fa59f38
2 changed files with 10 additions and 12 deletions
  1. 2 3
      src/audio/song.rs
  2. 8 9
      src/audio/subprocess.rs

+ 2 - 3
src/audio/song.rs

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

+ 8 - 9
src/audio/subprocess.rs

@@ -6,24 +6,23 @@ use std::{
 use symphonia_core::io::ReadOnlySource;
 use symphonia_core::io::ReadOnlySource;
 use tokio::process::Command as TokioCommand;
 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 mut cmd = TokioCommand::new("youtube-dl");
     let cmd = cmd
     let cmd = cmd
         .arg("-x")
         .arg("-x")
         .arg("--skip-download")
         .arg("--skip-download")
+        .arg("--get-title")
         .arg("--get-url")
         .arg("--get-url")
         .arg("--audio-quality")
         .arg("--audio-quality")
         .arg("128k")
         .arg("128k")
         .arg(query);
         .arg(query);
     let out = cmd.output().await?;
     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> {
 pub async fn ffmpeg_pcm(url: &str) -> Result<Box<dyn MediaSource + Send>, String> {