From 0702774345db27b106cb310a1d4121029593faf5 Mon Sep 17 00:00:00 2001 From: jscampucci Date: Tue, 27 Aug 2024 16:41:28 +0200 Subject: [PATCH] [convert] clean conversion --- lib/constants.dart | 4 +- lib/models/video.dart | 5 ++ lib/services/converter.dart | 38 ++++++++++---- lib/services/file_logger.dart | 6 ++- lib/videoList/cubit/videos_cubit.dart | 74 ++++++++++++++++++++++----- 5 files changed, 102 insertions(+), 25 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index dfbec50..7e823eb 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -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( diff --git a/lib/models/video.dart b/lib/models/video.dart index 6f7c05a..84e6fde 100644 --- a/lib/models/video.dart +++ b/lib/models/video.dart @@ -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 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 ]; } diff --git a/lib/services/converter.dart b/lib/services/converter.dart index a833b2e..09434fe 100644 --- a/lib/services/converter.dart +++ b/lib/services/converter.dart @@ -24,6 +24,13 @@ class ConverterService { Future _init() async { tempDir = await getTemporaryDirectory(); + if (Platform.isWindows) { + ffmpegPath = 'ffmpeg.exe'; + } else if (Platform.isLinux) { + ffmpegPath = 'ffmpeg'; + } else if (Platform.isMacOS) { + ffmpegPath = 'ffmpeg'; + } } Future 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; } diff --git a/lib/services/file_logger.dart b/lib/services/file_logger.dart index c0689c9..c1c30c9 100644 --- a/lib/services/file_logger.dart +++ b/lib/services/file_logger.dart @@ -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 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); } diff --git a/lib/videoList/cubit/videos_cubit.dart b/lib/videoList/cubit/videos_cubit.dart index f1d8670..91d9c93 100644 --- a/lib/videoList/cubit/videos_cubit.dart +++ b/lib/videoList/cubit/videos_cubit.dart @@ -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 { late DLServices dlService; late ConverterService converterService; + late Directory tempDir; Iterable