Skip to content

Instantly share code, notes, and snippets.

@SlowLogicBoy
Last active August 8, 2018 05:44
Show Gist options
  • Save SlowLogicBoy/45f25b3711c341ba974fe22f04f71f4a to your computer and use it in GitHub Desktop.
Save SlowLogicBoy/45f25b3711c341ba974fe22f04f71f4a to your computer and use it in GitHub Desktop.
This prototype pipes audio stream to ffmpeg and get result stream back.
#r "nuget: YoutubeExplode, *"
using YoutubeExplode;
using YoutubeExplode.Models.MediaStreams;
const string FFmpegPath = "./ffmpeg.exe";
var youtubeClient = new YoutubeClient();
var mediaStreamInfo = await youtubeClient.GetVideoMediaStreamInfosAsync("uBg-nNJCnmM");
var audioStream = mediaStreamInfo.Audio.WithHighestBitrate();
var processStartInfo = new ProcessStartInfo
{
FileName = Path.GetFullPath(FFmpegPath),
Arguments = $"-i pipe:0 -vn -ar 44100 -ac 2 -ab 192k -f mp3 -",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
using (var process = Process.Start(processStartInfo))
{
using (var fs = new FileStream("output.mp3", FileMode.CreateNew))
{
var downloadTask = youtubeClient.DownloadMediaStreamAsync(audioStream, process.StandardInput.BaseStream);
var outputTask = process.StandardOutput.BaseStream.CopyToAsync(fs);
await downloadTask;
process.StandardInput.Close();
await outputTask;
process.StandardOutput.Close();
}
process.WaitForExit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment