Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very slow download? #41

Closed
LeafarDev opened this issue Jun 19, 2020 · 10 comments
Closed

Very slow download? #41

LeafarDev opened this issue Jun 19, 2020 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@LeafarDev
Copy link

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.
@LeafarDev LeafarDev added the bug Something isn't working label Jun 19, 2020
@Hexer10
Copy link
Owner

Hexer10 commented Jun 19, 2020

I could reproduce this, this is happening because ratebypass parameters is not parsed correctly, gonna push a fix asap

@Hexer10 Hexer10 self-assigned this Jun 19, 2020
@Hexer10
Copy link
Owner

Hexer10 commented Jun 19, 2020

Please check if it is faster with v1.2.2

@LeafarDev
Copy link
Author

Much better now !

@LeafarDev
Copy link
Author

Hi again. Sounds like the file is partially corrupted, can you reproduce same issue?

@LeafarDev LeafarDev reopened this Jun 19, 2020
@Hexer10
Copy link
Owner

Hexer10 commented Jun 19, 2020

Mh, I couldn't find any major issues, is it randomly corrupted? Are missing some parts at the start, middle or end?
Does it happen for both short and long videos?

@LeafarDev
Copy link
Author

LeafarDev commented Jun 19, 2020

It's happening with long videos like this. When trying to seek in the middle of audio the player crash or go back to 00:00.

@Hexer10
Copy link
Owner

Hexer10 commented Jun 20, 2020

Seems like that I download the streams with the range headers every 9898989 bytes, one byte is duplicated, and I think this might cause some issues...

A quick side note, I forgot to update the docs but instead of .last to get the best audio stream you should use .withHighestBitrate()

@Hexer10
Copy link
Owner

Hexer10 commented Jun 20, 2020

Pushed v1.2.3, which should address this, please let me know if the issue still persists.
I couldn't test this since atm I have a very slow connection and it would take me years to download that video

@mymikemiller
Copy link
Contributor

I'm still having very slow downloads on 1.3.1. For example, this three minute video takes over a half hour to download (and often fails partway through and has to be restarted due to an HttpException, but that's another issue). My download code is in the linked issue.

If I pause and look at videoStreamInfo.url, I get a url like this, and when I go to that url in the browser, the entire video downloads in seconds.

Should I skip calling _yt.videos.streamsClient.get(videoStreamInfo) and just make a standard http request to download the video? Or maybe use a library like dio; people seem to like that one. But then I'm only using youtube_explode to get to the links, not to do the actual download. Are there any potential issues doing it that way? Maybe I'm more likely to be blocked by YouTube if I download directly?

@mymikemiller
Copy link
Contributor

mymikemiller commented Jun 23, 2020

Edit: downloading the above three minute video actually takes about 30 seconds. The first ~20% of the video is downloaded in a few seconds, and the remaining 80% takes about 25.

I forgot I had some debug stuff in there, which was actually what was causing it to take >30 minutes. Oops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants