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
759As an output option, duplicate or drop input frames to achieve constant output759As an output option, duplicate or drop input frames to achieve constant output
760frame rate @var{fps}.760frame rate @var{fps}.
761 761
762@item -fpsmax[:@var{stream_specifier}] @var{fps} (@emph{output,per-stream})
763Set maximum frame rate (Hz value, fraction or abbreviation).
764
765Clamps output frame rate when output framerate is auto-set and is higher than this value.
766Useful in batch processing or when input framerate is wrongly detected as very high.
767It cannot be set together with @code{-r}. It is ignored during streamcopy.
768
762@item -s[:@var{stream_specifier}] @var{size} (@emph{input/output,per-stream})769@item -s[:@var{stream_specifier}] @var{size} (@emph{input/output,per-stream})
763Set frame size.770Set frame size.
764 771
fftools/ffmpeg.c+6−1
@@ -3376,7 +3376,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) · 3375 unmodified lines
3376 ost->frame_rate = ist->framerate;3376 ost->frame_rate = ist->framerate;
3377 if (ist && !ost->frame_rate.num)3377 if (ist && !ost->frame_rate.num)
3378 ost->frame_rate = ist->st->r_frame_rate;3378 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) {
3380 ost->frame_rate = (AVRational){25, 1};3380 ost->frame_rate = (AVRational){25, 1};
3381 av_log(NULL, AV_LOG_WARNING,3381 av_log(NULL, AV_LOG_WARNING,
3382 "No information "3382 "No information "
@@ -3386,6 +3386,11 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) · 3 unmodified lines
3386 ost->file_index, ost->index);3386 ost->file_index, ost->index);
3387 }3387 }
3388 3388
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
3389 if (ost->enc->supported_framerates && !ost->force_fps) {3394 if (ost->enc->supported_framerates && !ost->force_fps) {
3390 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);3395 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
3391 ost->frame_rate = ost->enc->supported_framerates[idx];3396 ost->frame_rate = ost->enc->supported_framerates[idx];
fftools/ffmpeg.h+3−0
@@ -108,6 +108,8 @@ typedef struct OptionsContext { · 107 unmodified lines
108 int nb_audio_sample_rate;108 int nb_audio_sample_rate;
109 SpecifierOpt *frame_rates;109 SpecifierOpt *frame_rates;
110 int nb_frame_rates;110 int nb_frame_rates;
111 SpecifierOpt *max_frame_rates;
112 int nb_max_frame_rates;
111 SpecifierOpt *frame_sizes;113 SpecifierOpt *frame_sizes;
112 int nb_frame_sizes;114 int nb_frame_sizes;
113 SpecifierOpt *frame_pix_fmts;115 SpecifierOpt *frame_pix_fmts;
@@ -479,6 +481,7 @@ typedef struct OutputStream { · 365 unmodified lines
479 481
480 /* video only */482 /* video only */
481 AVRational frame_rate;483 AVRational frame_rate;
484 AVRational max_frame_rate;
482 int is_cfr;485 int is_cfr;
483 int force_fps;486 int force_fps;
484 int top_field_first;487 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
55static const char *const opt_name_audio_channels[] = {"ac", NULL};55static const char *const opt_name_audio_channels[] = {"ac", NULL};
56static const char *const opt_name_audio_sample_rate[] = {"ar", NULL};56static const char *const opt_name_audio_sample_rate[] = {"ar", NULL};
57static const char *const opt_name_frame_rates[] = {"r", NULL};57static const char *const opt_name_frame_rates[] = {"r", NULL};
58static const char *const opt_name_max_frame_rates[] = {"fpsmax", NULL};
58static const char *const opt_name_frame_sizes[] = {"s", NULL};59static const char *const opt_name_frame_sizes[] = {"s", NULL};
59static const char *const opt_name_frame_pix_fmts[] = {"pix_fmt", NULL};60static const char *const opt_name_frame_pix_fmts[] = {"pix_fmt", NULL};
60static const char *const opt_name_ts_scale[] = {"itsscale", NULL};61static 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
1688 AVStream *st;1689 AVStream *st;
1689 OutputStream *ost;1690 OutputStream *ost;
1690 AVCodecContext *video_enc;1691 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;
1692 1693
1693 ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);1694 ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);
1694 st = ost->st;1695 st = ost->st;
@@ -1699,8 +1700,21 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in · 4 unmodified lines
1699 av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);1700 av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
1700 exit_program(1);1701 exit_program(1);
1701 }1702 }
1702 if (frame_rate && video_sync_method == VSYNC_PASSTHROUGH)1703
1703 av_log(NULL, AV_LOG_ERROR, "Using -vsync 0 and -r can produce invalid output files\n");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");
1704 1718
1705 MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);1719 MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
1706 if (frame_aspect_ratio) {1720 if (frame_aspect_ratio) {
@@ -3599,6 +3613,9 @@ const OptionDef options[] = { · 1892 unmodified lines
3599 { "r", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |3613 { "r", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
3600 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_rates) },3614 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_rates) },
3601 "set frame rate (Hz value, fraction or abbreviation)", "rate" },3615 "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" },
3602 { "s", OPT_VIDEO | HAS_ARG | OPT_SUBTITLE | OPT_STRING | OPT_SPEC |3619 { "s", OPT_VIDEO | HAS_ARG | OPT_SUBTITLE | OPT_STRING | OPT_SPEC |
3603 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_sizes) },3620 OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_sizes) },
3604 "set frame size (WxH or abbreviation)", "size" },3621 "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