aboutsummaryrefslogtreecommitdiff
path: root/src/audio/ytdl.rs
blob: 7333c18a01dbf00e62051f578822494334784b69 (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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/// This module is entirely adapted from the relevant code in Serenity.

use std::{
    io::{
        Read,
        Result as IoResult,
    },
    process::{
        Child,
        Command,
        Stdio,
    },
};

use chrono::Duration;
use serde_json::Value;
use serenity::{
    voice::{
        AudioSource,
        pcm,
        VoiceError,
    }
};

use crate::Result;

struct ChildContainer(Child);

impl Read for ChildContainer {
    fn read(&mut self, buffer: &mut [u8]) -> IoResult<usize> {
        self.0.stdout.as_mut().unwrap().read(buffer)
    }
}

impl Drop for ChildContainer {
    fn drop(&mut self) {
        if let Err(e) = self.0.kill() {
            debug!("Error awaiting child process: {:?}", e);
        }
    }
}

pub(crate) trait CodecInfo {
    fn ffmpeg_opts() -> &'static[&'static str];
}

pub(crate) struct Pcm {}
pub(crate) struct Opus {}
pub(crate) struct Mp3 {}

impl CodecInfo for Pcm {
    #[inline]
    fn ffmpeg_opts() -> &'static[&'static str] {
        lazy_static! {
            static ref OPTS: Vec<&'static str> = vec! [
                "-f", "s16le",
                "-acodec", "pcm_s16le",
            ];
        }

        &*OPTS
    }
}

impl CodecInfo for Opus {
    #[inline]
    fn ffmpeg_opts() -> &'static[&'static str] {
        lazy_static! {
            static ref OPTS: Vec<&'static str> = vec! [
//                "-f", "s16le",
                "-acodec", "libopus",
                "-sample_fmt", "s16",
                "-vbr", "off",
//                "-b:a 96k",
//                "-vn",
            ];
        }

        &*OPTS
    }
}

impl CodecInfo for Mp3 {
    #[inline]
    fn ffmpeg_opts() -> &'static[&'static str] {
        lazy_static! {
            static ref OPTS: Vec<&'static str> = vec! [
                "-f", "aac",
                "-acodec", "libfdk_aac",
                "-b:a", "96k",
                "-sample_fmt", "s16",
            ];
        }

        &*OPTS
    }
}

pub fn ytdl_url(uri: &str) -> Result<String> {
    let args = [
        "-f",
        "webm[abr>0]/bestaudio/best",
        "--no-playlist",
        "--print-json",
        "--skip-download",
        uri,
    ];

    let out = Command::new("youtube-dl")
        .args(&args)
        .stdin(Stdio::null())
        .output()?;

    if !out.status.success() {
        return Err(VoiceError::YouTubeDLRun(out).into());
    }

    let value = serde_json::from_reader(&out.stdout[..])?;
    let mut obj = match value {
        Value::Object(obj) => obj,
        other => return Err(VoiceError::YouTubeDLProcessing(other).into()),
    };

    match obj.remove("url") {
        Some(v) => match v {
            Value::String(uri) => Ok(uri),
            other => Err(VoiceError::YouTubeDLUrl(other).into()),
        },
        None => Err(VoiceError::YouTubeDLUrl(Value::Object(obj)).into()),
    }
}

pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: Option<Duration>, size_limit: Option<usize>) -> Result<Child> {
    let start = start.unwrap_or(Duration::zero());
    let start_str = format!("{:02}:{:02}:{:02}", start.num_hours(), start.num_minutes() % 60, start.num_seconds() % 60);

    let mut opts = vec! [
        "-ac",
        "2", // force stereo -- this may cause issues
        "-ar",
        "48000",
        "-ss",
        &start_str,
    ]
        .into_iter()
        .map(|s| s.to_owned())
        .collect::<Vec<_>>();

    let codec_opts = T::ffmpeg_opts().into_iter().map(|&s| s.to_owned()).collect::<Vec<_>>();
    opts.extend(codec_opts);

    if let Some(limit) = size_limit {
        opts.push("-fs".to_owned());
        opts.push(format!("{}", limit));
    }

    if let Some(e) = end {
        opts.push("-to".to_owned());
        opts.push(format!("{:02}:{:02}:{:02}", e.num_hours(), e.num_minutes() % 60, e.num_seconds() % 60));
    }

    opts.push("-".to_owned());

    debug!("ffmpeg -i \"{}\" {}", uri, opts.join(" "));

    Command::new("ffmpeg")
        .arg("-i")
        .arg(uri)
        .args(opts)
        .stderr(Stdio::piped())
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .spawn()
        .map_err(|e| e.into())
}

pub fn ytdl(uri: &str, start: Option<Duration>, end: Option<Duration>) -> Result<Box<AudioSource>> {
    let youtube_uri = ytdl_url(uri)?;
    let command = ffmpeg_dl::<Pcm>(&youtube_uri, start, end, None)?;
    Ok(pcm(true, command.stdout.unwrap()))
}