diff options
Diffstat (limited to 'src/audio/ytdl.rs')
| -rw-r--r-- | src/audio/ytdl.rs | 47 |
1 files changed, 15 insertions, 32 deletions
diff --git a/src/audio/ytdl.rs b/src/audio/ytdl.rs index 7333c18..8239683 100644 --- a/src/audio/ytdl.rs +++ b/src/audio/ytdl.rs @@ -45,8 +45,9 @@ pub(crate) trait CodecInfo { } pub(crate) struct Pcm {} + +#[allow(dead_code)] pub(crate) struct Opus {} -pub(crate) struct Mp3 {} impl CodecInfo for Pcm { #[inline] @@ -67,28 +68,9 @@ impl CodecInfo for Opus { fn ffmpeg_opts() -> &'static[&'static str] { lazy_static! { static ref OPTS: Vec<&'static str> = vec! [ -// "-f", "s16le", + "-f", "opus", "-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", + "-b:a 96k", ]; } @@ -130,7 +112,7 @@ pub fn ytdl_url(uri: &str) -> Result<String> { } } -pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: Option<Duration>, size_limit: Option<usize>) -> Result<Child> { +pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: Option<Duration>, size_limit: Option<usize>) -> Result<Box<dyn Read + Send>> { 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); @@ -146,6 +128,11 @@ pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: O .map(|s| s.to_owned()) .collect::<Vec<_>>(); + 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)); + } + let codec_opts = T::ffmpeg_opts().into_iter().map(|&s| s.to_owned()).collect::<Vec<_>>(); opts.extend(codec_opts); @@ -154,28 +141,24 @@ pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: O 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") + let command = Command::new("ffmpeg") .arg("-i") .arg(uri) .args(opts) .stderr(Stdio::piped()) .stdin(Stdio::null()) .stdout(Stdio::piped()) - .spawn() - .map_err(|e| e.into()) + .spawn()?; + + Ok(Box::new(ChildContainer(command))) } 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())) + Ok(pcm(true, command)) } |
