Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. // elsewhere in the code
  2. if (videoWriter.ffmpegEncoderStart(filename, AV_CODEC_ID_H264, 60, 1920, 1080))
  3. {
  4.     //blah blah
  5. }
  6.  
  7. bool VideoWriter::ffmpegEncoderStart(const char *filename, int codec_id, int fps, int width, int height) {
  8.     AVCodec *codec;
  9.     int ret;
  10.     avcodec_register_all();
  11.     codec = avcodec_find_encoder((AVCodecID)codec_id);
  12.     if (!codec)
  13.         return false;
  14.  
  15.     c = avcodec_alloc_context3(codec);
  16.     if (!c)
  17.         return false;
  18.  
  19.     avcodec_get_context_defaults3(c, codec);
  20.  
  21.     c->profile = FF_PROFILE_H264_BASELINE;
  22.     c->width = width;
  23.     c->height = height;
  24.     c->time_base.num = 1;
  25.     c->time_base.den = fps;
  26.     c->pix_fmt = AV_PIX_FMT_YUV420P;
  27.     c->gop_size = 10;
  28.     c->level = 50;
  29.     /*mpeg1
  30.     c->bit_rate = 400000;
  31.     c->width = width;
  32.     c->height = height;
  33.     c->time_base.num = 1;
  34.     c->time_base.den = fps;
  35.     c->gop_size = 10;
  36.     c->max_b_frames = 1;
  37.     c->pix_fmt = AV_PIX_FMT_YUV420P;
  38.     */
  39.     if (codec_id == AV_CODEC_ID_H264)
  40.         av_opt_set(c->priv_data, "preset", "slow", 0);
  41.     int returnv = avcodec_open2(c, codec, NULL);
  42.     //if (avcodec_open2(c, codec, NULL) < 0)
  43.     if (returnv < 0) // <- this fails
  44.         return false;
  45.  
  46.     file = fopen(filename, "wb");
  47.     if (!file)
  48.         return false;
  49.  
  50.     frame = av_frame_alloc();
  51.     if (!frame)
  52.         return false;
  53.     frame->format = c->pix_fmt;
  54.     frame->width = c->width;
  55.     frame->height = c->height;
  56.     ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, c->pix_fmt, 32);
  57.     if (ret < 0)
  58.         return false;
  59.  
  60.     return true;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement