dump.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. extern crate mp4parse;
  2. use std::env;
  3. use std::fs::File;
  4. use std::io::{Seek, SeekFrom};
  5. fn dump_file(filename: &String, verbose: bool) {
  6. let mut reader = match File::open(filename) {
  7. Ok(reader) => reader,
  8. _ => {
  9. println!("ERROR: invalid path '{}'", filename);
  10. return;
  11. }
  12. };
  13. let mut context = mp4parse::MediaContext::new();
  14. // Turn on debug output.
  15. if verbose {
  16. mp4parse::set_debug_mode(true);
  17. }
  18. // Read all boxes.
  19. match mp4parse::read_mp4(&mut reader, &mut context) {
  20. Ok(_) => {},
  21. Err(mp4parse::Error::Io(e)) => {
  22. println!("I/O ERROR: {:?} in '{}'", e, filename);
  23. panic!(e);
  24. },
  25. Err(e) => {
  26. let offset = reader.seek(SeekFrom::Current(0)).unwrap();
  27. println!("ERROR: {:?} in '{}' @ {}", e, filename, offset);
  28. },
  29. }
  30. if verbose {
  31. println!("-- result of parsing '{}' --\n{:?}", filename, context);
  32. }
  33. }
  34. fn main() {
  35. let args: Vec<String> = env::args().collect();
  36. if args.len() < 2 {
  37. return;
  38. }
  39. let (skip, verbose) = if args[1] == "-v" {
  40. (2, true)
  41. } else {
  42. (1, false)
  43. };
  44. for filename in args.iter().skip(skip) {
  45. if verbose {
  46. println!("-- dump of '{}' --", filename);
  47. }
  48. dump_file(&filename, verbose);
  49. if verbose {
  50. println!("-- end of '{}' --", filename);
  51. }
  52. }
  53. }