115 lines
3.1 KiB
Dart
115 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:notube/models/Video.dart';
|
|
import 'package:notube/states/dlFormState.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:process_run/shell.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'dart:developer';
|
|
|
|
class DLServices {
|
|
late Directory tempDir;
|
|
late String assetName;
|
|
late String ytDlpPath;
|
|
|
|
String extractor = '';
|
|
String url = '';
|
|
|
|
bool isPlaylist = false;
|
|
String playlistTitle = '';
|
|
|
|
List<Video> _videos = [];
|
|
|
|
List<Video> get videos => _videos;
|
|
|
|
DLServices._();
|
|
|
|
static Future<DLServices> init() async {
|
|
debugPrint('Initializing DLServices');
|
|
var dlService = DLServices._();
|
|
await dlService._init();
|
|
return dlService;
|
|
}
|
|
|
|
Future<void> _init() async {
|
|
tempDir = await getTemporaryDirectory();
|
|
assetName = 'yt-dlp.exe';
|
|
await copyExecutable();
|
|
}
|
|
|
|
Future<void> copyExecutable() async {
|
|
File tempFile = File('${tempDir.path}/$assetName');
|
|
|
|
ByteData data = await rootBundle.load('assets/executable/$assetName');
|
|
await tempFile.exists().then((value) {
|
|
if (!value) {
|
|
debugPrint('Copying $assetName to ${tempFile.path}');
|
|
tempFile.writeAsBytes(
|
|
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
|
|
}
|
|
});
|
|
|
|
ytDlpPath = tempFile.path;
|
|
}
|
|
|
|
Future<void> downloadFile(String url, String format) async {
|
|
debugPrint('Downloading $url in $format format');
|
|
|
|
/*
|
|
https://youtu.be/playlist?list=PLk1fi9OrZmvGBdh9BdWIhZImGVDUqls1X
|
|
*/
|
|
var command =
|
|
'${url.trim()} --sub-langs "all,-live_chat" --embed-subs --embed-thumbnail --embed-metadata --windows-filenames --progress -o "temp/%(playlist)s/%(title)s"';
|
|
|
|
var shellLinesController = ShellLinesController();
|
|
var shell = Shell(stdout: shellLinesController.sink);
|
|
|
|
shellLinesController.stream.listen((line) {
|
|
if (line.contains('[download]')) {
|
|
debugPrint('___Download___');
|
|
debugPrint('%=${line.split('[download]')[1].split(' of')[0].trim()}');
|
|
debugPrint('______');
|
|
}
|
|
debugPrint(line);
|
|
});
|
|
|
|
debugPrint('Running $ytDlpPath $command');
|
|
await shell.run('''
|
|
$ytDlpPath $command
|
|
''');
|
|
}
|
|
|
|
Future<void> analyseUrl(String url) async {
|
|
debugPrint('Analyse $url');
|
|
|
|
var command = '${url.trim()} -q --flat-playlist -J';
|
|
|
|
var shellLinesController = ShellLinesController();
|
|
var shell = Shell(stdout: shellLinesController.sink);
|
|
|
|
shellLinesController.stream.listen((line) {
|
|
if (line[0] == '{') {
|
|
var dataInfos = jsonDecode(line);
|
|
extractor = dataInfos['extractor_key'];
|
|
isPlaylist = dataInfos['_type'] == 'playlist';
|
|
if (isPlaylist) {
|
|
playlistTitle = dataInfos['title'];
|
|
for (var video in dataInfos['entries']) {
|
|
videos.add(Video.fromJson(video));
|
|
}
|
|
} else {
|
|
videos.add(Video.fromJson(dataInfos));
|
|
}
|
|
}
|
|
});
|
|
|
|
await shell.run('''
|
|
$ytDlpPath $command
|
|
''');
|
|
}
|
|
}
|