Hexer10 / youtube_explode_dart

Dart library to interact with many Youtube APIs
https://pub.dev/packages/youtube_explode_dart
BSD 3-Clause "New" or "Revised" License
322 stars 141 forks source link

[BUG]the downloaded video have'nt the audio #301

Closed Khattak11 closed 2 weeks ago

Khattak11 commented 2 weeks ago

i download video from youtube to local storage but when the video is download it have not the audio here is my code :

void _downloadVideo(String url) async {
    await Permission.storage.request();
    try {
      setState(() {
        isLoading = true;
      });

      String? videoId = extractVideoId(url);
      if (videoId == null) {
        print("Invalid URL: Unable to extract video ID.");
        return;
      }

      var ytExplode = YoutubeExplode();
      var video = await ytExplode.videos.get(videoId);

      videoTitle = video.title;

      var manifest = await ytExplode.videos.streamsClient.getManifest(videoId);

      var streamInfo = manifest.audioOnly.first;
      var videoStream = manifest.video.first;

      var audioStream = ytExplode.videos.streamsClient.get(streamInfo);
      var videoFile = ytExplode.videos.streamsClient.get(videoStream);
      print(video.title);

      _saveVideo(videoFile, videoStream.container.name);
    } catch (e) {
      print("....................." + e.toString());
    }
  }
  void _saveVideo(Stream<List<int>> videoFile, String videoTitle) async {
    try {
      final appDocDir = await getDownloadsDirectory();
      final savePath = appDocDir!.path + '/$videoTitle.mp4';

      final file = File(savePath);
      final sink = file.openWrite();

      // Use pipe to write the stream content to the file
      await videoFile.pipe(sink);
      await sink.close();

      setState(() {
        isLoading = false;
      });
      print("...................................saved$savePath");
    } catch (e) {
      print("....................." + e.toString());
    }
  }
Hexer10 commented 2 weeks ago

You are not saving the audioStream anywhere, .video returns streams what have only videos without audio.