std::string get_vfilter_desc()

in torchaudio/csrc/ffmpeg/prototype.cpp [157:209]


std::string get_vfilter_desc(
    const c10::optional<double>& frame_rate,
    const c10::optional<int64_t>& width,
    const c10::optional<int64_t>& height,
    const c10::optional<std::string>& format) {
  // TODO:
  // - Add `flags` for different scale algorithm
  //   https://ffmpeg.org/ffmpeg-filters.html#scale
  // - Consider `framerate` as well
  //   https://ffmpeg.org/ffmpeg-filters.html#framerate

  // - scale
  //   https://ffmpeg.org/ffmpeg-filters.html#scale-1
  //   https://ffmpeg.org/ffmpeg-scaler.html#toc-Scaler-Options
  // - framerate
  //   https://ffmpeg.org/ffmpeg-filters.html#framerate

  // TODO:
  // - format
  //   https://ffmpeg.org/ffmpeg-filters.html#toc-format-1
  // - fps
  //   https://ffmpeg.org/ffmpeg-filters.html#fps-1
  std::vector<std::string> components;
  if (frame_rate)
    components.emplace_back(string_format("fps=%lf", frame_rate.value()));

  std::vector<std::string> scale_components;
  if (width)
    scale_components.emplace_back(string_format("width=%d", width.value()));
  if (height)
    scale_components.emplace_back(string_format("height=%d", height.value()));
  if (scale_components.size())
    components.emplace_back(
        string_format("scale=%s", join(scale_components, ":").c_str()));
  if (format) {
    // TODO:
    // Check other useful formats
    // https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes
    AVPixelFormat fmt = [&]() {
      std::string val = format.value();
      if (val == "RGB")
        return AV_PIX_FMT_RGB24;
      if (val == "BGR")
        return AV_PIX_FMT_BGR24;
      if (val == "GRAY")
        return AV_PIX_FMT_GRAY8;
      throw std::runtime_error("Unexpected format: " + val);
    }();
    components.emplace_back(
        string_format("format=pix_fmts=%s", av_get_pix_fmt_name(fmt)));
  }
  return join(components, ",");
};