summaryrefslogtreecommitdiffstats
path: root/src/youtube_dl.rs
blob: d588760d7a646ba4852e4b85585ae522ef7c92d1 (plain) (blame)
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
use std::process::{Command, Stdio};
use tokio_process::CommandExt;
use futures::compat::Future01CompatExt;

use log::debug;

pub async fn get_audio_download_url(uri: String) -> Result<(String, String), String> {
    let ytdl_args = [
        "--no-playlist",
        "-f",
        "bestaudio/best",
        "-g",
        "--get-filename",
        "-o",
        "%(title)s",
        &uri,
    ];

    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_async().compat().await.unwrap();

    let output = String::from_utf8(ytdl_output.stdout.clone()).unwrap();

    if ytdl_output.status.success() == false {
        return Err(String::from_utf8(ytdl_output.stderr.clone()).unwrap());
    }

    let lines = output.lines().collect::<Vec<_>>();
    let url = lines[0].to_owned();
    let title = lines[1].to_owned();

    Ok((url, title))
}