Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // elsewhere in the code
- if (videoWriter.ffmpegEncoderStart(filename, AV_CODEC_ID_H264, 60, 1920, 1080))
- {
- //blah blah
- }
- bool VideoWriter::ffmpegEncoderStart(const char *filename, int codec_id, int fps, int width, int height) {
- AVCodec *codec;
- int ret;
- avcodec_register_all();
- codec = avcodec_find_encoder((AVCodecID)codec_id);
- if (!codec)
- return false;
- c = avcodec_alloc_context3(codec);
- if (!c)
- return false;
- avcodec_get_context_defaults3(c, codec);
- c->profile = FF_PROFILE_H264_BASELINE;
- c->width = width;
- c->height = height;
- c->time_base.num = 1;
- c->time_base.den = fps;
- c->pix_fmt = AV_PIX_FMT_YUV420P;
- c->gop_size = 10;
- c->level = 50;
- /*mpeg1
- c->bit_rate = 400000;
- c->width = width;
- c->height = height;
- c->time_base.num = 1;
- c->time_base.den = fps;
- c->gop_size = 10;
- c->max_b_frames = 1;
- c->pix_fmt = AV_PIX_FMT_YUV420P;
- */
- if (codec_id == AV_CODEC_ID_H264)
- av_opt_set(c->priv_data, "preset", "slow", 0);
- int returnv = avcodec_open2(c, codec, NULL);
- //if (avcodec_open2(c, codec, NULL) < 0)
- if (returnv < 0) // <- this fails
- return false;
- file = fopen(filename, "wb");
- if (!file)
- return false;
- frame = av_frame_alloc();
- if (!frame)
- return false;
- frame->format = c->pix_fmt;
- frame->width = c->width;
- frame->height = c->height;
- ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, c->pix_fmt, 32);
- if (ret < 0)
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement