[convert] clean conversion

This commit is contained in:
jscampucci 2024-08-27 16:41:28 +02:00
parent b3f73fd514
commit 2ad07c5395
5 changed files with 102 additions and 25 deletions

View File

@ -11,12 +11,12 @@ enum Format {
mp3(
format: 'MP3',
ytCmd: 'bestaudio[ext=m4a]/bestaudio[ext=webm]',
ffmpegCmd: '-f mp3 -loglevel quiet -ab 192k -vn',
ffmpegCmd: '-f mp3 -ab 192k -vn',
extension: 'mp3'),
mp3HD(
format: 'MP3 HD',
ytCmd: 'bestaudio[ext=m4a]/bestaudio[ext=webm]',
ffmpegCmd: '-f mp3 -loglevel quiet -ab 320k -vn',
ffmpegCmd: '-f mp3 -ab 320k -vn',
extension: 'mp3'),
mp4(format: 'MP4'),
mp4HD(

View File

@ -19,6 +19,7 @@ class Video extends Equatable {
this.uploadDate = '',
this.filename = '',
this.playlistTitle = '',
this.destination = '',
});
final String id;
@ -35,6 +36,7 @@ class Video extends Equatable {
final String uploadDate;
final String filename;
final String playlistTitle;
final String destination;
factory Video.fromJson(Map<String, dynamic> json) {
return Video(
@ -66,6 +68,7 @@ class Video extends Equatable {
String? uploadDate,
String? filename,
String? playlistTitle,
String? destination,
}) {
return Video(
id: id ?? this.id,
@ -82,6 +85,7 @@ class Video extends Equatable {
uploadDate: uploadDate ?? this.uploadDate,
filename: filename ?? this.filename,
playlistTitle: playlistTitle ?? this.playlistTitle,
destination: destination ?? this.destination,
);
}
@ -100,5 +104,6 @@ class Video extends Equatable {
isParsed,
filename,
playlistTitle,
destination
];
}

View File

@ -24,6 +24,13 @@ class ConverterService {
Future<void> _init() async {
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 {
@ -34,6 +41,7 @@ class ConverterService {
FileLogger().d('dirFilename $dirFilename');
FileLogger().d('filename $filename');
if (dirFilename == '${filename}_tmp') {
FileLogger().d('Found tmp file ${file.path}');
return File(file.path);
}
}
@ -44,29 +52,39 @@ class ConverterService {
FileLogger().d(
'____Converting ${video.title} to ${video.format.format} format_____');
File tmpFile = File('temp/${video.filename}_tmp.mp4');
if (!tmpFile.existsSync()) {
tmpFile = await getTmpFile(video.filename) ??
File('temp/${video.filename}_tmp.mp4');
File tmpFile = File('${tempDir.path}/${video.filename}_tmp.mp4');
var dlFileExist = await tmpFile.exists();
FileLogger().d('dlFileExist: $dlFileExist');
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('temp/${video.filename}_done.${video.format.extension}');
File doneFile = File(
'${tempDir.path}/${video.filename}_done.${video.format.extension}');
if (doneFile.existsSync()) {
FileLogger().d('File already converted');
return Stream.fromIterable(['progress=end']);
}
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 shell = Shell(
stdout: shellLinesController.sink, stderr: shellLinesController.sink);
await shell.run('''
$ffmpegPath $command
''');
FileLogger().d('Running $ffmpegPath $command');
await shell.run("$ffmpegPath $command");
return shellLinesController.stream;
}

View File

@ -7,6 +7,7 @@ class FileLogger {
static final FileLogger _instance = FileLogger._internal();
late Logger _logger;
late File _logFile;
late Directory tempDir;
factory FileLogger() {
return _instance;
@ -15,7 +16,8 @@ class FileLogger {
FileLogger._internal();
Future<void> init() async {
_logFile = File('apps_logs.txt');
tempDir = await getTemporaryDirectory();
_logFile = File('${tempDir.path}/apps_logs.txt');
_logger = Logger(
filter: ProductionFilter(),
@ -30,6 +32,7 @@ class FileLogger {
}
void i(String message) {
debugPrint(message);
_logger.i(message);
}
@ -39,6 +42,7 @@ class FileLogger {
}
void e(String message, [dynamic error, StackTrace? stackTrace]) {
debugPrint(message);
_logger.e(message, error: error, stackTrace: stackTrace);
}

View File

@ -7,6 +7,7 @@ import 'package:notube/models/video.dart';
import 'package:notube/services/converter.dart';
import 'package:notube/services/download.dart';
import 'package:notube/constants.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:downloadsfolder/downloadsfolder.dart';
import 'package:path/path.dart' as p;
@ -19,6 +20,7 @@ class VideosCubit extends Cubit<VideosState> {
late DLServices dlService;
late ConverterService converterService;
late Directory tempDir;
Iterable<Video> downloadingVid = [];
@ -47,6 +49,22 @@ class VideosCubit extends Cubit<VideosState> {
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) {
emit(VideosState(status: status));
}
@ -71,6 +89,7 @@ class VideosCubit extends Cubit<VideosState> {
}
Future<void> startConverter() async {
tempDir = await getTemporaryDirectory();
var videosToConvert = state.videoList.where((video) =>
video.status == 'downloaded' &&
convertedFormats.contains(video.format));
@ -127,15 +146,15 @@ class VideosCubit extends Cubit<VideosState> {
for (Video video in state.videoList.where((video) =>
video.status == 'downloaded' || video.status == 'converted')) {
var cleanTitle = video.title.replaceAll(RegExp(r'[^\w\s]+'), '');
final tmpFile =
File('temp/${video.filename}_done.${video.format.extension}');
final tmpFile = File(
'${tempDir.path}/${video.filename}_done.${video.format.extension}');
var playlistTitle = video.playlistTitle;
await Directory('${downloadDirectory.path}/$playlistTitle')
.create(recursive: true)
.catchError((e) => FileLogger().e('Error creating directory: $e'));
FileLogger().d('Playlist title: $playlistTitle');
if (playlistTitle.isNotEmpty && playlistTitle != '') {
await Directory('${downloadDirectory.path}/$playlistTitle')
.create(recursive: true)
.catchError((e) => FileLogger().e('Error creating directory: $e'));
FileLogger().d('Playlist title: $playlistTitle');
cleanTitle = '$playlistTitle/$cleanTitle';
}
@ -145,6 +164,7 @@ class VideosCubit extends Cubit<VideosState> {
var isMoved = false;
while (!isMoved) {
try {
FileLogger().d('Moving ${tmpFile.path} to ${newFile.path}');
await tmpFile.rename(newFile.path);
isMoved = true;
} on FileSystemException catch (e) {
@ -152,9 +172,10 @@ class VideosCubit extends Cubit<VideosState> {
await Future.delayed(Duration(seconds: 1));
}
}
FileLogger().d('File moved to ${newFile.path}');
await changeStatus(video, 'done');
await archiveVideo(video.copyWith(status: 'done'));
FileLogger().d('File moved to ${newFile.path}');
}
}
@ -216,20 +237,49 @@ class VideosCubit extends Cubit<VideosState> {
FileLogger().d(
'___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);
var completer = Completer<void>();
shellStream.listen(
(line) {
(line) async {
if (line.contains('[download]') &&
line.contains('of') &&
line.contains('%')) {
var percentString =
line.split('[download]')[1].split(' of')[0].split('%')[0].trim();
var percent = percentString == null ? 0 : double.parse(percentString);
changeStatus(video, 'downloading $percent%');
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%');
}
}
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]')) {
@ -242,7 +292,7 @@ class VideosCubit extends Cubit<VideosState> {
completer.complete();
}
}
//FileLogger().d(line);
FileLogger().d(line);
},
onError: (error) {
if (!completer.isCompleted) {