Skip to content

Commit

Permalink
allow for providing extra args to yt-dlp --get-url
Browse files Browse the repository at this point in the history
  • Loading branch information
madiele committed Aug 15, 2024
1 parent c891ac3 commit db82ae8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum ConfName {
ValidUrlDomains,
AudioCodec,
PeerTubeValidHosts,
YouutbeYtDlpExtraArgs,
}

struct EnvConf {}
Expand Down Expand Up @@ -106,6 +107,10 @@ impl Conf for EnvConf {
ConfName::YouutbeMaxResults => {
Ok(std::env::var("YOUTUBE_MAX_RESULTS").unwrap_or_else(|_| "300".to_string()))
}
ConfName::YouutbeYtDlpExtraArgs => {
Ok(std::env::var("YOUTUBE_YT_DLP_GET_URL_EXTRA_ARGS")
.unwrap_or_else(|_| "[]".to_string()))
}
}
}
}
Expand Down Expand Up @@ -165,4 +170,3 @@ impl Default for AudioCodec {
Self::MP3
}
}

31 changes: 25 additions & 6 deletions src/provider/youtube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,20 @@ fn get_youtube_hub() -> YouTube<hyper_rustls::HttpsConnector<hyper::client::Http
)]
async fn get_youtube_stream_url(url: &Url) -> eyre::Result<Url> {
debug!("getting stream_url for yt video: {}", url);
let output = tokio::process::Command::new("yt-dlp")
let extra_args: Vec<String> =
serde_json::from_str(conf().get(ConfName::YouutbeYtDlpExtraArgs)?.as_str()).map_err(|_| eyre!(r#"failed to parse YOUTUBE_YT_DLP_GET_URL_EXTRA_ARGS allowed syntax is ["arg1#", "arg2", "arg3", ...]"#))?;
let mut command = tokio::process::Command::new("yt-dlp");
command
.arg("-f")
.arg("bestaudio")
.arg("--get-url")
.arg(url.as_str())
.output()
.await;
.arg(url.as_str());

for arg in extra_args {
command.arg(arg);
}

let output = command.output().await;

match output {
Ok(x) => {
Expand Down Expand Up @@ -565,8 +572,20 @@ async fn find_yt_channel_url_with_c_id(url: &Url) -> eyre::Result<Url> {
.arg(url.to_string())
.output()
.await?;
let feed_url = std::str::from_utf8(&output.stdout)?.trim().to_string();
Ok(Url::parse(&feed_url)?)
let conversion = std::str::from_utf8(&output.stdout);
let feed_url = match conversion {
Ok(feed_url) => feed_url,
Err(e) => {
warn!(
"error while translating channel name using yt-dlp:\nerror: {}\nyt-dlp stdout: {}\nyt-dlp stderr: {}",
e.to_string(),
conversion.unwrap_or_default(),
std::str::from_utf8(&output.stderr).unwrap_or_default()
);
return Err(eyre::eyre!(e));
}
};
Ok(Url::parse(feed_url)?)
}

fn convert_atom_to_rss(feed: Feed, duration_map: HashMap<String, Option<usize>>) -> String {
Expand Down

0 comments on commit db82ae8

Please sign in to comment.