FFmpeg: Difference between revisions
Line 32: | Line 32: | ||
===Muxing to memory=== | ===Muxing to memory=== | ||
You can specify a custom <code>AVIOContext</code> and attach it to your <code>AVFormatContext->pb</code> to mux directly to memory or to implement your own buffering. | You can specify a custom <code>AVIOContext</code> and attach it to your <code>AVFormatContext->pb</code> to mux directly to memory or to implement your own buffering. | ||
===NVENC=== | |||
[https://superuser.com/questions/1296374/best-settings-for-ffmpeg-with-nvenc Options Reference] | |||
When encoding using NVENC, your <code>codec_ctx->priv_data</code> is a pointer to a <code>NvencContext</code>. | |||
To list all of the things you can set in the private data, you can type the following in bash | |||
<syntaxhighlight lang="bash"> | |||
ffmpeg -hide_banner -h encoder=h264_nvenc | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="c++"> | |||
if ((ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_CUDA, NULL, | |||
NULL, 0)) < 0) { | |||
cerr << "[VideoEncoder::VideoEncoder] Failed to create hw context" << endl; | |||
return; | |||
} | |||
if (!(codec = avcodec_find_encoder_by_name("h264_nvenc"))) { | |||
cerr << "[VideoEncoder::VideoEncoder] Failed to find h264_nvenc encoder" | |||
<< endl; | |||
return; | |||
} | |||
codec_ctx = avcodec_alloc_context3(codec); | |||
codec_ctx->bit_rate = 2500000; | |||
codec_ctx->width = source_codec_ctx->width; | |||
codec_ctx->height = source_codec_ctx->height; | |||
codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; | |||
codec_ctx->time_base = source_codec_ctx->time_base; | |||
input_timebase = source_codec_ctx->time_base; | |||
codec_ctx->framerate = source_codec_ctx->framerate; | |||
codec_ctx->pix_fmt = AV_PIX_FMT_CUDA; | |||
codec_ctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE; | |||
codec_ctx->max_b_frames = 0; | |||
codec_ctx->delay = 0; | |||
codec_ctx->gop_size = 0; | |||
// Todo: figure out which ones of these do nothing | |||
av_opt_set(codec_ctx->priv_data, "cq", "23", AV_OPT_SEARCH_CHILDREN); | |||
av_opt_set(codec_ctx->priv_data, "preset", "llhp", 0); | |||
av_opt_set(codec_ctx->priv_data, "tune", "zerolatency", 0); | |||
av_opt_set(codec_ctx->priv_data, "look_ahead", "0", 0); | |||
av_opt_set(codec_ctx->priv_data, "zerolatency", "1", 0); | |||
av_opt_set(codec_ctx->priv_data, "nb_surfaces", "0", 0); | |||
</syntaxhighlight> |