Skip to content

Commit

Permalink
Add voice::streamer::ffmpeg_optioned
Browse files Browse the repository at this point in the history
Add a method for specifying custom ffmpeg arguments.

Closes #266.
  • Loading branch information
Zeyla Hellyer committed Jul 9, 2018
1 parent e602630 commit 5dab87b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/voice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use self::{
streamer::{
dca,
ffmpeg,
ffmpeg_optioned,
opus,
pcm,
ytdl
Expand Down
41 changes: 38 additions & 3 deletions src/voice/streamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn _ffmpeg(path: &OsStr) -> Result<Box<AudioSource>> {
let is_stereo = is_stereo(path).unwrap_or(false);
let stereo_val = if is_stereo { "2" } else { "1" };

let args = [
ffmpeg_optioned(path, &[
"-f",
"s16le",
"-ac",
Expand All @@ -118,12 +118,47 @@ fn _ffmpeg(path: &OsStr) -> Result<Box<AudioSource>> {
"-acodec",
"pcm_s16le",
"-",
];
])
}

/// Opens an audio file through `ffmpeg` and creates an audio source, with
/// user-specified arguments to pass to ffmpeg.
///
/// Note that this does _not_ build on the arguments passed by the [`ffmpeg`]
/// function.
///
/// # Examples
///
/// Pass options to create a custom ffmpeg streamer:
///
/// ```rust,no_run
/// use serenity::voice;
///
/// let streamer = voice::ffmpeg_optioned("./some_file.mp3", &[
/// "-f",
/// "s16le",
/// "-ac",
/// stereo_val,
/// "-ar",
/// "48000",
/// "-acodec",
/// "pcm_s16le",
/// "-",
/// ]);
pub fn ffmpeg_optioned<P: AsRef<OsStr>>(
path: P,
args: &[&str],
) -> Result<Box<AudioSource>> {
_ffmpeg_optioned(path.as_ref(), args)
}

fn _ffmpeg_optioned(path: &OsStr, args: &[&str]) -> Result<Box<AudioSource>> {
let is_stereo = is_stereo(path).unwrap_or(false);

let command = Command::new("ffmpeg")
.arg("-i")
.arg(path)
.args(&args)
.args(args)
.stderr(Stdio::null())
.stdin(Stdio::null())
.stdout(Stdio::piped())
Expand Down

0 comments on commit 5dab87b

Please sign in to comment.