| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2024 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <startconsole.h> |
| 14 | #include <joblist.h> |
| 15 | #include <utilities.h> |
| 16 | #include <config.h> |
| 17 | |
| 18 | #include <engine/converter.h> |
| 19 | |
| 20 | #include <jobs/engine/convert.h> |
| 21 | #include <jobs/joblist/addfiles.h> |
| 22 | #include <jobs/joblist/addtracks.h> |
| 23 | |
| 24 | #ifdef __WIN32__ |
| 25 | # include <windows.h> |
| 26 | #else |
| 27 | # include <unistd.h> |
| 28 | # include <signal.h> |
| 29 | # include <limits.h> |
| 30 | |
| 31 | # ifndef PATH_MAX |
| 32 | # define PATH_MAX 32768 |
| 33 | # endif |
| 34 | #endif |
| 35 | |
| 36 | using namespace smooth::IO; |
| 37 | |
| 38 | using namespace BoCA; |
| 39 | using namespace BoCA::AS; |
| 40 | |
| 41 | #ifdef __WIN32__ |
| 42 | static BOOL WINAPI ConsoleCtrlHandler(DWORD ctrlType) |
| 43 | { |
| 44 | if (ctrlType != CTRL_C_EVENT && ctrlType != CTRL_CLOSE_EVENT && |
| 45 | ctrlType != CTRL_LOGOFF_EVENT && ctrlType != CTRL_SHUTDOWN_EVENT) return FALSE; |
| 46 | |
| 47 | freac::freacCommandline::Get()->Stop(); |
| 48 | |
| 49 | return TRUE; |
| 50 | } |
| 51 | #else |
| 52 | static void ConsoleSignalHandler(int signal) |
| 53 | { |
| 54 | freac::freacCommandline::Get()->Stop(); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | Int StartConsole(const Array<String> &args) |
| 59 | { |
| 60 | #ifdef __WIN32__ |
| 61 | SetConsoleCtrlHandler(&ConsoleCtrlHandler, TRUE); |
| 62 | #else |
| 63 | signal(SIGINT, &ConsoleSignalHandler); |
| 64 | signal(SIGTERM, &ConsoleSignalHandler); |
| 65 | #endif |
| 66 | |
| 67 | freac::freacCommandline *application = freac::freacCommandline::Get(args); |
| 68 | Int errorCode = application->GetErrorCode(); |
| 69 | |
| 70 | freac::freacCommandline::Free(); |
| 71 | |
| 72 | return errorCode; |
| 73 | } |
| 74 | |
| 75 | freac::freacCommandline *freac::freacCommandline::Get(const Array<String> &args) |
| 76 | { |
| 77 | if (instance == NIL) new freacCommandline(args); |
| 78 | |
| 79 | return (freacCommandline *) instance; |
| 80 | } |
| 81 | |
| 82 | Void freac::freacCommandline::Free() |
| 83 | { |
| 84 | if (instance != NIL) delete (freacCommandline *) instance; |
| 85 | } |
| 86 | |
| 87 | freac::freacCommandline::freacCommandline(const Array<String> &arguments) : args(arguments), firstFile(True), stopped(False), errorCode(0) |
| 88 | { |
| 89 | Registry &boca = Registry::Get(); |
| 90 | |
| 91 | BoCA::Config *config = BoCA::Config::Get(); |
| 92 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 93 | |
| 94 | /* Don't save configuration settings set via command line. |
| 95 | */ |
| 96 | config->SetSaveSettingsOnExit(False); |
| 97 | |
| 98 | /* List configurations if requested. |
| 99 | */ |
| 100 | if (ScanForProgramOption("--list-configs")) |
| 101 | { |
| 102 | Console::OutputString(String(freac::appLongName).Append(" ").Append(freac::version).Append(" (").Append(freac::architecture).Append(") command line interface\n").Append(freac::copyright).Append("\n\n")); |
| 103 | Console::OutputString("Available configurations:\n\n"); |
| 104 | |
| 105 | for (Int i = 0; i < config->GetNOfConfigurations(); i++) |
| 106 | { |
| 107 | if (i == 0) Console::OutputString(String("\t").Append(i18n->TranslateString("Default configuration", "Configuration")).Append("\n")); |
| 108 | else Console::OutputString(String("\t").Append(config->GetNthConfigurationName(i)).Append("\n")); |
| 109 | } |
| 110 | |
| 111 | #ifndef __WIN32__ |
| 112 | Console::OutputString("\n"); |
| 113 | #endif |
| 114 | |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | /* Find configuration to use. |
| 119 | */ |
| 120 | String configName = "default"; |
| 121 | Bool userConfig = ScanForProgramOption("--config=%VALUE", &configName); |
| 122 | |
| 123 | if (configName == "Default configuration" || |
| 124 | configName == i18n->TranslateString("Default configuration", "Configuration")) configName = "default"; |
| 125 | |
| 126 | /* Check whether configuration exists. |
| 127 | */ |
| 128 | if (configName != "default") |
| 129 | { |
| 130 | Bool foundConfig = False; |
| 131 | |
| 132 | for (Int i = 1; i < config->GetNOfConfigurations() && !foundConfig; i++) |
| 133 | { |
| 134 | if (configName == config->GetNthConfigurationName(i)) foundConfig = True; |
| 135 | } |
| 136 | |
| 137 | if (!foundConfig) |
| 138 | { |
| 139 | Console::OutputString(String("Error: No such configuration: ").Append(configName).Append("\n")); |
| 140 | |
| 141 | errorCode = -2; |
| 142 | |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Activate configuration. |
| 148 | */ |
| 149 | config->SetActiveConfiguration(configName); |
| 150 | |
| 151 | SetConfigDefaults(config, userConfig); |
| 152 | |
| 153 | /* Activate English language. |
| 154 | */ |
| 155 | i18n->ActivateLanguage("internal"); |
| 156 | |
| 157 | /* Configure the converter. |
| 158 | */ |
| 159 | String helpenc; |
| 160 | |
| 161 | Array<String> files; |
| 162 | String pattern; |
| 163 | String outputFile; |
| 164 | String outputFolder = Directory::GetActiveDirectory(); |
| 165 | |
| 166 | Bool enableSuperFast = ScanForProgramOption("--superfast"); |
| 167 | String numberOfThreads; |
| 168 | |
| 169 | String cdDrive = "0"; |
| 170 | String tracks; |
| 171 | String ripperTimeout; |
| 172 | Bool enableCDDB = ScanForProgramOption("--cddb"); |
| 173 | |
| 174 | Bool splitChapters = ScanForProgramOption("--split-chapters"); |
| 175 | |
| 176 | Bool ignoreChapters = ScanForProgramOption("--ignore-chapters"); |
| 177 | Bool ignoreAlbumArt = ScanForProgramOption("--ignore-albumart") || ScanForProgramOption("--ignore-coverart"); |
| 178 | |
| 179 | String cueSheetFile; |
| 180 | String playlistFile; |
| 181 | |
| 182 | String playlistFormat = Config::PlaylistFormatDefault; |
| 183 | |
| 184 | encoderID = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 185 | encoderID = encoderID.Head(encoderID.Length() - 4); |
| 186 | |
| 187 | if (!ScanForProgramOption("--encoder=%VALUE", &encoderID)) ScanForProgramOption("-e %VALUE", &encoderID); |
| 188 | if (!ScanForProgramOption("--help=%VALUE", &helpenc)) ScanForProgramOption("-h %VALUE", &helpenc); |
| 189 | |
| 190 | ScanForProgramOption("-d %VALUE", &outputFolder); |
| 191 | ScanForProgramOption("-o %VALUE", &outputFile); |
| 192 | |
| 193 | if (outputFolder != NIL && BoCA::Utilities::IsRelativePath(outputFolder)) outputFolder = GetAbsolutePathName(outputFolder); |
| 194 | if (outputFile != NIL && BoCA::Utilities::IsRelativePath(outputFile)) outputFile = GetAbsolutePathName(outputFile); |
| 195 | |
| 196 | if (!ScanForProgramOption("--pattern=%VALUE", &pattern)) ScanForProgramOption("-p %VALUE", &pattern); |
| 197 | |
| 198 | ScanForProgramOption("--threads=%VALUE", &numberOfThreads); |
| 199 | |
| 200 | if (numberOfThreads != NIL) numberOfThreads = String::FromInt(numberOfThreads.ToInt()); |
| 201 | |
| 202 | ScanForProgramOption("--cuesheet=%VALUE", &cueSheetFile); |
| 203 | ScanForProgramOption("--playlist=%VALUE", &playlistFile); |
| 204 | |
| 205 | if (cueSheetFile == NIL && ScanForProgramOption("--cuesheet")) cueSheetFile = "<default>"; |
| 206 | if (playlistFile == NIL && ScanForProgramOption("--playlist")) playlistFile = "<default>"; |
| 207 | |
| 208 | encoderID = encoderID.ToLower(); |
| 209 | helpenc = helpenc.ToLower(); |
| 210 | |
| 211 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 212 | Int numDrives = 0; |
| 213 | |
| 214 | if (info != NIL) |
| 215 | { |
| 216 | numDrives = info->GetNumberOfDevices(); |
| 217 | |
| 218 | /* List drives if requested. |
| 219 | */ |
| 220 | if (ScanForProgramOption("--list-drives")) |
| 221 | { |
| 222 | Console::OutputString(String(freac::appLongName).Append(" ").Append(freac::version).Append(" (").Append(freac::architecture).Append(") command line interface\n").Append(freac::copyright).Append("\n\n")); |
| 223 | Console::OutputString("Available CD drives:\n\n"); |
| 224 | Console::OutputString("\tNumber\tDevice path\tDesignation\n"); |
| 225 | Console::OutputString("\t------------------------------------------------\n"); |
| 226 | |
| 227 | for (Int i = 0; i < numDrives; i++) |
| 228 | { |
| 229 | const Device &device = info->GetNthDeviceInfo(i); |
| 230 | |
| 231 | Console::OutputString(String("\t").Append(String::FromInt(i)).Append("\t").Append(device.path).Append(device.path.Length() < 8 ? "\t\t" : "\t").Append(device.GetName()).Append("\n")); |
| 232 | } |
| 233 | |
| 234 | #ifndef __WIN32__ |
| 235 | Console::OutputString("\n"); |
| 236 | #endif |
| 237 | |
| 238 | boca.DeleteComponent(info); |
| 239 | |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | /* Scan for CD ripping options. |
| 244 | */ |
| 245 | if (numDrives > 0) |
| 246 | { |
| 247 | if (!ScanForProgramOption("--drive=%VALUE", &cdDrive)) ScanForProgramOption("-cd %VALUE", &cdDrive); |
| 248 | if (!ScanForProgramOption("--track=%VALUE", &tracks)) ScanForProgramOption("-t %VALUE", &tracks); |
| 249 | |
| 250 | ScanForProgramOption("--timeout=%VALUE", &ripperTimeout); |
| 251 | |
| 252 | #ifndef __WIN32__ |
| 253 | /* Resolve links to devices. |
| 254 | */ |
| 255 | Buffer<char> buffer(PATH_MAX + 1); |
| 256 | |
| 257 | buffer.Zero(); |
| 258 | |
| 259 | while (readlink(cdDrive, buffer, buffer.Size() - 1) != -1) |
| 260 | { |
| 261 | if (buffer[0] == '/') cdDrive = buffer; |
| 262 | else cdDrive = File(cdDrive).GetFilePath().Append("/").Append(String(buffer)); |
| 263 | |
| 264 | buffer.Zero(); |
| 265 | } |
| 266 | |
| 267 | cdDrive = File(cdDrive); |
| 268 | #endif |
| 269 | |
| 270 | /* Find drive number for path. |
| 271 | */ |
| 272 | for (Int i = 0; i < numDrives; i++) |
| 273 | { |
| 274 | const Device &device = info->GetNthDeviceInfo(i); |
| 275 | |
| 276 | if (device.path != cdDrive) continue; |
| 277 | |
| 278 | cdDrive = String::FromInt(i); |
| 279 | |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | boca.DeleteComponent(info); |
| 285 | } |
| 286 | |
| 287 | ScanForFiles(&files); |
| 288 | |
| 289 | if (numDrives > 0) |
| 290 | { |
| 291 | config->SetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, cdDrive.ToInt()); |
| 292 | |
| 293 | if (config->GetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, Config::RipperActiveDriveDefault) >= numDrives) |
| 294 | { |
| 295 | Console::OutputString(String("Warning: Drive #").Append(cdDrive).Append(" does not exist. Using first drive.\n")); |
| 296 | |
| 297 | config->SetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, 0); |
| 298 | } |
| 299 | |
| 300 | if (!TracksToFiles(tracks, &files)) |
| 301 | { |
| 302 | Console::OutputString("Error: Invalid track(s) specified after --track.\n"); |
| 303 | |
| 304 | errorCode = -2; |
| 305 | |
| 306 | return; |
| 307 | } |
| 308 | } |
| 309 | else if (tracks != NIL || ScanForProgramOption("--list-drives")) |
| 310 | { |
| 311 | Console::OutputString("Error: CD ripping disabled!"); |
| 312 | |
| 313 | errorCode = -4; |
| 314 | |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | Console::SetTitle(String(freac::appName).Append(" ").Append(freac::version)); |
| 319 | |
| 320 | if ((files.Length() == 0 && !ScanForProgramOption("--eject")) || helpenc != NIL) |
| 321 | { |
| 322 | ShowHelp(helpenc); |
| 323 | |
| 324 | if (helpenc == NIL) errorCode = -2; |
| 325 | |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | if (!boca.ComponentExists(String(encoderID).Append("-enc"))) |
| 330 | { |
| 331 | Console::OutputString(String("Encoder '").Append(encoderID).Append("' is not supported by ").Append(freac::appName).Append("!\n")); |
| 332 | |
| 333 | errorCode = -2; |
| 334 | |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | /* Load album art files. |
| 339 | */ |
| 340 | String coverFrontFile; |
| 341 | String coverBackFile; |
| 342 | |
| 343 | ScanForProgramOption("--add-cover=%VALUE", &coverFrontFile); |
| 344 | ScanForProgramOption("--add-cover-back=%VALUE", &coverBackFile); |
| 345 | |
| 346 | if (coverFrontFile != NIL) |
| 347 | { |
| 348 | coverFront = LoadAlbumArt(coverFrontFile, 3); |
| 349 | |
| 350 | if (coverFront == Picture()) return; |
| 351 | } |
| 352 | |
| 353 | if (coverBackFile != NIL) |
| 354 | { |
| 355 | coverBack = LoadAlbumArt(coverBackFile, 4); |
| 356 | |
| 357 | if (coverBack == Picture()) return; |
| 358 | } |
| 359 | |
| 360 | /* Evaluate dynamic encoder parameters. |
| 361 | */ |
| 362 | if (!SetEncoderDefaults(config, userConfig)) return; |
| 363 | |
| 364 | /* Set config values. |
| 365 | */ |
| 366 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, String(encoderID).Append("-enc")); |
| 367 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderOutputDirectoryID, Directory(outputFolder)); |
| 368 | |
| 369 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsSingleFilenameID, outputFile); |
| 370 | |
| 371 | if (pattern != NIL) config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, pattern); |
| 372 | |
| 373 | if (enableSuperFast ) config->SetIntValue(Config::CategoryResourcesID, Config::ResourcesEnableSuperFastModeID, True); |
| 374 | if (numberOfThreads == "1") config->SetIntValue(Config::CategoryResourcesID, Config::ResourcesEnableParallelConversionsID, False); |
| 375 | if (numberOfThreads != NIL) config->SetIntValue(Config::CategoryResourcesID, Config::ResourcesNumberOfConversionThreadsID, numberOfThreads.ToInt()); |
| 376 | |
| 377 | if (enableCDDB ) config->SetIntValue(Config::CategoryFreedbID, Config::FreedbAutoQueryID, True); |
| 378 | |
| 379 | if (ripperTimeout != NIL) config->SetIntValue(Config::CategoryRipperID, Config::RipperTimeoutID, ripperTimeout.ToInt ()); |
| 380 | |
| 381 | if (cueSheetFile != NIL) |
| 382 | { |
| 383 | config->SetIntValue(Config::CategoryPlaylistID, Config::PlaylistCreateCueSheetID, True); |
| 384 | |
| 385 | if (cueSheetFile != "<default>") |
| 386 | { |
| 387 | if (!cueSheetFile.ToLower().EndsWith(".cue")) cueSheetFile.Append(".cue"); |
| 388 | |
| 389 | config->SetStringValue(Config::CategoryPlaylistID, Config::PlaylistCueSheetID, File(cueSheetFile)); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | cueSheetFile = String(Directory(outputFolder)).Append(Directory::GetDirectoryDelimiter()).Append("cuesheet.cue"); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (playlistFile != NIL) |
| 398 | { |
| 399 | config->SetIntValue(Config::CategoryPlaylistID, Config::PlaylistCreatePlaylistID, True); |
| 400 | |
| 401 | if (playlistFile != "<default>") |
| 402 | { |
| 403 | if (!playlistFile.Contains(".")) playlistFile.Append(".m3u8"); |
| 404 | |
| 405 | config->SetStringValue(Config::CategoryPlaylistID, Config::PlaylistFilenameID, File(playlistFile)); |
| 406 | |
| 407 | /* Set playlist format based on file extension. |
| 408 | */ |
| 409 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 410 | { |
| 411 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_PLAYLIST) continue; |
| 412 | |
| 413 | const Array<FileFormat *> &formats = boca.GetComponentFormats(i); |
| 414 | |
| 415 | foreach (FileFormat *format, formats) |
| 416 | { |
| 417 | const Array<String> &formatExtensions = format->GetExtensions(); |
| 418 | |
| 419 | foreach (const String &formatExtension, formatExtensions) |
| 420 | { |
| 421 | if (playlistFile.ToLower().EndsWith(String(".").Append(formatExtension))) playlistFormat = boca.GetComponentID(i).Append("-").Append(formatExtension); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | config->SetStringValue(Config::CategoryPlaylistID, Config::PlaylistFormatID, playlistFormat); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | playlistFile = String(Directory(outputFolder)).Append(Directory::GetDirectoryDelimiter()).Append("playlist.").Append(playlistFormat.Tail(playlistFormat.Length() - playlistFormat.FindLast("-") - 1)); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (ignoreChapters) |
| 435 | { |
| 436 | config->SetIntValue(Config::CategoryTagsID, Config::TagsReadChaptersID, False); |
| 437 | config->SetIntValue(Config::CategoryTagsID, Config::TagsWriteChaptersID, False); |
| 438 | |
| 439 | config->SetIntValue(Config::CategoryTagsID, Config::TagsReadEmbeddedCueSheetsID, False); |
| 440 | } |
| 441 | |
| 442 | if (ignoreAlbumArt) |
| 443 | { |
| 444 | config->SetIntValue(Config::CategoryTagsID, Config::TagsAlbumArtReadFromTagsID, False); |
| 445 | config->SetIntValue(Config::CategoryTagsID, Config::TagsAlbumArtWriteToTagsID, False); |
| 446 | } |
| 447 | |
| 448 | /* Perform actual conversion. |
| 449 | */ |
| 450 | JobConvert::onEncodeTrack.Connect(&freacCommandline::OnEncodeTrack, this); |
| 451 | JobConvert::onFinishEncoding.Connect(&freacCommandline::OnFinishEncoding, this); |
| 452 | |
| 453 | if (outputFile != NIL) |
| 454 | { |
| 455 | /* Check if input files exist. |
| 456 | */ |
| 457 | Array<String> jobFiles; |
| 458 | Bool addCDTracks = False; |
| 459 | |
| 460 | foreach (const String &file, files) |
| 461 | { |
| 462 | if (file.StartsWith("device://cdda:")) addCDTracks = True; |
| 463 | |
| 464 | InStream in(STREAM_FILE, file, IS_READ); |
| 465 | |
| 466 | if (in.GetLastError() != IO_ERROR_OK && !file.StartsWith("device://")) |
| 467 | { |
| 468 | Console::OutputString(String("File not found: ").Append(file).Append("\n")); |
| 469 | |
| 470 | errorCode = -3; |
| 471 | |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | jobFiles.Add(file); |
| 476 | } |
| 477 | |
| 478 | /* Add files to joblist. |
| 479 | */ |
| 480 | JobList *joblist = new JobList(Point(0, 0), Size(0, 0)); |
| 481 | |
| 482 | if (AddToJoblist(jobFiles, addCDTracks)) |
| 483 | { |
| 484 | /* Convert tracks in joblist. |
| 485 | */ |
| 486 | Converter().Convert(*joblist->GetTrackList(), False, False); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | Console::OutputString("Could not process input files!\n"); |
| 491 | |
| 492 | errorCode = -1; |
| 493 | } |
| 494 | |
| 495 | DeleteObject(joblist); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | Array<Track> playlistTracks; |
| 500 | |
| 501 | config->SetIntValue(Config::CategoryPlaylistID, Config::PlaylistCreatePlaylistID, False); |
| 502 | config->SetIntValue(Config::CategoryPlaylistID, Config::PlaylistCreateCueSheetID, False); |
| 503 | |
| 504 | JobList *joblist = new JobList(Point(0, 0), Size(0, 0)); |
| 505 | |
| 506 | foreach (const String &file, files) |
| 507 | { |
| 508 | String currentFile = file; |
| 509 | Bool addCDTrack = False; |
| 510 | |
| 511 | if (currentFile.StartsWith("device://cdda:")) |
| 512 | { |
| 513 | currentFile = String("Audio CD ").Append(String::FromInt(config->GetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, Config::RipperActiveDriveDefault))).Append(" - Track ").Append(currentFile.Tail(currentFile.Length() - 16)); |
| 514 | addCDTrack = True; |
| 515 | } |
| 516 | |
| 517 | /* Check if input file exists. |
| 518 | */ |
| 519 | InStream in(STREAM_FILE, file, IS_READ); |
| 520 | |
| 521 | if (in.GetLastError() != IO_ERROR_OK && !file.StartsWith("device://")) |
| 522 | { |
| 523 | Console::OutputString(String("File not found: ").Append(file).Append("\n")); |
| 524 | |
| 525 | errorCode = -3; |
| 526 | |
| 527 | continue; |
| 528 | } |
| 529 | |
| 530 | in.Close(); |
| 531 | |
| 532 | /* Handle splitting chapters and cue sheets. |
| 533 | */ |
| 534 | Bool splitFile = splitChapters || currentFile.EndsWith(".cue"); |
| 535 | String pattern = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, "<filename>"); |
| 536 | |
| 537 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, !splitFile); |
| 538 | |
| 539 | if (splitFile && pattern == "<filename>") config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, "<filename> - <track> - <title>"); |
| 540 | else config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, pattern); |
| 541 | |
| 542 | /* Add file to joblist. |
| 543 | */ |
| 544 | Array<String> jobFiles; |
| 545 | |
| 546 | jobFiles.Add(file); |
| 547 | |
| 548 | if (!AddToJoblist(jobFiles, addCDTrack)) |
| 549 | { |
| 550 | Console::OutputString(String("Could not process file: ").Append(currentFile).Append("\n")); |
| 551 | |
| 552 | errorCode = -1; |
| 553 | |
| 554 | continue; |
| 555 | } |
| 556 | |
| 557 | /* Set output file name. |
| 558 | */ |
| 559 | Track track = joblist->GetNthTrack(0); |
| 560 | |
| 561 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsSingleFilenameID, Utilities::GetOutputFileName(config, track)); |
| 562 | |
| 563 | /* Add tracks to playlist tracks. |
| 564 | */ |
| 565 | const Array<Track> *joblistTracks = joblist->GetTrackList(); |
| 566 | |
| 567 | foreach (Track playlistTrack, *joblistTracks) |
| 568 | { |
| 569 | playlistTrack.fileName = Utilities::GetOutputFileName(config, playlistTrack); |
| 570 | playlistTrack.isCDTrack = False; |
| 571 | |
| 572 | playlistTracks.Add(playlistTrack); |
| 573 | } |
| 574 | |
| 575 | /* Convert tracks in joblist. |
| 576 | */ |
| 577 | firstFile = True; |
| 578 | |
| 579 | Converter().Convert(*joblist->GetTrackList(), False, False); |
| 580 | |
| 581 | joblist->RemoveAllTracks(); |
| 582 | |
| 583 | if (stopped) break; |
| 584 | } |
| 585 | |
| 586 | /* Create playlist and cue sheet. |
| 587 | */ |
| 588 | if (!stopped && playlistTracks.Length() > 0 && playlistFile != NIL) |
| 589 | { |
| 590 | /* Write playlist. |
| 591 | */ |
| 592 | PlaylistComponent *playlist = (PlaylistComponent *) boca.CreateComponentByID(playlistFormat.Head(playlistFormat.FindLast("-"))); |
| 593 | |
| 594 | if (playlist != NIL) |
| 595 | { |
| 596 | playlist->SetTrackList(playlistTracks); |
| 597 | playlist->WritePlaylist(File(playlistFile)); |
| 598 | |
| 599 | boca.DeleteComponent(playlist); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!stopped && playlistTracks.Length() > 0 && cueSheetFile != NIL) |
| 604 | { |
| 605 | /* Write cue sheet. |
| 606 | */ |
| 607 | PlaylistComponent *cuesheet = (PlaylistComponent *) boca.CreateComponentByID("cuesheet-playlist"); |
| 608 | |
| 609 | if (cuesheet != NIL) |
| 610 | { |
| 611 | cuesheet->SetTrackList(playlistTracks); |
| 612 | cuesheet->WritePlaylist(File(cueSheetFile)); |
| 613 | |
| 614 | boca.DeleteComponent(cuesheet); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | DeleteObject(joblist); |
| 619 | } |
| 620 | |
| 621 | /* Eject disc if requested. |
| 622 | */ |
| 623 | if (ScanForProgramOption("--eject") && !stopped) |
| 624 | { |
| 625 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 626 | |
| 627 | if (info != NIL) |
| 628 | { |
| 629 | info->OpenNthDeviceTray(cdDrive.ToInt() < numDrives ? cdDrive.ToInt() : 0); |
| 630 | |
| 631 | boca.DeleteComponent(info); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* Wait for jobs to finish. |
| 636 | */ |
| 637 | const Array<Job *> &jobs = Job::GetRunningJobs(); |
| 638 | |
| 639 | while (jobs.Length() > 0) S::System::System::Sleep(10); |
| 640 | } |
| 641 | |
| 642 | freac::freacCommandline::~freacCommandline() |
| 643 | { |
| 644 | } |
| 645 | |
| 646 | Void freac::freacCommandline::OnEncodeTrack(const Track &track, const String &decoderName, const String &encoderName, ConversionStep mode) |
| 647 | { |
| 648 | BoCA::Config *config = BoCA::Config::Get(); |
| 649 | Bool quiet = ScanForProgramOption("--quiet"); |
| 650 | |
| 651 | if (!quiet) |
| 652 | { |
| 653 | static String previousFile; |
| 654 | String currentFile = track.fileName; |
| 655 | |
| 656 | if (currentFile == previousFile) return; |
| 657 | else previousFile = currentFile; |
| 658 | |
| 659 | if (currentFile.StartsWith("device://cdda:")) currentFile = String("Audio CD ").Append(String::FromInt(config->GetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, Config::RipperActiveDriveDefault))).Append(" - Track ").Append(currentFile.Tail(currentFile.Length() - 16)); |
| 660 | |
| 661 | if (!firstFile) Console::OutputString("done.\n"); |
| 662 | else firstFile = False; |
| 663 | |
| 664 | Console::OutputString(String("Processing file: ").Append(currentFile).Append("...")); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | Void freac::freacCommandline::OnFinishEncoding(Bool success) |
| 669 | { |
| 670 | Bool quiet = ScanForProgramOption("--quiet"); |
| 671 | |
| 672 | if (!quiet) |
| 673 | { |
| 674 | if (success) Console::OutputString("done.\n"); |
| 675 | else Console::OutputString("aborted.\n"); |
| 676 | } |
| 677 | |
| 678 | if (!success) errorCode = -1; |
| 679 | } |
| 680 | |
| 681 | Bool freac::freacCommandline::ScanForProgramOption(const String &option, String *value) |
| 682 | { |
| 683 | /* Scan all arguments. |
| 684 | */ |
| 685 | foreach (const String &arg, args) |
| 686 | { |
| 687 | /* Stop upon encountering separator. |
| 688 | */ |
| 689 | if (arg == "--") break; |
| 690 | |
| 691 | if (option.StartsWith("-") && option.EndsWith("%VALUE") && !option.Contains(" ") && value != NIL && arg.StartsWith(option.Head(option.Find("%")))) |
| 692 | { |
| 693 | *value = arg.Tail(arg.Length() - option.Length() + 6); |
| 694 | |
| 695 | return True; |
| 696 | } |
| 697 | else if (option.StartsWith("-") && option.EndsWith(" %VALUE") && value != NIL && arg == option.Head(option.Find(" "))) |
| 698 | { |
| 699 | *value = args.GetNth(foreachindex + 1); |
| 700 | |
| 701 | return True; |
| 702 | } |
| 703 | else if (option.StartsWith("-") && arg == option) |
| 704 | { |
| 705 | return True; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | return False; |
| 710 | } |
| 711 | |
| 712 | Bool freac::freacCommandline::ScanForEncoderOption(const String &option, String *value) |
| 713 | { |
| 714 | /* Check if we have a separator for program and encoder options. |
| 715 | */ |
| 716 | Int separatorindex = -1; |
| 717 | |
| 718 | foreach (const String &arg, args) |
| 719 | { |
| 720 | if (arg != "--") continue; |
| 721 | |
| 722 | separatorindex = foreachindex; |
| 723 | |
| 724 | break; |
| 725 | } |
| 726 | |
| 727 | /* Scan all arguments. |
| 728 | */ |
| 729 | foreach (const String &arg, args) |
| 730 | { |
| 731 | /* Discard arguments before separator. |
| 732 | */ |
| 733 | if (foreachindex <= separatorindex) continue; |
| 734 | |
| 735 | if (option.StartsWith("-") && option.EndsWith("%VALUE") && !option.Contains(" ") && value != NIL && arg.StartsWith(option.Head(option.Find("%")))) |
| 736 | { |
| 737 | *value = arg.Tail(arg.Length() - option.Length() + 6); |
| 738 | |
| 739 | return True; |
| 740 | } |
| 741 | else if (option.StartsWith("-") && option.EndsWith(" %VALUE") && value != NIL && arg == option.Head(option.Find(" "))) |
| 742 | { |
| 743 | *value = args.GetNth(foreachindex + 1); |
| 744 | |
| 745 | return True; |
| 746 | } |
| 747 | else if (option.StartsWith("-") && arg == option) |
| 748 | { |
| 749 | return True; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | return False; |
| 754 | } |
| 755 | |
| 756 | Void freac::freacCommandline::ScanForFiles(Array<String> *files) |
| 757 | { |
| 758 | Bool encoderOptionsOnly = False; |
| 759 | |
| 760 | foreach (const String &arg, args) |
| 761 | { |
| 762 | /* Skip non-file arguments. |
| 763 | */ |
| 764 | if (arg == "--") encoderOptionsOnly = True; |
| 765 | |
| 766 | if (arg.StartsWith("-")) |
| 767 | { |
| 768 | if (ParamHasArguments(arg, encoderOptionsOnly)) foreachindex++; |
| 769 | |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | /* Add file argument to list. |
| 774 | */ |
| 775 | if (arg.Contains("*") || arg.Contains("?")) |
| 776 | { |
| 777 | File file(arg); |
| 778 | Directory dir(file.GetFilePath()); |
| 779 | const Array<File> &array = dir.GetFilesByPattern(file.GetFileName()); |
| 780 | |
| 781 | foreach (const File &entry, array) (*files).Add(entry); |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | (*files).Add(arg); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | Bool freac::freacCommandline::ParamHasArguments(const String &arg, Bool encoderOptionsOnly) |
| 791 | { |
| 792 | if (!encoderOptionsOnly && (arg == "-e" || arg == "-h" || arg == "-d" || arg == "-o" || arg == "-p" || arg == "-cd" || arg == "-t")) return True; |
| 793 | |
| 794 | Registry &boca = Registry::Get(); |
| 795 | Component *component = boca.CreateComponentByID(String(encoderID).Append("-enc")); |
| 796 | |
| 797 | if (component == NIL) return False; |
| 798 | |
| 799 | /* Check dynamic encoder parameters. |
| 800 | */ |
| 801 | Bool hasArgs = False; |
| 802 | const Array<Parameter *> ¶meters = component->GetParameters(); |
| 803 | |
| 804 | foreach (Parameter *parameter, parameters) |
| 805 | { |
| 806 | if (parameter->GetHidden()) continue; |
| 807 | |
| 808 | ParameterType type = parameter->GetType(); |
| 809 | String spec = parameter->GetArgument(); |
| 810 | const Array<Option *> &options = parameter->GetOptions(); |
| 811 | |
| 812 | if (type == PARAMETER_TYPE_SWITCH) |
| 813 | { |
| 814 | if (spec == arg) break; |
| 815 | } |
| 816 | else if (type == PARAMETER_TYPE_SELECTION) |
| 817 | { |
| 818 | if (spec.StartsWith(arg.Append(" "))) { hasArgs = True; break; } |
| 819 | if (spec == arg.Append("%VALUE")) break; |
| 820 | |
| 821 | if (spec == "%VALUE") |
| 822 | { |
| 823 | Bool found = False; |
| 824 | |
| 825 | foreach (Option *option, options) if (option->GetValue() == arg) found = True; |
| 826 | |
| 827 | if (found) break; |
| 828 | } |
| 829 | } |
| 830 | else if (type == PARAMETER_TYPE_RANGE) |
| 831 | { |
| 832 | if (spec.StartsWith(arg.Append(" "))) { hasArgs = True; break; } |
| 833 | if (spec == arg.Append("%VALUE")) break; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | boca.DeleteComponent(component); |
| 838 | |
| 839 | return hasArgs; |
| 840 | } |
| 841 | |
| 842 | Bool freac::freacCommandline::TracksToFiles(const String &tracks, Array<String> *files) |
| 843 | { |
| 844 | BoCA::Config *config = BoCA::Config::Get(); |
| 845 | |
| 846 | if (tracks == "all") |
| 847 | { |
| 848 | Registry &boca = Registry::Get(); |
| 849 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 850 | |
| 851 | if (info != NIL) |
| 852 | { |
| 853 | const Array<String> &tracks = info->GetNthDeviceTrackList(config->GetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, Config::RipperActiveDriveDefault)); |
| 854 | |
| 855 | foreach (const String &track, tracks) (*files).Add(track); |
| 856 | |
| 857 | boca.DeleteComponent(info); |
| 858 | } |
| 859 | |
| 860 | return True; |
| 861 | } |
| 862 | |
| 863 | for (Int i = 0; i < tracks.Length(); i++) |
| 864 | { |
| 865 | if ((tracks[i] >= 'a' && tracks[i] <= 'z') || |
| 866 | (tracks[i] >= 'A' && tracks[i] <= 'Z')) return False; |
| 867 | } |
| 868 | |
| 869 | String rest = tracks; |
| 870 | |
| 871 | while (rest.Length() > 0) |
| 872 | { |
| 873 | String current; |
| 874 | |
| 875 | if (rest.Contains(",")) |
| 876 | { |
| 877 | Int comma = rest.Find(","); |
| 878 | |
| 879 | current = rest.Head(comma); |
| 880 | rest = rest.Tail(rest.Length() - comma - 1); |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | current = rest; |
| 885 | rest = NIL; |
| 886 | } |
| 887 | |
| 888 | if (current.Contains("-")) |
| 889 | { |
| 890 | Int dash = current.Find("-"); |
| 891 | Int first = current.Head(dash).ToInt(); |
| 892 | Int last = current.Tail(current.Length() - dash - 1).ToInt(); |
| 893 | |
| 894 | for (Int i = first; i <= last; i++) (*files).Add(String("device://cdda:") |
| 895 | .Append(String::FromInt(config->GetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, Config::RipperActiveDriveDefault))) |
| 896 | .Append("/") |
| 897 | .Append(String::FromInt(i))); |
| 898 | } |
| 899 | else |
| 900 | { |
| 901 | (*files).Add(String("device://cdda:") |
| 902 | .Append(String::FromInt(config->GetIntValue(Config::CategoryRipperID, Config::RipperActiveDriveID, Config::RipperActiveDriveDefault))) |
| 903 | .Append("/") |
| 904 | .Append(current)); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | return True; |
| 909 | } |
| 910 | |
| 911 | String freac::freacCommandline::GetAbsolutePathName(const String &path) const |
| 912 | { |
| 913 | String pathName = path; |
| 914 | |
| 915 | /* Convert relative to absolute paths. |
| 916 | */ |
| 917 | if (BoCA::Utilities::IsRelativePath(pathName)) pathName = String(Directory::GetActiveDirectory()).Append(Directory::GetDirectoryDelimiter()).Append(pathName); |
| 918 | |
| 919 | if (!pathName.EndsWith(Directory::GetDirectoryDelimiter())) pathName.Append(Directory::GetDirectoryDelimiter()); |
| 920 | |
| 921 | return pathName; |
| 922 | } |
| 923 | |
| 924 | Picture freac::freacCommandline::LoadAlbumArt(const String &file, Int type) |
| 925 | { |
| 926 | if (!File(file).Exists()) |
| 927 | { |
| 928 | Console::OutputString(String("Album art file not found: ").Append(file).Append("\n")); |
| 929 | |
| 930 | errorCode = -3; |
| 931 | |
| 932 | return Picture(); |
| 933 | } |
| 934 | |
| 935 | Picture picture; |
| 936 | |
| 937 | picture.LoadFromFile(file); |
| 938 | |
| 939 | if (picture.GetBitmap() == NIL) |
| 940 | { |
| 941 | Console::OutputString(String("Invalid album art file format: ").Append(file).Append("\n")); |
| 942 | |
| 943 | errorCode = -1; |
| 944 | |
| 945 | return Picture(); |
| 946 | } |
| 947 | |
| 948 | picture.type = type; |
| 949 | |
| 950 | return picture; |
| 951 | } |
| 952 | |
| 953 | Bool freac::freacCommandline::AddToJoblist(const Array<String> &files, Bool cdTracks) |
| 954 | { |
| 955 | /* Schedule job to add tracks to joblist. |
| 956 | */ |
| 957 | Job *job = cdTracks ? (Job *) new JobAddTracks(files) : (Job *) new JobAddFiles(files); |
| 958 | |
| 959 | job->Schedule(); |
| 960 | |
| 961 | while (Job::GetScheduledJobs().Length() > 0) S::System::System::Sleep(10); |
| 962 | while (Job::GetPlannedJobs().Length() > 0) S::System::System::Sleep(10); |
| 963 | while (Job::GetRunningJobs().Length() > 0) S::System::System::Sleep(10); |
| 964 | |
| 965 | /* Check track list. |
| 966 | */ |
| 967 | BoCA::JobList *joblist = BoCA::JobList::Get(); |
| 968 | const Array<Track> *tracks = joblist->getTrackList.Call(); |
| 969 | |
| 970 | if (tracks->Length() == 0) return False; |
| 971 | |
| 972 | /* Add album art. |
| 973 | */ |
| 974 | if (coverFront != Picture() || coverBack != Picture()) |
| 975 | { |
| 976 | foreach (Track track, *tracks) |
| 977 | { |
| 978 | if (coverFront != Picture()) track.pictures.Add(coverFront); |
| 979 | if (coverBack != Picture()) track.pictures.Add(coverBack); |
| 980 | |
| 981 | joblist->onComponentModifyTrack.Emit(track); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return True; |
| 986 | } |
| 987 | |
| 988 | Void freac::freacCommandline::Stop() |
| 989 | { |
| 990 | stopped = True; |
| 991 | |
| 992 | JobConvert::Stop(); |
| 993 | } |
| 994 | |
| 995 | Bool freac::freacCommandline::SetConfigDefaults(BoCA::Config *config, Bool userConfig) |
| 996 | { |
| 997 | /* Set user specified config flag. |
| 998 | */ |
| 999 | config->SetIntValue(Config::CategorySettingsID, "UserSpecifiedConfig", userConfig); |
| 1000 | |
| 1001 | /* Make mandatory config adjustments. |
| 1002 | */ |
| 1003 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, True); |
| 1004 | |
| 1005 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, True); |
| 1006 | |
| 1007 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsEncodeOnTheFlyID, True); |
| 1008 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsDeleteAfterEncodingID, False); |
| 1009 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsAddEncodedTracksID, False); |
| 1010 | |
| 1011 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, False); |
| 1012 | |
| 1013 | config->SetIntValue(Config::CategoryVerificationID, Config::VerificationVerifyInputID, False); |
| 1014 | config->SetIntValue(Config::CategoryVerificationID, Config::VerificationVerifyOutputID, False); |
| 1015 | |
| 1016 | config->SetIntValue(Config::CategoryResourcesID, Config::ResourcesEnableParallelConversionsID, True); |
| 1017 | |
| 1018 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbAutoSelectID, True); |
| 1019 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbEnableCacheID, True); |
| 1020 | |
| 1021 | config->SetIntValue(Config::CategoryRipperID, Config::RipperEjectAfterRippingID, False); |
| 1022 | |
| 1023 | config->SetIntValue(Config::CategoryTagsID, Config::TagsPreferCueSheetsToChaptersID, True); |
| 1024 | |
| 1025 | config->SetIntValue(Config::CategoryTagsID, Config::TagsAlbumArtReadFromFilesID, False); |
| 1026 | config->SetIntValue(Config::CategoryTagsID, Config::TagsAlbumArtWriteToFilesID, False); |
| 1027 | |
| 1028 | config->SetIntValue(Config::CategoryPlaylistID, Config::PlaylistCreatePlaylistID, False); |
| 1029 | config->SetIntValue(Config::CategoryPlaylistID, Config::PlaylistCreateCueSheetID, False); |
| 1030 | |
| 1031 | /* Don't set defaults for user-provided configs. |
| 1032 | */ |
| 1033 | if (userConfig) return True; |
| 1034 | |
| 1035 | /* Set configurable values to defaults. |
| 1036 | */ |
| 1037 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, "lame-enc"); |
| 1038 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, "<filename>"); |
| 1039 | |
| 1040 | config->SetIntValue(Config::CategoryProcessingID, Config::ProcessingEnableProcessingID, False); |
| 1041 | |
| 1042 | config->SetIntValue(Config::CategoryResourcesID, Config::ResourcesEnableSuperFastModeID, False); |
| 1043 | config->SetIntValue(Config::CategoryResourcesID, Config::ResourcesNumberOfConversionThreadsID, 0); |
| 1044 | |
| 1045 | config->SetIntValue(Config::CategoryFreedbID, Config::FreedbAutoQueryID, False); |
| 1046 | |
| 1047 | config->SetIntValue(Config::CategoryRipperID, Config::RipperTimeoutID, 0); |
| 1048 | |
| 1049 | config->SetIntValue(Config::CategoryTagsID, Config::TagsReadChaptersID, True); |
| 1050 | config->SetIntValue(Config::CategoryTagsID, Config::TagsWriteChaptersID, True); |
| 1051 | |
| 1052 | config->SetIntValue(Config::CategoryTagsID, Config::TagsReadEmbeddedCueSheetsID, True); |
| 1053 | |
| 1054 | config->SetIntValue(Config::CategoryTagsID, Config::TagsAlbumArtReadFromTagsID, True); |
| 1055 | config->SetIntValue(Config::CategoryTagsID, Config::TagsAlbumArtWriteToTagsID, True); |
| 1056 | |
| 1057 | return True; |
| 1058 | } |
| 1059 | |
| 1060 | Bool freac::freacCommandline::SetEncoderDefaults(BoCA::Config *config, Bool userConfig) |
| 1061 | { |
| 1062 | /* Check dynamic encoder parameters. |
| 1063 | */ |
| 1064 | Registry &boca = Registry::Get(); |
| 1065 | Component *component = boca.CreateComponentByID(String(encoderID).Append("-enc")); |
| 1066 | |
| 1067 | if (component == NIL) |
| 1068 | { |
| 1069 | Console::OutputString(String("Encoder '").Append(encoderID).Append("' could not be initialized!\n")); |
| 1070 | |
| 1071 | errorCode = -1; |
| 1072 | |
| 1073 | return False; |
| 1074 | } |
| 1075 | |
| 1076 | Bool broken = False; |
| 1077 | const Array<Parameter *> ¶meters = component->GetParameters(); |
| 1078 | |
| 1079 | foreach (Parameter *parameter, parameters) |
| 1080 | { |
| 1081 | if (parameter->GetHidden()) continue; |
| 1082 | |
| 1083 | ParameterType type = parameter->GetType(); |
| 1084 | String name = parameter->GetName(); |
| 1085 | String spec = parameter->GetArgument(); |
| 1086 | const Array<Option *> &options = parameter->GetOptions(); |
| 1087 | Float step = parameter->GetStepSize(); |
| 1088 | String def = parameter->GetDefault(); |
| 1089 | |
| 1090 | if (type == PARAMETER_TYPE_SWITCH) |
| 1091 | { |
| 1092 | /* Enable switch if given on command line. |
| 1093 | */ |
| 1094 | Bool enabled = ScanForEncoderOption(spec); |
| 1095 | |
| 1096 | if (!userConfig || enabled) config->SetIntValue(component->GetID(), name, enabled); |
| 1097 | } |
| 1098 | else if (type == PARAMETER_TYPE_SELECTION) |
| 1099 | { |
| 1100 | /* Get selection value. |
| 1101 | */ |
| 1102 | String value; |
| 1103 | Bool present = ScanForEncoderOption(String(spec).Trim(), &value); |
| 1104 | |
| 1105 | /* Check if given value is allowed. |
| 1106 | */ |
| 1107 | if (present) |
| 1108 | { |
| 1109 | Bool found = False; |
| 1110 | |
| 1111 | /* Check case sensitive. |
| 1112 | */ |
| 1113 | foreach (Option *option, options) |
| 1114 | { |
| 1115 | if (found || option->GetValue() != value) continue; |
| 1116 | |
| 1117 | found = True; |
| 1118 | } |
| 1119 | |
| 1120 | /* Check ignoring case. |
| 1121 | */ |
| 1122 | value = value.ToLower(); |
| 1123 | |
| 1124 | foreach (Option *option, options) |
| 1125 | { |
| 1126 | if (found || option->GetValue().ToLower() != value) continue; |
| 1127 | |
| 1128 | found = True; |
| 1129 | value = option->GetValue(); |
| 1130 | } |
| 1131 | |
| 1132 | if (!found) broken = True; |
| 1133 | } |
| 1134 | |
| 1135 | /* Set selection to given value. |
| 1136 | */ |
| 1137 | if (!userConfig || present) |
| 1138 | { |
| 1139 | config->SetIntValue(component->GetID(), String("Set ").Append(name), present); |
| 1140 | config->SetStringValue(component->GetID(), name, value); |
| 1141 | } |
| 1142 | } |
| 1143 | else if (type == PARAMETER_TYPE_RANGE) |
| 1144 | { |
| 1145 | /* Set range parameter to given value. |
| 1146 | */ |
| 1147 | String value; |
| 1148 | Bool present = ScanForEncoderOption(String(spec).Trim(), &value); |
| 1149 | |
| 1150 | if (!userConfig || present) |
| 1151 | { |
| 1152 | config->SetIntValue(component->GetID(), String("Set ").Append(name), present); |
| 1153 | config->SetIntValue(component->GetID(), name, Math::Round(value.ToFloat() / step)); |
| 1154 | } |
| 1155 | |
| 1156 | /* Check if given value is valid. |
| 1157 | */ |
| 1158 | Bool valid = True; |
| 1159 | |
| 1160 | foreach (Option *option, options) if (option->GetType() == OPTION_TYPE_MIN && value.ToFloat() < option->GetValue().ToFloat()) valid = False; |
| 1161 | foreach (Option *option, options) if (option->GetType() == OPTION_TYPE_MAX && value.ToFloat() > option->GetValue().ToFloat()) valid = False; |
| 1162 | |
| 1163 | if (present && !valid) broken = True; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | boca.DeleteComponent(component); |
| 1168 | |
| 1169 | if (broken) |
| 1170 | { |
| 1171 | Console::OutputString(String("Invalid arguments for encoder '").Append(encoderID).Append("'!\n")); |
| 1172 | |
| 1173 | errorCode = -2; |
| 1174 | } |
| 1175 | |
| 1176 | return !broken; |
| 1177 | } |
| 1178 | |
| 1179 | Void freac::freacCommandline::ShowHelp(const String &helpenc) |
| 1180 | { |
| 1181 | Console::OutputString(String(freac::appLongName).Append(" ").Append(freac::version).Append(" (").Append(freac::architecture).Append(") command line interface\n").Append(freac::copyright).Append("\n\n")); |
| 1182 | |
| 1183 | Registry &boca = Registry::Get(); |
| 1184 | |
| 1185 | if (helpenc == NIL) |
| 1186 | { |
| 1187 | Console::OutputString("Usage:\tfreaccmd [options] [--] [encoder options] [file(s)]\n\n"); |
| 1188 | Console::OutputString(" --encoder=<id> | -e <id>\tSpecify the encoder to use (default is LAME)\n"); |
| 1189 | Console::OutputString(" --help=<id> | -h <id>\tPrint help for encoder specific options\n\n"); |
| 1190 | Console::OutputString(" -d <dir>\tSpecify output directory for encoded files\n"); |
| 1191 | Console::OutputString(" -o <file>\tSpecify output file name in single file mode\n"); |
| 1192 | Console::OutputString(" --pattern=<pat> | -p <pat>\tSpecify output file name pattern (default is \"<filename>\")\n\n"); |
| 1193 | |
| 1194 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 1195 | |
| 1196 | if (info != NIL) |
| 1197 | { |
| 1198 | if (info->GetNumberOfDevices() > 0) |
| 1199 | { |
| 1200 | Console::OutputString(" --list-drives\t\t\tPrint a list of available CD drives\n\n"); |
| 1201 | Console::OutputString(" --drive=<n|id> | -cd <n|id>\tSpecify active CD drive (0..n or device path)\n"); |
| 1202 | Console::OutputString(" --track=<n> | -t <n>\tSpecify input track(s) to rip (e.g. 1-5,7,9 or 'all')\n"); |
| 1203 | Console::OutputString(" --timeout=<s>\t\t\tTimeout for CD track ripping (disabled by default)\n"); |
| 1204 | Console::OutputString(" --cddb\t\t\tEnable CDDB database lookup\n"); |
| 1205 | Console::OutputString(" --eject\t\t\tEject disc after ripping\n\n"); |
| 1206 | } |
| 1207 | |
| 1208 | boca.DeleteComponent(info); |
| 1209 | } |
| 1210 | |
| 1211 | Console::OutputString(" --superfast\t\t\tEnable SuperFast mode\n"); |
| 1212 | Console::OutputString(" --threads=<n>\t\t\tSpecify number of threads to use in SuperFast mode\n\n"); |
| 1213 | |
| 1214 | Console::OutputString(" --split-chapters\t\tSplit to individual chapters if chapter tags are present\n\n"); |
| 1215 | |
| 1216 | Console::OutputString(" --add-cover=<file>\t\tAdd front cover image from file (.png or .jpg)\n"); |
| 1217 | Console::OutputString(" --add-cover-back=<file>\tAdd back cover image from file (.png or .jpg)\n\n"); |
| 1218 | |
| 1219 | if (boca.GetNumberOfComponentsOfType(COMPONENT_TYPE_PLAYLIST) > 0) |
| 1220 | { |
| 1221 | if ( boca.ComponentExists("cuesheet-playlist")) Console::OutputString(" --cuesheet[=<file>]\t\tCreate a cue sheet of the conversion results\n"); |
| 1222 | if (boca.GetNumberOfComponentsOfType(COMPONENT_TYPE_PLAYLIST) > 1 || !boca.ComponentExists("cuesheet-playlist")) Console::OutputString(" --playlist[=<file>]\t\tCreate a playlist containing the conversion results\n"); |
| 1223 | |
| 1224 | Console::OutputString("\n"); |
| 1225 | } |
| 1226 | |
| 1227 | Console::OutputString(" --ignore-chapters\t\tDo not write chapter information to tags\n"); |
| 1228 | Console::OutputString(" --ignore-albumart\t\tDo not write album images to tags\n\n"); |
| 1229 | |
| 1230 | Console::OutputString(" --list-configs\t\tPrint a list of available configurations\n"); |
| 1231 | Console::OutputString(" --config=<cfg>\t\tSpecify configuration to use\n\n"); |
| 1232 | |
| 1233 | Console::OutputString(" --quiet\t\t\tDo not print any messages\n\n"); |
| 1234 | |
| 1235 | Console::OutputString("Use -- to separate freaccmd options from encoder options if both have the same name.\n\n"); |
| 1236 | Console::OutputString("Encoder <id> can be one of:\n\n"); |
| 1237 | |
| 1238 | String list; |
| 1239 | Bool first = True; |
| 1240 | |
| 1241 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 1242 | { |
| 1243 | if (boca.GetComponentType(i) != COMPONENT_TYPE_ENCODER) continue; |
| 1244 | |
| 1245 | if (list.Length() + boca.GetComponentID(i).Length() > 80) |
| 1246 | { |
| 1247 | Console::OutputString(list.Append(",\n")); |
| 1248 | |
| 1249 | list = NIL; |
| 1250 | first = True; |
| 1251 | } |
| 1252 | |
| 1253 | if (first) list.Append("\t"); |
| 1254 | else list.Append(", "); |
| 1255 | |
| 1256 | first = False; |
| 1257 | |
| 1258 | list.Append(boca.GetComponentID(i).Head(boca.GetComponentID(i).FindLast("-enc"))); |
| 1259 | } |
| 1260 | |
| 1261 | Console::OutputString(list.Append("\n")); |
| 1262 | } |
| 1263 | else |
| 1264 | { |
| 1265 | if (!boca.ComponentExists(String(helpenc).Append("-enc"))) |
| 1266 | { |
| 1267 | Console::OutputString(String("Encoder '").Append(helpenc).Append("' is not supported by ").Append(freac::appName).Append("!\n")); |
| 1268 | |
| 1269 | errorCode = -2; |
| 1270 | |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | Component *component = boca.CreateComponentByID(String(helpenc).Append("-enc")); |
| 1275 | |
| 1276 | if (component == NIL) |
| 1277 | { |
| 1278 | Console::OutputString(String("Encoder '").Append(helpenc).Append("' could not be initialized!\n")); |
| 1279 | |
| 1280 | errorCode = -1; |
| 1281 | |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | Console::OutputString(String("Options for ").Append(component->GetName()).Append(":\n\n")); |
| 1286 | |
| 1287 | if (component->GetParameters().Length() > 0) |
| 1288 | { |
| 1289 | const Array<Parameter *> ¶meters = component->GetParameters(); |
| 1290 | |
| 1291 | /* Get required number of tabs for formatting. |
| 1292 | */ |
| 1293 | Int maxTabs = 0; |
| 1294 | |
| 1295 | foreach (Parameter *parameter, parameters) |
| 1296 | { |
| 1297 | if (parameter->GetHidden()) continue; |
| 1298 | |
| 1299 | String spec = parameter->GetArgument().Replace("%VALUE", "<val>"); |
| 1300 | |
| 1301 | maxTabs = Math::Max(maxTabs, Math::Ceil((spec.Length() + 1) / 8.0)); |
| 1302 | } |
| 1303 | |
| 1304 | /* Print formatted parameter list. |
| 1305 | */ |
| 1306 | foreach (Parameter *parameter, parameters) |
| 1307 | { |
| 1308 | if (parameter->GetHidden()) continue; |
| 1309 | |
| 1310 | ParameterType type = parameter->GetType(); |
| 1311 | String name = parameter->GetName(); |
| 1312 | String spec = parameter->GetArgument().Replace("%VALUE", "<val>"); |
| 1313 | const Array<Option *> &options = parameter->GetOptions(); |
| 1314 | String def = parameter->GetDefault(); |
| 1315 | |
| 1316 | if (type == PARAMETER_TYPE_SWITCH) |
| 1317 | { |
| 1318 | Console::OutputString(String("\t").Append(spec).Append(String().FillN('\t', maxTabs - Math::Floor(spec.Length() / 8.0))).Append(name).Append("\n")); |
| 1319 | } |
| 1320 | else if (type == PARAMETER_TYPE_SELECTION) |
| 1321 | { |
| 1322 | Console::OutputString(String("\t").Append(spec).Append(String().FillN('\t', maxTabs - Math::Floor(spec.Length() / 8.0))).Append(name).Append(": ")); |
| 1323 | |
| 1324 | { |
| 1325 | foreach (Option *option, options) |
| 1326 | { |
| 1327 | if (foreachindex > 0) Console::OutputString(String().FillN('\t', maxTabs + 1).Append(String().FillN(' ', name.Length() + 2))); |
| 1328 | |
| 1329 | Console::OutputString(option->GetValue().Append(option->GetAlias() != option->GetValue() ? String(" (").Append(option->GetAlias().Replace("%1", option->GetValue())).Append(")") : String()).Append(def == option->GetValue() ? ", default" : NIL).Append("\n")); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | if (foreachindex < parameters.Length() - 1) Console::OutputString("\n"); |
| 1334 | } |
| 1335 | else if (type == PARAMETER_TYPE_RANGE) |
| 1336 | { |
| 1337 | Console::OutputString(String("\t").Append(spec).Append(String().FillN('\t', maxTabs - Math::Floor(spec.Length() / 8.0))).Append(name).Append(": ")); |
| 1338 | |
| 1339 | foreach (Option *option, options) if (option->GetType() == OPTION_TYPE_MIN) Console::OutputString(String(option->GetValue()).Append(option->GetAlias() != option->GetValue() ? String(" (").Append(option->GetAlias().Replace("%1", option->GetValue())).Append(")") : String()).Append(" - ")); |
| 1340 | foreach (Option *option, options) if (option->GetType() == OPTION_TYPE_MAX) Console::OutputString(String(option->GetValue()).Append(option->GetAlias() != option->GetValue() ? String(" (").Append(option->GetAlias().Replace("%1", option->GetValue())).Append(")") : String())); |
| 1341 | |
| 1342 | if (def != NIL) Console::OutputString(String(", default ").Append(def)); |
| 1343 | |
| 1344 | Console::OutputString("\n"); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | else |
| 1349 | { |
| 1350 | Console::OutputString(String("\tno options for ").Append(component->GetName()).Append("\n")); |
| 1351 | } |
| 1352 | |
| 1353 | boca.DeleteComponent(component); |
| 1354 | } |
| 1355 | } |