Extract audio and cue sheet from an NRG audio CD image.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.1 KiB

  1. // This file is part of the NRGrip project.
  2. //
  3. // Copyright (c) 2016, 2018 Matteo Cypriani <mcy@lm7.fr>
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. use std::env;
  23. use std::fs::File;
  24. use std::path::PathBuf;
  25. use std::process;
  26. extern crate getopts;
  27. use getopts::Options;
  28. extern crate nrgrip;
  29. use nrgrip::metadata;
  30. use nrgrip::cue_sheet;
  31. use nrgrip::raw_audio;
  32. const PRETTY_PROGNAME: &'static str = "NRGrip";
  33. const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
  34. fn print_version() {
  35. println!("{} v{}", PRETTY_PROGNAME, VERSION.unwrap_or("X.Y.Z"));
  36. }
  37. fn print_usage(prog_name: &str, opts: &Options) {
  38. let brief = format!("{prettyprog} - rip Nero Burning ROM audio images
  39. Usage:
  40. {prog} [-icrx] [options] <image.nrg>
  41. {prog} [-h | -V]", prettyprog = PRETTY_PROGNAME, prog = prog_name);
  42. print!("{}", opts.usage(&brief));
  43. }
  44. fn main() {
  45. process::exit(main_main());
  46. }
  47. fn main_main() -> i32 {
  48. let args: Vec<String> = env::args().collect();
  49. let prog_fullname = &args.first().expect("Can't retrieve program name");
  50. let prog_name = PathBuf::from(prog_fullname).file_name()
  51. .expect("Can't retrieve program base name")
  52. .to_string_lossy().into_owned();
  53. let mut opts = Options::new();
  54. opts.optflag("i", "info",
  55. "display the image's metadata (default action)");
  56. opts.optflag("x", "extract",
  57. "same as --extract-cue --extract-raw");
  58. opts.optflag("c", "extract-cue",
  59. "extract cue sheet from the NRG metadata");
  60. opts.optflag("r", "extract-raw",
  61. "extract the raw audio tracks");
  62. opts.optflag("S", "no-strip-subchannel",
  63. "don't strip the 96-bit subchannel if present");
  64. opts.optflag("h", "help",
  65. "print this help message");
  66. opts.optflag("V", "version",
  67. "print program version");
  68. let options = match opts.parse(&args[1..]) {
  69. Ok(options) => options,
  70. Err(_) => {
  71. print_usage(&prog_name, &opts);
  72. return 1;
  73. },
  74. };
  75. if options.opt_present("help") {
  76. print_usage(&prog_name, &opts);
  77. return 0;
  78. }
  79. if options.opt_present("version") {
  80. print_version();
  81. return 0;
  82. }
  83. let strip_subchannel = !options.opt_present("no-strip-subchannel");
  84. // Get input NRG image name
  85. if options.free.len() != 1 {
  86. // We need exactly one input file!
  87. print_usage(&prog_name, &opts);
  88. return 1;
  89. }
  90. let img_path = &options.free[0];
  91. println!("NRG image path: \"{}\"", img_path);
  92. // See what actions are to be taken on that file
  93. let action_cue =
  94. options.opt_present("extract-cue") || options.opt_present("extract");
  95. let action_raw =
  96. options.opt_present("extract-raw") || options.opt_present("extract");
  97. let action_info =
  98. options.opt_present("info") || !(action_cue || action_raw);
  99. // Open the image file
  100. let mut fd = match File::open(&img_path) {
  101. Ok(fd) => fd,
  102. Err(err) => {
  103. println!("Can't open image file \"{}\": {}", img_path, err);
  104. return 1;
  105. },
  106. };
  107. // Read the image's metadata
  108. let metadata = match metadata::read_nrg_metadata(&mut fd) {
  109. Ok(metadata) => metadata,
  110. Err(err) => {
  111. println!("Error reading \"{}\": {}", img_path, err);
  112. return 1;
  113. },
  114. };
  115. // Display metadata if requested
  116. if action_info {
  117. println!("\n{}", metadata);
  118. }
  119. // Read and write the cue sheet
  120. if action_cue {
  121. println!("\nExtracting cue sheet...");
  122. if let Err(err) = cue_sheet::write_cue_sheet(&img_path, &metadata) {
  123. println!("Error writing cue sheet: {}", err);
  124. return 1;
  125. }
  126. println!("OK!");
  127. }
  128. // Extract raw audio data
  129. if action_raw {
  130. println!("\nExtracting raw audio data...");
  131. if let Err(err) =
  132. raw_audio::extract_nrg_raw_audio(&mut fd, &img_path,
  133. &metadata, strip_subchannel) {
  134. println!("Error extracting raw audio data: {}", err);
  135. }
  136. println!("OK!");
  137. }
  138. 0
  139. }