Skip to content

Very slow download? #41

Description

@LeafarDev

Bug:

It looks like it's downloading each piece every 10 seconds

Code:

import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:path_provider/path_provider.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

// Initialize the YoutubeExplode instance.
final yt = YoutubeExplode();

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  print('Type the video id or url: ');

  var url = "https://www.youtube.com/watch?v=I7pBaCorMfM";

  // Save the video to the download directory.
  var dir = await getExternalStorageDirectory();
  Directory(dir.path).createSync();

  // Download the video.
  await download(url);

  yt.close();
  exit(0);
}

Future<void> download(String id) async {
  // Get video metadata.
  var video = await yt.videos.get(id);

  // Get the video manifest.
  var manifest = await yt.videos.streamsClient.getManifest(id);
  var streams = manifest.audioOnly;

  // Get the last audio track (the one with the highest bitrate).
  var audio = streams.last;
  var audioStream = yt.videos.streamsClient.get(audio);

  // Compose the file name removing the unallowed characters in windows.
  var fileName = '${video.title}.${audio.container.name.toString()}'
      .replaceAll('Container.', '')
      .replaceAll(r'\', '')
      .replaceAll('/', '')
      .replaceAll('*', '')
      .replaceAll('?', '')
      .replaceAll('"', '')
      .replaceAll('<', '')
      .replaceAll('>', '')
      .replaceAll('|', '');
  var dir = await getExternalStorageDirectory();
  print(dir.path);
  var file = File('${dir.path}/$fileName');

  // Create the StreamedRequest to track the download status.

  // Open the file in appendMode.
  var output = file.openWrite(mode: FileMode.writeOnlyAppend);

  // Track the file download status.
  var len = audio.size.totalBytes;
  var count = 0;
  var oldProgress = -1;

  // Create the message and set the cursor position.
  var msg = 'Downloading `${video.title}`(.${audio.container.name}):  \n';
  print(msg);
  //  var row = console.cursorPosition.row;
//  var col = msg.length - 2;
//  console.cursorPosition = Coordinate(row, 0);
//  console.write(msg);

  // Listen for data received.
  await for (var data in audioStream) {
    count += data.length;
    var progress = ((count / len) * 100).round();
    if (progress != oldProgress) {
//      console.cursorPosition = Coordinate(row, col);
      print('$progress%');
      oldProgress = progress;
    }
    output.add(data);
  }
  await output.close();
}

Flutter Doctor

    • Flutter version 1.17.3 at /home/rafael/flutter
    • Framework revision b041144f83 (2 weeks ago), 2020-06-04 09:26:11 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /home/rafael/Android/Sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /home/rafael/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6325121/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Android Studio (version 4.1)
    • Android Studio at /home/rafael/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6325121
    • Flutter plugin version 43.0.3
    • Dart plugin version 193.5731
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[!] IntelliJ IDEA Ultimate Edition (version 2019.3)
    • IntelliJ at /home/rafael/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.6911.18
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[✓] Connected device (1 available)
    • Redmi Note 5 • 41e4e3c • android-arm64 • Android 9 (API 28)

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions