From 7cedad8e5b8e15a19951ed196b3e7e2f3c95ae8e Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sun, 17 Feb 2019 23:02:16 -0500 Subject: initial mp3 attempt --- src/audio/ytdl.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/audio/ytdl.rs') diff --git a/src/audio/ytdl.rs b/src/audio/ytdl.rs index e88d8f8..7333c18 100644 --- a/src/audio/ytdl.rs +++ b/src/audio/ytdl.rs @@ -46,6 +46,7 @@ pub(crate) trait CodecInfo { pub(crate) struct Pcm {} pub(crate) struct Opus {} +pub(crate) struct Mp3 {} impl CodecInfo for Pcm { #[inline] @@ -79,6 +80,22 @@ impl CodecInfo for Opus { } } +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 { let args = [ "-f", @@ -113,7 +130,7 @@ pub fn ytdl_url(uri: &str) -> Result { } } -pub(crate) fn ffmpeg_dl(uri: &str, start: Option, end: Option, size_limit: Option) -> Result> { +pub(crate) fn ffmpeg_dl(uri: &str, start: Option, end: Option, size_limit: Option) -> Result { 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,20 +163,19 @@ pub(crate) fn ffmpeg_dl(uri: &str, start: Option, end: O debug!("ffmpeg -i \"{}\" {}", uri, opts.join(" ")); - let command = Command::new("ffmpeg") + Command::new("ffmpeg") .arg("-i") .arg(uri) .args(opts) .stderr(Stdio::piped()) .stdin(Stdio::null()) .stdout(Stdio::piped()) - .spawn()?; - - Ok(Box::new(ChildContainer(command))) + .spawn() + .map_err(|e| e.into()) } pub fn ytdl(uri: &str, start: Option, end: Option) -> Result> { let youtube_uri = ytdl_url(uri)?; let command = ffmpeg_dl::(&youtube_uri, start, end, None)?; - Ok(pcm(true, command)) + Ok(pcm(true, command.stdout.unwrap())) } -- cgit v1.3.1