diff --git a/ffmpeg.c b/ffmpeg.c
index ce3c05a1a34ade63b15b98fe7d51388926fc64b2..fc48e7c442e33c09954fe674cd6fd75b4ea53d30 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -3083,6 +3083,7 @@ static int transcode_init(void)
                     ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
                 if (ist && !ost->frame_rate.num)
                     ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
+//                    ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_frame_rate : (AVRational){25, 1};
                 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
                     int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
                     ost->frame_rate = ost->enc->supported_framerates[idx];
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 19a11fc627dedfee4b7d1527627a24ce0541dd53..1614646aa5d42279cb3d2f6c750f69387de99c61 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -648,6 +648,7 @@ typedef struct AVStream {
      *             not actually used for encoding.
      */
     AVCodecContext *codec;
+#if FF_API_R_FRAME_RATE
     /**
      * Real base framerate of the stream.
      * This is the lowest framerate with which all timestamps can be
@@ -657,6 +658,7 @@ typedef struct AVStream {
      * approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
      */
     AVRational r_frame_rate;
+#endif
     void *priv_data;
 
     /**
diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
index edde77843bf1b6e03fe88a09f05e70f97d4ce1ad..dcfaa27033e131e04e8e0a867d64e83593c32fad 100644
--- a/libavformat/avisynth.c
+++ b/libavformat/avisynth.c
@@ -120,8 +120,11 @@ static int avisynth_read_header(AVFormatContext *s)
                   st = avformat_new_stream(s, NULL);
                   st->id = id;
                   st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-                  st->r_frame_rate.num = stream->info.dwRate;
-                  st->r_frame_rate.den = stream->info.dwScale;
+                  st->avg_frame_rate.num = stream->info.dwRate;
+                  st->avg_frame_rate.den = stream->info.dwScale;
+#if FF_API_R_FRAME_RATE
+                  st->r_frame_rate = st->avg_frame_rate;
+#endif
 
                   st->codec->width = imgfmt.bmiHeader.biWidth;
                   st->codec->height = imgfmt.bmiHeader.biHeight;
diff --git a/libavformat/avs.c b/libavformat/avs.c
index 8f3dd27daecbf9c26f8e51a15dbaa566000e3c0b..7bd4bcf82c8b7a8f30f5d0867e8dc2cf86c39ad6 100644
--- a/libavformat/avs.c
+++ b/libavformat/avs.c
@@ -188,8 +188,10 @@ static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
                     avs->st_video->codec->height = avs->height;
                     avs->st_video->codec->bits_per_coded_sample=avs->bits_per_sample;
                     avs->st_video->nb_frames = avs->nb_frames;
-                    avs->st_video->r_frame_rate = avs->st_video->avg_frame_rate =
-                                                  (AVRational){avs->fps, 1};
+#if FF_API_R_FRAME_RATE
+                    avs->st_video->r_frame_rate =
+#endif
+                    avs->st_video->avg_frame_rate = (AVRational){avs->fps, 1};
                 }
                 return avs_read_video_packet(s, pkt, type, sub_type, size,
                                              palette, palette_size);
diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c
index 3070b05780e52e78143206e4bfa57c298ca7682a..d6dfd92f2f728d15e5cefd34c0144a88e270b890 100644
--- a/libavformat/electronicarts.c
+++ b/libavformat/electronicarts.c
@@ -439,8 +439,8 @@ static int ea_read_header(AVFormatContext *s)
         st->duration = st->nb_frames = ea->nb_frames;
         if (ea->time_base.num)
             avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
-        st->r_frame_rate = st->avg_frame_rate = (AVRational){ea->time_base.den,
-                                                             ea->time_base.num};
+        st->r_frame_rate =
+        st->avg_frame_rate = (AVRational){ea->time_base.den, ea->time_base.num};
     }
 
     if (ea->audio_codec) {
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 997824dcecea468095959eb164c8195106353b13..f8c25d36c1f6e54f8a35314e24977cfd0ec1384a 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -65,11 +65,11 @@ static int flv_probe(AVProbeData *p)
     return 0;
 }
 
-static AVStream *create_stream(AVFormatContext *s, int tag, int codec_type){
+static AVStream *create_stream(AVFormatContext *s, int codec_type)
+{
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return NULL;
-    st->id = tag;
     st->codec->codec_type = codec_type;
     if(s->nb_streams>=3 ||(   s->nb_streams==2
                            && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_DATA
@@ -402,7 +402,7 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vst
             else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
                 acodec->bit_rate = num_val * 1024.0;
             else if (!strcmp(key, "datastream")) {
-                AVStream *st = create_stream(s, 2, AVMEDIA_TYPE_DATA);
+                AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
                 if (!st)
                     return AVERROR(ENOMEM);
                 st->codec->codec_id = CODEC_ID_TEXT;
@@ -509,11 +509,11 @@ static int flv_read_header(AVFormatContext *s)
         s->ctx_flags |= AVFMTCTX_NOHEADER;
 
     if(flags & FLV_HEADER_FLAG_HASVIDEO){
-        if(!create_stream(s, 0, AVMEDIA_TYPE_VIDEO))
+        if(!create_stream(s, AVMEDIA_TYPE_VIDEO))
             return AVERROR(ENOMEM);
     }
     if(flags & FLV_HEADER_FLAG_HASAUDIO){
-        if(!create_stream(s, 1, AVMEDIA_TYPE_AUDIO))
+        if(!create_stream(s, AVMEDIA_TYPE_AUDIO))
             return AVERROR(ENOMEM);
     }
     // Flag doesn't indicate whether or not there is script-data present. Must
@@ -617,7 +617,7 @@ static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
     }
 
     if (i == s->nb_streams) {
-        st = create_stream(s, 2, AVMEDIA_TYPE_DATA);
+        st = create_stream(s, AVMEDIA_TYPE_DATA);
         if (!st)
             goto out;
         st->codec->codec_id = CODEC_ID_TEXT;
@@ -727,7 +727,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
     if(i == s->nb_streams){
         av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
-        st = create_stream(s, stream_type,
+        st = create_stream(s,
              (int[]){AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA}[stream_type]);
     }
     av_dlog(s, "%d %X %d \n", stream_type, flags, st->discard);
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 610c3fc7207732e466c5f4a0ee6374929422ab98..c6a676a2db75ee126b87253480a57a212da9be9f 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -205,9 +205,9 @@ static int flv_write_header(AVFormatContext *s)
         FLVStreamContext *sc;
         switch (enc->codec_type) {
         case AVMEDIA_TYPE_VIDEO:
-            if (s->streams[i]->r_frame_rate.den &&
-                s->streams[i]->r_frame_rate.num) {
-                framerate = av_q2d(s->streams[i]->r_frame_rate);
+            if (s->streams[i]->avg_frame_rate.den &&
+                s->streams[i]->avg_frame_rate.num) {
+                framerate = av_q2d(s->streams[i]->avg_frame_rate);
             } else {
                 framerate = 1 / av_q2d(s->streams[i]->codec->time_base);
             }
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index bb610427758c064c474827e9c5cd2aa877ac649a..8640aeeee05fb21be32616574759db5d455c8c36 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -1705,9 +1705,11 @@ static int matroska_read_header(AVFormatContext *s)
                       255);
             st->need_parsing = AVSTREAM_PARSE_HEADERS;
             if (track->default_duration) {
-                av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
+                av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
                           1000000000, track->default_duration, 30000);
-                st->avg_frame_rate = st->r_frame_rate;
+#if FF_API_R_FRAME_RATE
+                st->r_frame_rate = st->avg_frame_rate;
+#endif
             }
 
             /* export stereo mode flag as metadata tag */
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 1646c6330cc7ac8facaa099f58c4e8350d445358..98e88af70e95b7f11ed427d2b5c7c34232b59a1e 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2085,9 +2085,11 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
         av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
                   sc->time_scale*st->nb_frames, st->duration, INT_MAX);
 
+#if FF_API_R_FRAME_RATE
         if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
             av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
                       sc->time_scale, sc->stts_data[0].duration, INT_MAX);
+#endif
     }
 
     switch (st->codec->codec_id) {
diff --git a/libavformat/nuv.c b/libavformat/nuv.c
index e48c85a789f9998eddff50143b1a8a7364fa213f..928f856c88eca8bfd71f7c2487a36a9fd17ec1d1 100644
--- a/libavformat/nuv.c
+++ b/libavformat/nuv.c
@@ -163,7 +163,10 @@ static int nuv_header(AVFormatContext *s) {
         vst->codec->height = height;
         vst->codec->bits_per_coded_sample = 10;
         vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
-        vst->r_frame_rate = av_d2q(fps, 60000);
+#if FF_API_R_FRAME_RATE
+        vst->r_frame_rate =
+#endif
+        vst->avg_frame_rate = av_d2q(fps, 60000);
         avpriv_set_pts_info(vst, 32, 1, 1000);
     } else
         ctx->v_id = -1;
diff --git a/libavformat/r3d.c b/libavformat/r3d.c
index 757e583213ef8f35a18e5d5c1637746e9737bb47..4c32d90af57bd5c45e8a8625c47e25f2f788dfb5 100644
--- a/libavformat/r3d.c
+++ b/libavformat/r3d.c
@@ -87,8 +87,12 @@ static int r3d_read_red1(AVFormatContext *s)
 
     framerate.num = avio_rb16(s->pb);
     framerate.den = avio_rb16(s->pb);
-    if (framerate.num && framerate.den)
-        st->r_frame_rate = st->avg_frame_rate = framerate;
+    if (framerate.num && framerate.den) {
+#if FF_API_R_FRAME_RATE
+        st->r_frame_rate =
+#endif
+        st->avg_frame_rate = framerate;
+    }
 
     tmp = avio_r8(s->pb); // audio channels
     av_dlog(s, "audio channels %d\n", tmp);
@@ -135,10 +139,10 @@ static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
         av_dlog(s, "video offset %d: %#x\n", i, r3d->video_offsets[i]);
     }
 
-    if (st->r_frame_rate.num)
+    if (st->avg_frame_rate.num)
         st->duration = av_rescale_q(r3d->video_offsets_count,
-                                    (AVRational){st->r_frame_rate.den,
-                                                 st->r_frame_rate.num},
+                                    (AVRational){st->avg_frame_rate.den,
+                                                 st->avg_frame_rate.num},
                                     st->time_base);
     av_dlog(s, "duration %"PRId64"\n", st->duration);
 
@@ -262,9 +266,9 @@ static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
 
     pkt->stream_index = 0;
     pkt->dts = dts;
-    if (st->r_frame_rate.num)
+    if (st->avg_frame_rate.num)
         pkt->duration = (uint64_t)st->time_base.den*
-            st->r_frame_rate.den/st->r_frame_rate.num;
+            st->avg_frame_rate.den/st->avg_frame_rate.num;
     av_dlog(s, "pkt dts %"PRId64" duration %d\n", pkt->dts, pkt->duration);
 
     return 0;
@@ -362,11 +366,11 @@ static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, i
     R3DContext *r3d = s->priv_data;
     int frame_num;
 
-    if (!st->r_frame_rate.num)
+    if (!st->avg_frame_rate.num)
         return -1;
 
     frame_num = av_rescale_q(sample_time, st->time_base,
-                             (AVRational){st->r_frame_rate.den, st->r_frame_rate.num});
+                             (AVRational){st->avg_frame_rate.den, st->avg_frame_rate.num});
     av_dlog(s, "seek frame num %d timestamp %"PRId64"\n",
             frame_num, sample_time);
 
diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index 76b3bdfb621f2cd61305cf609cac6ba06a073938..24d80c820c5db54b3ad8e705a2c5db74bc09afc0 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -345,9 +345,11 @@ ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
         if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
             return ret;
 
-        av_reduce(&st->r_frame_rate.den, &st->r_frame_rate.num,
+        av_reduce(&st->avg_frame_rate.den, &st->avg_frame_rate.num,
                   0x10000, fps, (1 << 30) - 1);
-        st->avg_frame_rate = st->r_frame_rate;
+#if FF_API_R_FRAME_RATE
+        st->r_frame_rate = st->avg_frame_rate;
+#endif
     }
 
 skip:
diff --git a/libavformat/utils.c b/libavformat/utils.c
index d1e6459def978b3b547fe58550d7eb2ce736d3ce..3807954e21250b064582a54556ee074573283f97 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2556,7 +2556,9 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
     }
 
     for (i=0; i<ic->nb_streams; i++) {
+#if FF_API_R_FRAME_RATE
         ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
+#endif
         ic->streams[i]->info->fps_first_dts = AV_NOPTS_VALUE;
         ic->streams[i]->info->fps_last_dts  = AV_NOPTS_VALUE;
     }
@@ -2676,6 +2678,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
             }
             st->info->codec_info_duration += pkt->duration;
         }
+#if FF_API_R_FRAME_RATE
         {
             int64_t last = st->info->last_dts;
 
@@ -2703,6 +2706,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
             if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
                 st->info->last_dts = pkt->dts;
         }
+#endif
         if(st->parser && st->parser->parser->split && !st->codec->extradata){
             int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
             if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
@@ -2784,9 +2788,28 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
 
             /* estimate average framerate if not set by demuxer */
             if (st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && st->info->codec_info_duration) {
+                int      best_fps = 0;
+                double best_error = 0.01;
+
                 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
                           (st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
                           st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
+
+                /* round guessed framerate to a "standard" framerate if it's
+                 * within 1% of the original estimate*/
+                for (j = 1; j < MAX_STD_TIMEBASES; j++) {
+                    AVRational std_fps = (AVRational){get_std_framerate(j), 12*1001};
+                    double error = fabs(av_q2d(st->avg_frame_rate) / av_q2d(std_fps) - 1);
+
+                    if (error < best_error) {
+                        best_error = error;
+                        best_fps   = std_fps.num;
+                    }
+                }
+                if (best_fps) {
+                    av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
+                              best_fps, 12*1001, INT_MAX);
+                }
             }
             // the check for tb_unreliable() is not completely correct, since this is not about handling
             // a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
@@ -3835,8 +3858,10 @@ static void dump_stream_format(AVFormatContext *ic, int i, int index, int is_out
     if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
         if(st->avg_frame_rate.den && st->avg_frame_rate.num)
             print_fps(av_q2d(st->avg_frame_rate), "fps");
+#if FF_API_R_FRAME_RATE
         if(st->r_frame_rate.den && st->r_frame_rate.num)
             print_fps(av_q2d(st->r_frame_rate), "tbr");
+#endif
         if(st->time_base.den && st->time_base.num)
             print_fps(1/av_q2d(st->time_base), "tbn");
         if(st->codec->time_base.den && st->codec->time_base.num)
diff --git a/libavformat/vc1testenc.c b/libavformat/vc1testenc.c
index 10b7b64819f6aaa72f2d74d7ec678e117213ad6c..56c723c729d58dd63c33fcd6db20a2f01ccc1d90 100644
--- a/libavformat/vc1testenc.c
+++ b/libavformat/vc1testenc.c
@@ -44,8 +44,8 @@ static int vc1test_write_header(AVFormatContext *s)
     avio_wl24(pb, 0); // hrd_buffer
     avio_w8(pb, 0x80); // level|cbr|res1
     avio_wl32(pb, 0); // hrd_rate
-    if (s->streams[0]->r_frame_rate.den && s->streams[0]->r_frame_rate.num == 1)
-        avio_wl32(pb, s->streams[0]->r_frame_rate.den);
+    if (s->streams[0]->avg_frame_rate.den && s->streams[0]->avg_frame_rate.num == 1)
+        avio_wl32(pb, s->streams[0]->avg_frame_rate.den);
     else
         avio_wl32(pb, 0xFFFFFFFF); //variable framerate
     avpriv_set_pts_info(s->streams[0], 32, 1, 1000);
diff --git a/libavformat/version.h b/libavformat/version.h
index d7841f743cb79ec3452ff59444d9f622c7d39626..faf068ff3970829416b3040c94ecf9d6ef8e1461 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -82,5 +82,8 @@
 #ifndef FF_API_AV_GETTIME
 #define FF_API_AV_GETTIME              (LIBAVFORMAT_VERSION_MAJOR < 55)
 #endif
+#ifndef FF_API_R_FRAME_RATE
+#define FF_API_R_FRAME_RATE            (LIBAVFORMAT_VERSION_MAJOR < 55)
+#endif
 
 #endif /* AVFORMAT_VERSION_H */