generateOutputPath function
Generates a file path for a downloaded video based on format and options.
format The format details of the video.
outputDir The directory to save the file in.
videoTitle The title of the video.
overrideName Optional custom filename to override the title.
overwrite Whether to overwrite existing files.
Returns the generated file path.
Implementation
String generateOutputPath(
Map<String, dynamic> format,
String outputDir,
String videoTitle,
String? overrideName,
bool overwrite,
) {
final baseName =
(overrideName ?? videoTitle).replaceAll(RegExp(r'[^\w\s-]'), '').trim();
String suffix;
String ext;
switch (format['type'] as String) {
case 'combined':
suffix = '${format['resolution']}_${format['bitrate']}kbps';
ext = format['needsConversion'] as bool &&
!(format['downloadAsRaw'] as bool? ?? true)
? 'mp4'
: format['ext'] as String;
break;
case 'merge':
final video = format['video'] as Map<String, dynamic>;
final audio = format['audio'] as Map<String, dynamic>;
suffix = '${video['resolution']}_${audio['bitrate']}kbps';
ext = 'mp4';
break;
case 'audio_only':
suffix = '${format['bitrate']}kbps';
ext = format['needsConversion'] as bool &&
!(format['downloadAsRaw'] as bool? ?? true)
? 'mp3'
: format['ext'] as String;
break;
default:
throw ArgumentError('Unknown format type: ${format['type']}');
}
String filePath = '$outputDir/${baseName}_$suffix.$ext';
if (!overwrite) {
filePath = _getUniqueFilePath(filePath);
}
PluginLogger.info('Generated path: $filePath');
return filePath;
}