Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
FFMPEG-capi-GIF创建_C_Ffmpeg_Gif - Fatal编程技术网

FFMPEG-capi-GIF创建

FFMPEG-capi-GIF创建,c,ffmpeg,gif,C,Ffmpeg,Gif,我有一个图像处理管道,我在内存中有图像,我将其转换成AVFrame,然后我尝试用这些图像创建一个GIF 我从这里开始,我只是用将内存中的图像转换为AVFrame来替换视频解码器部分 这项工作相当不错,但我对GIF帧率有意见 在init\u filters(…)方法中,我不理解参数结构的time\u base和pixel\u aspect变量: snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_b

我有一个图像处理管道,我在内存中有图像,我将其转换成
AVFrame
,然后我尝试用这些图像创建一个GIF

我从这里开始,我只是用将内存中的图像转换为
AVFrame
来替换视频解码器部分

这项工作相当不错,但我对GIF帧率有意见

init\u filters(…)
方法中,我不理解参数结构的
time\u base
pixel\u aspect
变量:

    snprintf(args, sizeof(args),
         "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
         width, height, in_fmt, time_base.num, time_base.den,
         pixel_aspect.num, pixel_aspect.den);
我希望FPS=12,那么我应该如下定义它们吗

AVRational time_base = AVRational{1, 12};
AVRational pixel_aspect= AVRational{1, 1}; 
接下来,在将帧馈送到过滤器缓冲区(对于palettegen)的循环中,我还有一个我不理解的选项,AVFrame->pts指的是什么

    // palettegen need a whole stream, just add frame to buffer.
    ret = av_buffersrc_add_frame_flags(buffersrc_ctx, picture_rgb24, AV_BUFFERSRC_FLAG_KEEP_REF);
    if (ret < 0) {
        av_log(nullptr, AV_LOG_ERROR, "error add frame to buffer source %s\n", av_make_error_string(msg_v2, MSG_LEN, ret));
    }

    picture_rgb24->pts += 1; // HERE
所以我对所有需要设置帧率的地方都有点困惑


现在,使用FFMPEG C API在内存中很好地生成了GIF(使用调色板),唯一的问题是GIF速度太快,帧速率不正确。

最后,我找到了它。必须根据帧率(12)和编解码器时间基数(GIF为1/100)增加视频pts

编辑:

更妙的是,我没有硬编码我的发现,我发现我可以通过编解码器访问它,并且无论如何只需要计算一次

if (ofmt_ctx && ofmt_ctx->nb_streams > 0)
        m_pts_increment = av_rescale_q(1, { 1, m_framerate }, ofmt_ctx->streams[0]->time_base);
    else
        m_pts_increment = av_rescale_q(1, { 1, m_framerate }, { 1, 100 });
只需要做(每一帧)

picture_rgb24->pts += av_rescale_q(1, { 1, 12 }, { 1, 100 });
if (ofmt_ctx && ofmt_ctx->nb_streams > 0)
        m_pts_increment = av_rescale_q(1, { 1, m_framerate }, ofmt_ctx->streams[0]->time_base);
    else
        m_pts_increment = av_rescale_q(1, { 1, m_framerate }, { 1, 100 });
picture_rgb24->pts += m_pts_increment;