31 #define __STDC_CONSTANT_MACROS
35 #pragma warning(disable: 4242 4244)
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wpedantic"
40 #pragma GCC diagnostic ignored "-Wvariadic-macros"
44 #include <libavutil/opt.h>
45 #include <libavutil/imgutils.h>
46 #include <libavcodec/avcodec.h>
47 #include <libavformat/avformat.h>
48 #include <libswscale/swscale.h>
54 #pragma GCC diagnostic pop
70 GUIVideoEncoder(
const char*
const out_file,
const int width,
const int height,
double frameDelay) {
79 if (frameDelay > 0.) {
80 framerate = (int)(1000. / frameDelay);
86 video_st->time_base.num = 1;
87 video_st->time_base.den = framerate;
89 const AVCodec* codec = avcodec_find_encoder(
myFormatContext->oformat->video_codec);
90 if (codec ==
nullptr) {
92 codec = avcodec_find_encoder_by_name(
"libx265");
94 if (codec ==
nullptr) {
100 throw ProcessError(
TL(
"Could not allocate video codec context!"));
125 if (
myCodecCtx->codec_id == AV_CODEC_ID_H264) {
126 av_opt_set(
myCodecCtx->priv_data,
"preset",
"slow", 0);
131 if (
myCodecCtx->codec_id == AV_CODEC_ID_HEVC) {
132 av_opt_set(
myCodecCtx->priv_data,
"preset",
"ultrafast", 0);
133 av_opt_set(
myCodecCtx->priv_data,
"tune",
"zero-latency", 0);
135 if (avcodec_open2(
myCodecCtx, codec,
nullptr) < 0) {
138 avcodec_parameters_from_context(video_st->codecpar,
myCodecCtx);
147 if (av_frame_get_buffer(
myFrame, 32) < 0) {
148 throw ProcessError(
TL(
"Could not allocate the video frame data!"));
163 myPkt = av_packet_alloc();
164 if (
myPkt ==
nullptr) {
171 if (!(
myCodecCtx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
174 if (avcodec_send_frame(
myCodecCtx,
nullptr) < 0) {
180 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
182 }
else if (ret < 0) {
187 av_packet_unref(
myPkt);
197 av_packet_free(&
myPkt);
202 if (av_frame_make_writable(
myFrame) < 0) {
205 uint8_t* inData[1] = { buffer };
206 int inLinesize[1] = { 4 *
myCodecCtx->width };
213 av_strerror(r, errbuf, 64);
219 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
221 }
else if (ret < 0) {
226 myPkt->stream_index = 0;
228 av_packet_unref(
myPkt);
#define WRITE_WARNING(msg)
A simple video encoder from RGBA pics to anything ffmpeg can handle.
SwsContext * mySwsContext
AVFormatContext * myFormatContext
void writeFrame(uint8_t *buffer)
GUIVideoEncoder(const char *const out_file, const int width, const int height, double frameDelay)
AVCodecContext * myCodecCtx