我们正在使用 FFmpeg 库 git-ee94362 libavformat v55.2.100。我们的目的是使用 HLS 将两个流(视频和音频)混合到 M3U8 播放列表中。此外,我们希望每个 TS 片段文件的持续时间恰好为 3.0 秒(帧速率为 25 fps)。
为了达到它,我们试图设置几个选项和属性,即:
-segment_time-keyint_min-scenechange_threshold-gop_size-force_key_frames。
我们的代码如下所示:
AVCodecContext *codec_ctx = NULL;
AVFormatContext *ofmt_ctx = NULL;
int ret = 0, gopSize = (int)(3.0 * 25); // 3 sec * 25 fps
// ofmt_ctx and codec_ctx initialization and filling are OK, but:
codec_ctx->time_base.num = 1;
codec_ctx->time_base.den = 25 // fps
// It seems, that the following three lines have no effect without explisit setting of the "hls_time" property
codec_ctx->keyint_min = gopSize; // in FFMpeg application, the corresponding option is "-keyint_min 3"
codec_ctx->scenechange_threshold = 0; // in FFMpeg application, the corresponding option is "-sc_threshold 0"
codec_ctx->gop_size = gopSize; // in FFMpeg application, the corresponding option is "-g 3"
ret = av_opt_set_double(ofmt_ctx, "hls_time", 3.0, AV_OPT_SEARCH_CHILDREN);
// Any of the following lines causes "Option not found" error.
ret = av_opt_set(codec_ctx->priv_data, "profile", "main", AV_OPT_SEARCH_CHILDREN);
ret = av_opt_set(codec_ctx->priv_data, "preset", "ultrafast", AV_OPT_SEARCH_CHILDREN);
ret = av_opt_get(ofmt_ctx, "segment_time", AV_OPT_SEARCH_CHILDREN, &str);
ret = av_opt_set((ofmt_ctx, "segment_time", "3.0", AV_OPT_SEARCH_CHILDREN);
无论如何,TS 文件的持续时间是不同的(~2-3 秒),而不是正好 3.0 秒。我们的问题:解决问题的最佳方法是什么?
安德烈·莫切诺夫。