Youtubeexplode: How to download just the audiostream as an mp3?

Created on 4 May 2017  路  3Comments  路  Source: Tyrrrz/YoutubeExplode

I understand that this library takes both the video and audio streams and then mixes them together to download a video. How can I do it so that it just downloads the audiostream?

question

Most helpful comment

var client = new YoutubeClient();
var info = await client.GetVideoInfoAsync(video.Id);
var audioStream = info?.AudioStreams?.OrderBy(s => s.Bitrate)?.Last();
using (var input = await client.GetMediaStreamAsync(audioStream))
using (var output = File.Create(fileName))
      await input.CopyToAsync(output);

This will most likely download .webm you will need to convert from .webm to .mp3 yourself
https://github.com/Tyrrrz/YoutubeExplode/wiki/Usage-examples#download-media-stream

All 3 comments

var client = new YoutubeClient();
var info = await client.GetVideoInfoAsync(video.Id);
var audioStream = info?.AudioStreams?.OrderBy(s => s.Bitrate)?.Last();
using (var input = await client.GetMediaStreamAsync(audioStream))
using (var output = File.Create(fileName))
      await input.CopyToAsync(output);

This will most likely download .webm you will need to convert from .webm to .mp3 yourself
https://github.com/Tyrrrz/YoutubeExplode/wiki/Usage-examples#download-media-stream

@ShaiPreter
It doesn't mix them on its own, that's a huge task which is definitely out of scope. You can use ffmpeg via command line interface to do it though, if you need to.
You can get metadata for separated media streams by accessing either the VideoStreams or AudioStreams collections on VideoInfo. The MixedStreams collection is for legacy video types that didn't have separate media.

@SlowLogicBoy
Btw, you don't need to check info and info.AudioStreams for null as they are guaranteed to never be null. In fact, all public properties are guaranteed to not be null. :)
You may want to use LastOrDefault() though, because if the video is muted it won't have any audio streams.

The returned audio stream can be in various formats, not just .webm, however neither of them is .mp3. To get the extension you can use streamInfo.Container.GetFileExtension(). Again, you can use ffmpeg to convert between formats.

@ShaiPreter example project for you, in case you are still interested
https://github.com/Tyrrrz/YoutubeMusicDownloader

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Luke1998x picture Luke1998x  路  5Comments

severino65 picture severino65  路  3Comments

SlowLogicBoy picture SlowLogicBoy  路  7Comments

alexrainman picture alexrainman  路  5Comments

jvitoroc picture jvitoroc  路  8Comments