[convert] clean conversion
This commit is contained in:
parent
2d4fb0253b
commit
0702774345
|
|
@ -11,12 +11,12 @@ enum Format {
|
||||||
mp3(
|
mp3(
|
||||||
format: 'MP3',
|
format: 'MP3',
|
||||||
ytCmd: 'bestaudio[ext=m4a]/bestaudio[ext=webm]',
|
ytCmd: 'bestaudio[ext=m4a]/bestaudio[ext=webm]',
|
||||||
ffmpegCmd: '-f mp3 -loglevel quiet -ab 192k -vn',
|
ffmpegCmd: '-f mp3 -ab 192k -vn',
|
||||||
extension: 'mp3'),
|
extension: 'mp3'),
|
||||||
mp3HD(
|
mp3HD(
|
||||||
format: 'MP3 HD',
|
format: 'MP3 HD',
|
||||||
ytCmd: 'bestaudio[ext=m4a]/bestaudio[ext=webm]',
|
ytCmd: 'bestaudio[ext=m4a]/bestaudio[ext=webm]',
|
||||||
ffmpegCmd: '-f mp3 -loglevel quiet -ab 320k -vn',
|
ffmpegCmd: '-f mp3 -ab 320k -vn',
|
||||||
extension: 'mp3'),
|
extension: 'mp3'),
|
||||||
mp4(format: 'MP4'),
|
mp4(format: 'MP4'),
|
||||||
mp4HD(
|
mp4HD(
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ class Video extends Equatable {
|
||||||
this.uploadDate = '',
|
this.uploadDate = '',
|
||||||
this.filename = '',
|
this.filename = '',
|
||||||
this.playlistTitle = '',
|
this.playlistTitle = '',
|
||||||
|
this.destination = '',
|
||||||
});
|
});
|
||||||
|
|
||||||
final String id;
|
final String id;
|
||||||
|
|
@ -35,6 +36,7 @@ class Video extends Equatable {
|
||||||
final String uploadDate;
|
final String uploadDate;
|
||||||
final String filename;
|
final String filename;
|
||||||
final String playlistTitle;
|
final String playlistTitle;
|
||||||
|
final String destination;
|
||||||
|
|
||||||
factory Video.fromJson(Map<String, dynamic> json) {
|
factory Video.fromJson(Map<String, dynamic> json) {
|
||||||
return Video(
|
return Video(
|
||||||
|
|
@ -66,6 +68,7 @@ class Video extends Equatable {
|
||||||
String? uploadDate,
|
String? uploadDate,
|
||||||
String? filename,
|
String? filename,
|
||||||
String? playlistTitle,
|
String? playlistTitle,
|
||||||
|
String? destination,
|
||||||
}) {
|
}) {
|
||||||
return Video(
|
return Video(
|
||||||
id: id ?? this.id,
|
id: id ?? this.id,
|
||||||
|
|
@ -82,6 +85,7 @@ class Video extends Equatable {
|
||||||
uploadDate: uploadDate ?? this.uploadDate,
|
uploadDate: uploadDate ?? this.uploadDate,
|
||||||
filename: filename ?? this.filename,
|
filename: filename ?? this.filename,
|
||||||
playlistTitle: playlistTitle ?? this.playlistTitle,
|
playlistTitle: playlistTitle ?? this.playlistTitle,
|
||||||
|
destination: destination ?? this.destination,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,5 +104,6 @@ class Video extends Equatable {
|
||||||
isParsed,
|
isParsed,
|
||||||
filename,
|
filename,
|
||||||
playlistTitle,
|
playlistTitle,
|
||||||
|
destination
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,13 @@ class ConverterService {
|
||||||
|
|
||||||
Future<void> _init() async {
|
Future<void> _init() async {
|
||||||
tempDir = await getTemporaryDirectory();
|
tempDir = await getTemporaryDirectory();
|
||||||
|
if (Platform.isWindows) {
|
||||||
|
ffmpegPath = 'ffmpeg.exe';
|
||||||
|
} else if (Platform.isLinux) {
|
||||||
|
ffmpegPath = 'ffmpeg';
|
||||||
|
} else if (Platform.isMacOS) {
|
||||||
|
ffmpegPath = 'ffmpeg';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<File?> getTmpFile(String filename) async {
|
Future<File?> getTmpFile(String filename) async {
|
||||||
|
|
@ -34,6 +41,7 @@ class ConverterService {
|
||||||
FileLogger().d('dirFilename $dirFilename');
|
FileLogger().d('dirFilename $dirFilename');
|
||||||
FileLogger().d('filename $filename');
|
FileLogger().d('filename $filename');
|
||||||
if (dirFilename == '${filename}_tmp') {
|
if (dirFilename == '${filename}_tmp') {
|
||||||
|
FileLogger().d('Found tmp file ${file.path}');
|
||||||
return File(file.path);
|
return File(file.path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -44,29 +52,39 @@ class ConverterService {
|
||||||
FileLogger().d(
|
FileLogger().d(
|
||||||
'____Converting ${video.title} to ${video.format.format} format_____');
|
'____Converting ${video.title} to ${video.format.format} format_____');
|
||||||
|
|
||||||
File tmpFile = File('temp/${video.filename}_tmp.mp4');
|
File tmpFile = File('${tempDir.path}/${video.filename}_tmp.mp4');
|
||||||
if (!tmpFile.existsSync()) {
|
var dlFileExist = await tmpFile.exists();
|
||||||
tmpFile = await getTmpFile(video.filename) ??
|
FileLogger().d('dlFileExist: $dlFileExist');
|
||||||
File('temp/${video.filename}_tmp.mp4');
|
if (!dlFileExist) {
|
||||||
|
FileLogger()
|
||||||
|
.d('File not found, testing destination ${video.destination}');
|
||||||
|
if (video.destination != '') {
|
||||||
|
tmpFile = File('${tempDir.path}/${video.destination}');
|
||||||
}
|
}
|
||||||
|
if (!tmpFile.existsSync()) {
|
||||||
|
FileLogger().d('File not found, testing temp directory');
|
||||||
|
tmpFile = await getTmpFile(video.filename) ??
|
||||||
|
File('${tempDir.path}/${video.filename}_tmp.mp4');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FileLogger().d('tmpFile: ${tmpFile.path}');
|
||||||
|
|
||||||
File doneFile =
|
File doneFile = File(
|
||||||
File('temp/${video.filename}_done.${video.format.extension}');
|
'${tempDir.path}/${video.filename}_done.${video.format.extension}');
|
||||||
if (doneFile.existsSync()) {
|
if (doneFile.existsSync()) {
|
||||||
FileLogger().d('File already converted');
|
FileLogger().d('File already converted');
|
||||||
return Stream.fromIterable(['progress=end']);
|
return Stream.fromIterable(['progress=end']);
|
||||||
}
|
}
|
||||||
|
|
||||||
var command =
|
var command =
|
||||||
'-i "${tmpFile.path}" ${video.format.ffmpegCmd} "temp/${video.filename}_done.${video.format.extension}"';
|
"-i '${tmpFile.path}' ${video.format.ffmpegCmd} '${tempDir.path}/${video.filename}_done.${video.format.extension}'";
|
||||||
|
|
||||||
var shellLinesController = ShellLinesController();
|
var shellLinesController = ShellLinesController();
|
||||||
var shell = Shell(
|
var shell = Shell(
|
||||||
stdout: shellLinesController.sink, stderr: shellLinesController.sink);
|
stdout: shellLinesController.sink, stderr: shellLinesController.sink);
|
||||||
|
|
||||||
await shell.run('''
|
FileLogger().d('Running $ffmpegPath $command');
|
||||||
$ffmpegPath $command
|
await shell.run("$ffmpegPath $command");
|
||||||
''');
|
|
||||||
|
|
||||||
return shellLinesController.stream;
|
return shellLinesController.stream;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ class FileLogger {
|
||||||
static final FileLogger _instance = FileLogger._internal();
|
static final FileLogger _instance = FileLogger._internal();
|
||||||
late Logger _logger;
|
late Logger _logger;
|
||||||
late File _logFile;
|
late File _logFile;
|
||||||
|
late Directory tempDir;
|
||||||
|
|
||||||
factory FileLogger() {
|
factory FileLogger() {
|
||||||
return _instance;
|
return _instance;
|
||||||
|
|
@ -15,7 +16,8 @@ class FileLogger {
|
||||||
FileLogger._internal();
|
FileLogger._internal();
|
||||||
|
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
_logFile = File('apps_logs.txt');
|
tempDir = await getTemporaryDirectory();
|
||||||
|
_logFile = File('${tempDir.path}/apps_logs.txt');
|
||||||
|
|
||||||
_logger = Logger(
|
_logger = Logger(
|
||||||
filter: ProductionFilter(),
|
filter: ProductionFilter(),
|
||||||
|
|
@ -30,6 +32,7 @@ class FileLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
void i(String message) {
|
void i(String message) {
|
||||||
|
debugPrint(message);
|
||||||
_logger.i(message);
|
_logger.i(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,6 +42,7 @@ class FileLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
void e(String message, [dynamic error, StackTrace? stackTrace]) {
|
void e(String message, [dynamic error, StackTrace? stackTrace]) {
|
||||||
|
debugPrint(message);
|
||||||
_logger.e(message, error: error, stackTrace: stackTrace);
|
_logger.e(message, error: error, stackTrace: stackTrace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import 'package:notube/models/video.dart';
|
||||||
import 'package:notube/services/converter.dart';
|
import 'package:notube/services/converter.dart';
|
||||||
import 'package:notube/services/download.dart';
|
import 'package:notube/services/download.dart';
|
||||||
import 'package:notube/constants.dart';
|
import 'package:notube/constants.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:downloadsfolder/downloadsfolder.dart';
|
import 'package:downloadsfolder/downloadsfolder.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
|
|
@ -19,6 +20,7 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
|
|
||||||
late DLServices dlService;
|
late DLServices dlService;
|
||||||
late ConverterService converterService;
|
late ConverterService converterService;
|
||||||
|
late Directory tempDir;
|
||||||
|
|
||||||
Iterable<Video> downloadingVid = [];
|
Iterable<Video> downloadingVid = [];
|
||||||
|
|
||||||
|
|
@ -47,6 +49,22 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future changeDestination(Video video, String destination) async {
|
||||||
|
var completer = Completer<void>();
|
||||||
|
var tmpVideoList = state.videoList.toList();
|
||||||
|
int index = tmpVideoList.indexWhere((v) => v.id == video.id);
|
||||||
|
|
||||||
|
if (index != -1) {
|
||||||
|
tmpVideoList[index] = video.copyWith(destination: destination);
|
||||||
|
emit(VideosState(videoList: tmpVideoList));
|
||||||
|
completer.complete(tmpVideoList[index]);
|
||||||
|
} else {
|
||||||
|
FileLogger().d('Video not found in the list');
|
||||||
|
completer.completeError('Video not found in the list');
|
||||||
|
}
|
||||||
|
return completer.future;
|
||||||
|
}
|
||||||
|
|
||||||
void setGlobalStatus(String status) {
|
void setGlobalStatus(String status) {
|
||||||
emit(VideosState(status: status));
|
emit(VideosState(status: status));
|
||||||
}
|
}
|
||||||
|
|
@ -71,6 +89,7 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> startConverter() async {
|
Future<void> startConverter() async {
|
||||||
|
tempDir = await getTemporaryDirectory();
|
||||||
var videosToConvert = state.videoList.where((video) =>
|
var videosToConvert = state.videoList.where((video) =>
|
||||||
video.status == 'downloaded' &&
|
video.status == 'downloaded' &&
|
||||||
convertedFormats.contains(video.format));
|
convertedFormats.contains(video.format));
|
||||||
|
|
@ -127,15 +146,15 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
for (Video video in state.videoList.where((video) =>
|
for (Video video in state.videoList.where((video) =>
|
||||||
video.status == 'downloaded' || video.status == 'converted')) {
|
video.status == 'downloaded' || video.status == 'converted')) {
|
||||||
var cleanTitle = video.title.replaceAll(RegExp(r'[^\w\s]+'), '');
|
var cleanTitle = video.title.replaceAll(RegExp(r'[^\w\s]+'), '');
|
||||||
final tmpFile =
|
final tmpFile = File(
|
||||||
File('temp/${video.filename}_done.${video.format.extension}');
|
'${tempDir.path}/${video.filename}_done.${video.format.extension}');
|
||||||
|
|
||||||
var playlistTitle = video.playlistTitle;
|
var playlistTitle = video.playlistTitle;
|
||||||
|
if (playlistTitle.isNotEmpty && playlistTitle != '') {
|
||||||
await Directory('${downloadDirectory.path}/$playlistTitle')
|
await Directory('${downloadDirectory.path}/$playlistTitle')
|
||||||
.create(recursive: true)
|
.create(recursive: true)
|
||||||
.catchError((e) => FileLogger().e('Error creating directory: $e'));
|
.catchError((e) => FileLogger().e('Error creating directory: $e'));
|
||||||
FileLogger().d('Playlist title: $playlistTitle');
|
FileLogger().d('Playlist title: $playlistTitle');
|
||||||
if (playlistTitle.isNotEmpty && playlistTitle != '') {
|
|
||||||
cleanTitle = '$playlistTitle/$cleanTitle';
|
cleanTitle = '$playlistTitle/$cleanTitle';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,6 +164,7 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
var isMoved = false;
|
var isMoved = false;
|
||||||
while (!isMoved) {
|
while (!isMoved) {
|
||||||
try {
|
try {
|
||||||
|
FileLogger().d('Moving ${tmpFile.path} to ${newFile.path}');
|
||||||
await tmpFile.rename(newFile.path);
|
await tmpFile.rename(newFile.path);
|
||||||
isMoved = true;
|
isMoved = true;
|
||||||
} on FileSystemException catch (e) {
|
} on FileSystemException catch (e) {
|
||||||
|
|
@ -152,9 +172,10 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
await Future.delayed(Duration(seconds: 1));
|
await Future.delayed(Duration(seconds: 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileLogger().d('File moved to ${newFile.path}');
|
||||||
await changeStatus(video, 'done');
|
await changeStatus(video, 'done');
|
||||||
await archiveVideo(video.copyWith(status: 'done'));
|
await archiveVideo(video.copyWith(status: 'done'));
|
||||||
FileLogger().d('File moved to ${newFile.path}');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -216,21 +237,50 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
FileLogger().d(
|
FileLogger().d(
|
||||||
'___Downloading ${video.title} in ${video.format} format ${video.filename}____');
|
'___Downloading ${video.title} in ${video.format} format ${video.filename}____');
|
||||||
|
|
||||||
FileLogger().d(
|
|
||||||
'___Downloading ${video.title} in ${video.format} format ${video.filename}____');
|
|
||||||
final shellStream = await dlService.downloadFile(video);
|
final shellStream = await dlService.downloadFile(video);
|
||||||
var completer = Completer<void>();
|
var completer = Completer<void>();
|
||||||
|
|
||||||
shellStream.listen(
|
shellStream.listen(
|
||||||
(line) {
|
(line) async {
|
||||||
if (line.contains('[download]') &&
|
if (line.contains('[download]') &&
|
||||||
line.contains('of') &&
|
line.contains('of') &&
|
||||||
line.contains('%')) {
|
line.contains('%')) {
|
||||||
var percentString =
|
var percentString =
|
||||||
line.split('[download]')[1].split(' of')[0].split('%')[0].trim();
|
line.split('[download]')[1].split(' of')[0].split('%')[0].trim();
|
||||||
var percent = percentString == null ? 0 : double.parse(percentString);
|
var percent = percentString == null ? 0 : double.parse(percentString);
|
||||||
|
|
||||||
|
if (audioFormats.contains(video.format) && percent == 100) {
|
||||||
|
changeStatus(video, 'downloaded');
|
||||||
|
if (!completer.isCompleted) {
|
||||||
|
FileLogger().d('____Download completed___');
|
||||||
|
FileLogger().d('Download completed');
|
||||||
|
runningTasks--;
|
||||||
|
completer.complete();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
changeStatus(video, 'downloading $percent%');
|
changeStatus(video, 'downloading $percent%');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.contains('Destination:')) {
|
||||||
|
FileLogger().w('Destination: $line');
|
||||||
|
var destination =
|
||||||
|
line.split('Destination:')[1].trim().split(RegExp(r'[\\/]')).last;
|
||||||
|
video = await changeDestination(video, destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.contains('has already been downloaded')) {
|
||||||
|
FileLogger().w('Destination: $line');
|
||||||
|
RegExp regExp = RegExp(r'\\([^\\]+?\.[^\\]+?)\s');
|
||||||
|
Match? match = regExp.firstMatch(line);
|
||||||
|
if (match != null) {
|
||||||
|
String destination = match.group(1)!;
|
||||||
|
FileLogger().w('Destination: $destination');
|
||||||
|
video = await changeDestination(video, destination);
|
||||||
|
} else {
|
||||||
|
FileLogger().w('Destination: $line');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (line.contains('[EmbedThumbnail]')) {
|
if (line.contains('[EmbedThumbnail]')) {
|
||||||
changeStatus(video, 'downloaded');
|
changeStatus(video, 'downloaded');
|
||||||
|
|
@ -242,7 +292,7 @@ class VideosCubit extends Cubit<VideosState> {
|
||||||
completer.complete();
|
completer.complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//FileLogger().d(line);
|
FileLogger().d(line);
|
||||||
},
|
},
|
||||||
onError: (error) {
|
onError: (error) {
|
||||||
if (!completer.isCompleted) {
|
if (!completer.isCompleted) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue