视频流 - 控制帧速率x264

时间:2017-03-01 21:23:48

标签: c++ video x264 libx264

我是视频编码的新手,我发现这个https://github.com/RafaelPalomar/H264LiveStreamer应用程序代码对我的项目非常有用。我想通过RTP直播网络摄像头。但是,我在选择每秒帧数(FPS)时遇到问题。下面是控制编码参数的函数。

import java.util.ArrayList;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;


public class GetStateName extends GenericUDF {
    private ArrayList<String> result;

    public Object evaluate(DeferredObject[] arg0) throws HiveException {
        String lat = arg0[0].toString();
        String lng = arg0[1].toString();
        ArrayList<String> tmpLatLng = LookUp.getLatLng(lat,lng);

        result = new ArrayList<String>();
        result.add(tmpLatLng.get(0));
        result.add(tmpLatLng.get(1));
        return result;
    }

    public String getDisplayString(String[] arg0) {
        return new String("Address");
    }


    public ObjectInspector initialize(ObjectInspector[] arguments)
            throws UDFArgumentException {
        // Exactly one input argument
        if( arguments.length != 2 ) {
            throw new UDFArgumentLengthException( " accepts exactly two argument.");
        }

        return ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaHiveVarcharObjectInspector);
    }

}

我使用VLC作为客户端无论我设置i_fps_num的数量,我总是得到12-14 FPS。例如,在VLC Codec选项卡中显示25 FPS但在流统计选项卡中显示它平均只显示13 FPS。这是X264编码器输出

void x264Encoder::initilize()
{
  x264_param_default_preset(&parameters, "veryfast", "zerolatency");
  parameters.i_log_level =  X264_LOG_DEBUG;
  parameters.i_threads = 1;
  parameters.i_width = 640;
  parameters.i_height = 480;
  parameters.i_fps_num = 25;
  parameters.i_fps_den = 1;
  parameters.i_keyint_max = 25;
  parameters.b_intra_refresh = 1;
  parameters.rc.i_rc_method = X264_RC_CRF;
  parameters.rc.i_vbv_buffer_size = 1000000;
  parameters.rc.i_vbv_max_bitrate = 90000;
  parameters.rc.f_rf_constant = 25;
  parameters.rc.f_rf_constant_max = 35;
  parameters.i_sps_id = 7;
  // the following two value you should keep 1
  parameters.b_repeat_headers = 1;    // to get header before every I-Frame
  parameters.b_annexb = 1; // put start code in front of nal. we will remove start code later
  x264_param_apply_profile(&parameters, "baseline");

  encoder = x264_encoder_open(&parameters);
  x264_picture_alloc(&picture_in, X264_CSP_I420, parameters.i_width, parameters.i_height);
  picture_in.i_type = X264_TYPE_AUTO;
  picture_in.img.i_csp = X264_CSP_I420;
  // i have initilized my color space converter for BGR24 to YUV420 because my opencv video capture gives BGR24 image. You can initilize according to your input pixelFormat
  convertContext = sws_getContext(parameters.i_width,parameters.i_height, PIX_FMT_BGR24, parameters.i_width,parameters.i_height,PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
}

编码的最后一个帧编号是72,而在VLC流统计中,它显示133帧被显示,66个块被解码10秒流,此外视频播放是突发的。

我的问题是如何更改x264参数以获得25 FPS?

1 个答案:

答案 0 :(得分:1)

我怀疑您的计算机无法实时编码25 fps。您正在使用一个线程 - 这可能是瓶颈。你能尝试使用相同的设置离线编码并检查编码速度吗?

此外,视频捕获/摄像机可能存在帧速率问题。尝试使用视频捕获和在屏幕上立即播放(无编码)的管道,以确保您的相机能够执行25 fps。