1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
use std::time::Duration;
use std::process::Stdio;
use tokio::process::Command;
use serde::{Deserialize, Serialize};
use log::debug;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AudioMetadata {
pub url: String,
pub webpage_url: String,
pub title: String,
pub thumbnail: Option<String>,
#[serde(default, deserialize_with = "duration_deserialize")]
pub duration: Option<Duration>,
#[serde(skip)]
pub added_by: String,
}
fn duration_deserialize<'de, D>(deserializer: D) -> Result<Option<Duration>, D::Error>
where
D: serde::Deserializer<'de>,
{
let dur: Option<f64> = Deserialize::deserialize(deserializer)?;
Ok(dur.map(Duration::from_secs_f64))
}
pub async fn get_audio_download_from_url(uri: String) -> Result<AudioMetadata, String> {
//youtube-dl sometimes just fails, so we give it a second try
let ytdl_output = match run_youtube_dl(&uri).await {
Ok(o) => o,
Err(e) => {
if e.contains("Unable to extract video data") {
run_youtube_dl(&uri).await?
} else {
return Err(e);
}
}
};
let output = serde_json::from_str(&ytdl_output).map_err(|e| e.to_string())?;
Ok(output)
}
async fn run_youtube_dl(url: &str) -> Result<String, String> {
let ytdl_args = ["--no-playlist", "-f", "bestaudio/best", "-j", &url];
let mut cmd = Command::new("youtube-dl");
cmd.args(&ytdl_args);
cmd.stdin(Stdio::null());
debug!("yt-dl command: {:?}", cmd);
let ytdl_output = cmd.output().await.unwrap();
if !ytdl_output.status.success() {
let s = String::from_utf8(ytdl_output.stderr).unwrap();
return Err(s);
}
let output_str = String::from_utf8(ytdl_output.stdout).unwrap();
Ok(output_str)
}
|