youtube_explode/example/video_download.dart

84 lines
2.1 KiB
Dart
Raw Normal View History

2020-06-05 20:18:42 +02:00
//TODO: Fixing the console printing.
2020-02-20 19:50:10 +01:00
import 'dart:async';
import 'dart:io';
2020-06-27 21:32:03 +02:00
import 'package:console/console.dart';
2020-02-20 19:55:45 +01:00
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
2020-02-20 19:50:10 +01:00
// Initialize the YoutubeExplode instance.
final yt = YoutubeExplode();
Future<void> main() async {
2020-06-27 21:32:03 +02:00
stdout.writeln('Type the video id or url: ');
2020-03-06 23:18:33 +01:00
2021-03-11 14:20:10 +01:00
var url = stdin.readLineSync()!.trim();
2020-02-20 19:50:10 +01:00
// Save the video to the download directory.
Directory('downloads').createSync();
// Download the video.
2020-06-05 20:08:04 +02:00
await download(url);
2020-02-20 19:50:10 +01:00
yt.close();
exit(0);
}
Future<void> download(String id) async {
2020-06-05 20:08:04 +02:00
// Get video metadata.
var video = await yt.videos.get(id);
// Get the video manifest.
var manifest = await yt.videos.streamsClient.getManifest(id);
2020-11-06 22:46:47 +01:00
var streams = manifest.videoOnly;
2020-02-20 19:50:10 +01:00
// Get the audio track with the highest bitrate.
2020-11-06 22:46:47 +01:00
var audio = streams.first;
2020-06-05 20:08:04 +02:00
var audioStream = yt.videos.streamsClient.get(audio);
2020-02-20 19:50:10 +01:00
// Compose the file name removing the unallowed characters in windows.
2022-02-03 12:06:09 +01:00
var fileName = '${video.title}.${audio.container.name}'
2020-06-05 20:08:04 +02:00
.replaceAll(r'\', '')
.replaceAll('/', '')
.replaceAll('*', '')
.replaceAll('?', '')
.replaceAll('"', '')
.replaceAll('<', '')
.replaceAll('>', '')
.replaceAll('|', '');
2020-02-20 19:50:10 +01:00
var file = File('downloads/$fileName');
2020-06-27 21:32:03 +02:00
// Delete the file if exists.
if (file.existsSync()) {
file.deleteSync();
}
2020-02-20 19:50:10 +01:00
2020-06-27 21:32:03 +02:00
// Open the file in writeAppend.
2020-02-20 19:50:10 +01:00
var output = file.openWrite(mode: FileMode.writeOnlyAppend);
// Track the file download status.
2020-06-05 20:08:04 +02:00
var len = audio.size.totalBytes;
2020-02-20 19:50:10 +01:00
var count = 0;
// Create the message and set the cursor position.
2020-06-27 21:32:03 +02:00
var msg = 'Downloading ${video.title}.${audio.container.name}';
stdout.writeln(msg);
2020-06-05 20:08:04 +02:00
2020-02-20 19:50:10 +01:00
// Listen for data received.
2020-06-27 21:32:03 +02:00
var progressBar = ProgressBar();
2021-03-11 14:20:10 +01:00
await for (final data in audioStream) {
2020-06-27 21:32:03 +02:00
// Keep track of the current downloaded data.
2020-02-20 19:50:10 +01:00
count += data.length;
2020-06-27 21:32:03 +02:00
// Calculate the current progress.
var progress = ((count / len) * 100).ceil();
// Update the progressbar.
progressBar.update(progress);
// Write to file.
2020-02-20 19:50:10 +01:00
output.add(data);
2020-03-06 23:18:33 +01:00
}
await output.close();
2020-02-20 19:50:10 +01:00
}