title: libx264 encoder video plays too fast
body: <p>I'm trying to write a program that uses libx264 to encode the video frames. I've wrapped this code into a small class (see below). I have frames that are in YUV420 format. libx264 encodes the frames and I save them to a file. I can play the file back in VLC, all of the frames are there, but it plays back at several hundred times the actual frame rate. Currently I am capturing frames at 2.5 FPS, but they play back as if it was recorded at 250 or more FPS. I've tried to change the frame rate with no luck. </p>
  
  <p>I've also tried to set </p>
  
  <pre><code>_param.b_vfr_input = 1
  </code></pre>
  
  <p>and then set the time bases appropriately, but that causes my program to crash. Any ideas? My encode code is shown below. I've also included the output of ffprobe -show_frames</p>
  
  <p>Wrapper Class:</p>
  
  <pre><code>x264wrapper::x264wrapper(int width, int height, int fps, int timeBaseNum, int timeBaseDen, int vfr)
  {
      x264_param_default_preset(&amp;_param, "veryfast", "zerolatency");
      _param.i_threads = 1;
      _param.i_width = width;
      _param.i_height = height;
      _param.i_fps_num = fps;
      _param.i_fps_den = 1;
      // Intra refres:
      _param.i_keyint_max = fps;
      _param.b_intra_refresh = 1;
      //Rate control:
      _param.rc.i_rc_method = X264_RC_CRF;
      //_param.rc.i_rc_method = X264_RC_CQP;
      _param.rc.f_rf_constant = 25;
      _param.rc.f_rf_constant_max = 35;
      //For streaming:
      _param.b_repeat_headers = 1;
      _param.b_annexb = 1;    
      // misc
      _param.b_vfr_input = vfr;
      _param.i_timebase_num = timeBaseNum; 
      _param.i_timebase_den = timeBaseDen;
  
      _param.i_log_level = X264_LOG_DEBUG;
  
      _encoder = x264_encoder_open(&amp;_param);
  
      cout &lt;&lt; "Timebase " &lt;&lt; _param.i_timebase_num &lt;&lt; "/" &lt;&lt; _param.i_timebase_den &lt;&lt; endl;
      cout &lt;&lt; "fps " &lt;&lt; _param.i_fps_num &lt;&lt; "/" &lt;&lt; _param.i_fps_den &lt;&lt; endl;
      _ticks_per_frame = (int64_t)_param.i_timebase_den * _param.i_fps_den / _param.i_timebase_num / _param.i_fps_num;
      cout &lt;&lt; "ticks_per_frame " &lt;&lt; _ticks_per_frame &lt;&lt; endl;
      int result = x264_picture_alloc(&amp;_pic_in, X264_CSP_I420, width, height);
      if (result != 0)
      {
          cout &lt;&lt; "Failed to allocate picture" &lt;&lt; endl;
          throw(1);
      }
  
      _ofs = new ofstream("output.h264", ofstream::out | ofstream::binary);
      _pts = 0;
  }
  
  
  x264wrapper::~x264wrapper(void)
  {
      _ofs-&gt;close();
  }
  
  
  
  void x264wrapper::encode(uint8_t * buf)
  {
      x264_nal_t* nals;
      int i_nals;
      convertFromBalserToX264(buf);
      _pts += _ticks_per_frame;
      _pic_in.i_pts = _pts;
      x264_picture_t pic_out;
      int frame_size = x264_encoder_encode(_encoder, &amp;nals, &amp;i_nals, &amp;_pic_in, &amp;pic_out);
      if (frame_size &gt;= 0)
      {
          _ofs-&gt;write((char*)nals[0].p_payload, frame_size);
      }
      else
      {
          cout &lt;&lt; "error: x264_encoder_encode failed" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Output of ffprobe -show_frames:</p>
  
  <pre><code>[FRAME]
  media_type=video
  key_frame=1
  pkt_pts=N/A
  pkt_pts_time=N/A
  pkt_dts=N/A
  pkt_dts_time=N/A
  pkt_duration=48000
  pkt_duration_time=0.040000
  pkt_pos=0
  width=1920
  height=1080
  pix_fmt=yuv420p
  sample_aspect_ratio=N/A
  pict_type=I
  coded_picture_number=0
  display_picture_number=0
  interlaced_frame=0
  top_field_first=0
  repeat_pict=0
  reference=0
  [/FRAME]
  [FRAME]
  media_type=video
  key_frame=0
  pkt_pts=N/A
  pkt_pts_time=N/A
  pkt_dts=N/A
  pkt_dts_time=N/A
  pkt_duration=N/A
  pkt_duration_time=N/A
  pkt_pos=54947
  width=1920
  height=1080
  pix_fmt=yuv420p
  sample_aspect_ratio=N/A
  pict_type=P
  coded_picture_number=1
  display_picture_number=0
  interlaced_frame=0
  top_field_first=0
  repeat_pict=0
  reference=0
  [/FRAME]
  [FRAME]
  media_type=video
  key_frame=0
  pkt_pts=N/A
  pkt_pts_time=N/A
  pkt_dts=N/A
  pkt_dts_time=N/A
  pkt_duration=N/A
  pkt_duration_time=N/A
  pkt_pos=57899
  width=1920
  height=1080
  pix_fmt=yuv420p
  sample_aspect_ratio=N/A
  pict_type=P
  coded_picture_number=2
  display_picture_number=0
  interlaced_frame=0
  top_field_first=0
  repeat_pict=0
  reference=0
  [/FRAME]
  </code></pre>