History

ffmpeg: add -fpsmax to clamp output framerate

Useful when encoding in batch or with aberrant inputs.

Gyan Doshi committed 5 years ago

Commit d99cc171 parent 6b8ef514 files · +36 −4Browse files

Unified · Split

4 files
  1. doc/ffmpeg.texi +7 −0
  2. fftools/ffmpeg.c +6 −1
  3. fftools/ffmpeg.h +3 −0
  4. fftools/ffmpeg_opt.c +20 −3
doc/ffmpeg.texi+7−0
@@ -759,6 +759,13 @@ If in doubt use @option{-framerate} instead of the input option @option{-r}. · 758 unmodified lines
759759 As an output option, duplicate or drop input frames to achieve constant output
760760 frame rate @var{fps}.
761761
762+@item -fpsmax[:@var{stream_specifier}] @var{fps} (@emph{output,per-stream})
763+Set maximum frame rate (Hz value, fraction or abbreviation).
764+
765+Clamps output frame rate when output framerate is auto-set and is higher than this value.
766+Useful in batch processing or when input framerate is wrongly detected as very high.
767+It cannot be set together with @code{-r}. It is ignored during streamcopy.
768+
762769 @item -s[:@var{stream_specifier}] @var{size} (@emph{input/output,per-stream})
763770 Set frame size.
764771
fftools/ffmpeg.c+6−1
@@ -3376,7 +3376,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) · 3375 unmodified lines
33763376 ost->frame_rate = ist->framerate;
33773377 if (ist && !ost->frame_rate.num)
33783378 ost->frame_rate = ist->st->r_frame_rate;
3379− if (ist && !ost->frame_rate.num) {
3379+ if (ist && !ost->frame_rate.num && !ost->max_frame_rate.num) {
33803380 ost->frame_rate = (AVRational){25, 1};
33813381 av_log(NULL, AV_LOG_WARNING,
33823382 "No information "
@@ -3386,6 +3386,11 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) · 3 unmodified lines
33863386 ost->file_index, ost->index);
33873387 }
33883388
3389+ if (ost->max_frame_rate.num &&
3390+ (av_q2d(ost->frame_rate) > av_q2d(ost->max_frame_rate) ||
3391+ !ost->frame_rate.den))
3392+ ost->frame_rate = ost->max_frame_rate;
3393+
33893394 if (ost->enc->supported_framerates && !ost->force_fps) {
33903395 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
33913396 ost->frame_rate = ost->enc->supported_framerates[idx];
fftools/ffmpeg.h+3−0
@@ -108,6 +108,8 @@ typedef struct OptionsContext { · 107 unmodified lines
108108 int nb_audio_sample_rate;
109109 SpecifierOpt *frame_rates;
110110 int nb_frame_rates;
111+ SpecifierOpt *max_frame_rates;
112+ int nb_max_frame_rates;
111113 SpecifierOpt *frame_sizes;
112114 int nb_frame_sizes;
113115 SpecifierOpt *frame_pix_fmts;
@@ -479,6 +481,7 @@ typedef struct OutputStream { · 365 unmodified lines
479481
480482 /* video only */
481483 AVRational frame_rate;
484+ AVRational max_frame_rate;
482485 int is_cfr;
483486 int force_fps;
484487 int top_field_first;
fftools/ffmpeg_opt.c+20−3
@@ -55,6 +55,7 @@ static const char *const opt_name_codec_names[] = {"c", "codec", " · 54 unmodified lines
5555 static const char *const opt_name_audio_channels[] = {"ac", NULL};
5656 static const char *const opt_name_audio_sample_rate[] = {"ar", NULL};
5757 static const char *const opt_name_frame_rates[] = {"r", NULL};
58+static const char *const opt_name_max_frame_rates[] = {"fpsmax", NULL};
5859 static const char *const opt_name_frame_sizes[] = {"s", NULL};
5960 static const char *const opt_name_frame_pix_fmts[] = {"pix_fmt", NULL};
6061 static const char *const opt_name_ts_scale[] = {"itsscale", NULL};
@@ -1688,7 +1689,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in · 1627 unmodified lines
16881689 AVStream *st;
16891690 OutputStream *ost;
16901691 AVCodecContext *video_enc;
1691− char *frame_rate = NULL, *frame_aspect_ratio = NULL;
1692+ char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
16921693
16931694 ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);
16941695 st = ost->st;
@@ -1699,8 +1700,21 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in · 4 unmodified lines
16991700 av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
17001701 exit_program(1);
17011702 }
1702− if (frame_rate && video_sync_method == VSYNC_PASSTHROUGH)
1703− av_log(NULL, AV_LOG_ERROR, "Using -vsync 0 and -r can produce invalid output files\n");
1703+
1704+ MATCH_PER_STREAM_OPT(max_frame_rates, str, max_frame_rate, oc, st);
1705+ if (max_frame_rate && av_parse_video_rate(&ost->max_frame_rate, max_frame_rate) < 0) {
1706+ av_log(NULL, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate);
1707+ exit_program(1);
1708+ }
1709+
1710+ if (frame_rate && max_frame_rate) {
1711+ av_log(NULL, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n");
1712+ exit_program(1);
1713+ }
1714+
1715+ if ((frame_rate || max_frame_rate) &&
1716+ video_sync_method == VSYNC_PASSTHROUGH)
1717+ av_log(NULL, AV_LOG_ERROR, "Using -vsync 0 and -r/-fpsmax can produce invalid output files\n");
17041718
17051719 MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
17061720 if (frame_aspect_ratio) {
@@ -3599,6 +3613,9 @@ const OptionDef options[] = { · 1892 unmodified lines
35993613 { "r", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
36003614 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_rates) },
36013615 "set frame rate (Hz value, fraction or abbreviation)", "rate" },
3616+ { "fpsmax", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
3617+ OPT_OUTPUT, { .off = OFFSET(max_frame_rates) },
3618+ "set max frame rate (Hz value, fraction or abbreviation)", "rate" },
36023619 { "s", OPT_VIDEO | HAS_ARG | OPT_SUBTITLE | OPT_STRING | OPT_SPEC |
36033620 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_sizes) },
36043621 "set frame size (WxH or abbreviation)", "size" },

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t