Search code examples
flutterdartflutter-dependenciesdart-pub

How to download URL using youtube_explode_dart package in 2023?


I want to download videos using audioOnly from youtube using URL, I am using youtube_explode_dart for this purpose, but the code method has migrated now, I cant figure out despite checking documentation of package, that how to download audio only.

my code:

Future<String> getDownloadUrl(String videoId) async {
var youtube = YoutubeExplode();
var video = await youtube.videos.get(videoId);
return youtube.streamsClient.getAudioOnly().first.url;

}


Solution

  • You are doing it right but with old methods, update your code to the following mentioned, also check latest youtube_explode_dart package's documentation to understand better.

    Future<String> getDownloadUrl(String videoId) async {
    var youtube = YoutubeExplode();
    var video = await youtube.videos.get(videoId);
    
    // Get the stream manifest for the video
    var streamManifest = await youtube.videos.streamsClient.getManifest(videoId);
    
    // Get the audio-only streams from the manifest
    var audioOnlyStreams = streamManifest.audioOnly;
    
    // Get the highest quality audio-only stream
    var audioStream = audioOnlyStreams.withHighestBitrate();
    
    // Return the URL of the audio stream
    return audioStream.url.toString();
    
    
    }