int parse_arguments()

in vireo/tools/remux/main.cpp [78:150]


int parse_arguments(int argc, const char* argv[], Config& config) {
  int last_arg = 1;
  for (int i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "-iterations") == 0) {
      int arg_iterations = atoi(argv[++i]);
      if (arg_iterations < 1 || arg_iterations > kMaxIterations) {
        cerr << "iterations must be between 1 and " << kMaxIterations << endl;
        return 1;
      }
      config.iterations = (int)arg_iterations;
      last_arg = i + 1;
    } else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "-start_gop") == 0) {
      int arg_start_gop = atoi(argv[++i]);
      if (arg_start_gop < 0) {
        cerr << "start gop cannot be non-negative" << endl;
        return 1;
      }
      config.start_gop = (int)arg_start_gop;
      last_arg = i + 1;
    } else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "-num_gops") == 0) {
      int arg_num_gops = atoi(argv[++i]);
      if (arg_num_gops <= 0) {
        cerr << "num gops must be positive" << endl;
        return 1;
      }
      config.num_gops = (int)arg_num_gops;
      last_arg = i + 1;
    } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "-type") == 0) {
      FileFormat arg_file_format = (FileFormat)atoi(argv[++i]);
      if (arg_file_format < FileFormat::Regular || arg_file_format > FileFormat::SamplesOnly) {
        stringstream choices; choices << FileFormat::Regular << ": regular, " << FileFormat::DashInitializer << ": dash init, " << FileFormat::DashData << ": dash data, " << FileFormat::HeaderOnly << ": header only, " << FileFormat::SamplesOnly << ": samples only";
        cerr << "file type has to be one of the following choices => " << choices.str() << endl;
        return 1;
      }
      config.file_format = arg_file_format;
      last_arg = i + 1;
    } else if (strcmp(argv[i], "--vonly") == 0) {
      config.video_only = true;
      last_arg = i + 1;
    } else if (strcmp(argv[i], "--aonly") == 0) {
      config.audio_only = true;
      last_arg = i + 1;
    }
  }
  if (last_arg + 1 >= argc) {
    cerr << "Need to specify infile and outfile" << endl;
    return 1;
  }
  config.infile = common::Path::MakeAbsolute(argv[last_arg]);
  config.outfile = common::Path::MakeAbsolute(argv[last_arg + 1]);

  struct ExtFileTypePair {
    string ext;
    FileType outfile_type;
  };
  const vector<ExtFileTypePair> ext_file_types = { {".mp4", MP4}, {".m4a", MP4}, {".m4v", MP4}, {".mov", MP4}, {".ts", MP2TS}, {".webm", WebM} };

  // Get output file format
  for (auto ext_file_type: ext_file_types) {
    auto ext = ext_file_type.ext;
    auto out_file_type = ext_file_type.outfile_type;
    if (config.outfile.find(ext) != string::npos) {
        config.outfile_type = out_file_type;
        break;
    }
  }
  if (config.outfile_type == UnknownFileType) {
    cerr << "Output content type is unknown" << endl;
    return 1;
  }

  return 0;
}