如何在libx264中设置变量FPS以及使用哪些编码器参数?

时间:2017-12-20 17:03:37

标签: c++ video libx264

我正在尝试使用libx264实时编码网络摄像头帧,面对一个问题 - 生成的视频长度正是我设置的,但摄像头延迟了一些,实际捕获时间超过了视频长度。结果,视频中的画面变得很快。
我认为这是由于x264设置中的常量FPS,所以我需要以某种方式使其动态化。可能吗?如果我对FPS有误,那么我需要做什么,同步捕获和写入?

另外,我想知道什么是通过互联网流式传输和录制到磁盘的最佳编码器参数(客户端是从摄像头或屏幕流式传输,服务器正在录制)?

这是控制台日志截图和我的代码:

#include <stdint.h>
#include "stringf.h"
#include "Capture.h"
#include "x264.h"

int main( int argc, char **argv ){
    Camera instance;
    if(!instance.Enable(0)){printf("Camera not available\n");return 1;}
    // Initializing metrics and buffer of frame
    unsigned int width, height, size = instance.GetMetrics(width, height);
    unsigned char *data = (unsigned char *)malloc(size);
    // Setting encoder (I'm not sure about all parameters)
    x264_param_t param;
    x264_param_default_preset(&param, "ultrafast", "zerolatency");
    param.i_threads = 1;
    param.i_width = width;
    param.i_height = height;
    param.i_fps_num = 20;
    param.i_fps_den = 1;
    // Intra refres:
    param.i_keyint_max = 8;
    param.b_intra_refresh = 1;
    // Rate control:
    param.rc.i_rc_method = X264_RC_CRF;
    param.rc.f_rf_constant = 25;
    param.rc.f_rf_constant_max = 35;
    // For streaming:
    param.b_repeat_headers = 1;
    param.b_annexb = 1;

    x264_param_apply_profile(&param, "baseline");
    x264_t* encoder = x264_encoder_open(&param);

    int seconds, expected_time, operation_start, i_nals, frame_size, frames_count;
    expected_time = 1000/param.i_fps_num;
    operation_start = 0;
    seconds = 1;
    frames_count = param.i_fps_num * seconds;
    int *Timings = new int[frames_count];

    x264_picture_t pic_in, pic_out;
    x264_nal_t* nals;
    x264_picture_alloc(&pic_in, X264_CSP_I420, param.i_width, param.i_height);
    // Capture-Encode-Write loop
    for(int i = 0; i < frames_count; i++){
        operation_start = GetTickCount();
        size = instance.GrabBGR(&data);
        instance.BGRtoI420(data, &pic_in.img.plane[0], &pic_in.img.plane[1], &pic_in.img.plane[2], param.i_width, param.i_height);
        frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
        if( frame_size > 0){
            stringf::WriteBufferToFile("test.h264",std::string(reinterpret_cast<char*>(nals->p_payload), frame_size),1);
        }
        Timings[i] = GetTickCount() - operation_start;
    }
    while( x264_encoder_delayed_frames( encoder ) ){ // Flush delayed frames
        frame_size = x264_encoder_encode(encoder, &nals, &i_nals, NULL, &pic_out);
        if( frame_size > 0 ){stringf::WriteBufferToFile("test.h264",std::string(reinterpret_cast<char*>(nals->p_payload), frame_size),1);}
    }
    unsigned int total_time = 0;
    printf("Expected operation time was %d ms per frame at %u FPS\n",expected_time, param.i_fps_num);
    for(unsigned int i = 0; i < frames_count; i++){
        total_time += Timings[i];
        printf("Frame %u takes %d ms\n",(i+1), Timings[i]);
    }
    printf("Record takes %u ms\n",total_time);
    free(data);
    x264_encoder_close( encoder );
    x264_picture_clean( &pic_in );
    return 0;
}

log_x264 捕获需要1453毫秒,输出文件正好播放1秒。

因此,一般来说,视频长度必须与捕获时间相同,但不能像编码器“想要”那样。
如何做到这一点?

0 个答案:

没有答案