Adi Ulici
Adi Ulici

Reputation: 1295

Encoding with ffmpeg: video quality drops

I'm trying to encode a set of pictures into a video with ffmpeg. I'm new to this stuff, but I managed to get it work. I have only one problem: the first second or two of video look good but as the time goes the video quality keeps dropping and dropping. At the end (it's a video of about 16 seconds) the quality is really bad and I can't understand why.

I'm using AV_CODEC_ID_MPEG1VIDEO as video codec (frankly, it's the only one I could make work) and here is a sample of my code:

c = avcodec_alloc_context3(codec);
if (!c) {
    fprintf(stderr, "Could not allocate video codec context\n");
    exit(1);
}

c->bit_rate = 400000;
c->width = width;
c->height = height;
struct AVRational time_base = {1, 25};
c->time_base = time_base;
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);

// Open the codec
if (avcodec_open2(c, codec, NULL) < 0) {
    fprintf(stderr, "Could not open codec\n");
    exit(1);
}

fopen_s(&f, filename, "wb");
if (!f) {
    fprintf(stderr, "Could not open %s\n", filename);
    exit(1);
}

frame = av_frame_alloc();
if (!frame) {
    fprintf(stderr, "Could not allocate video frame\n");
    exit(1);
}
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) {
    fprintf(stderr, "Could not allocate raw picture buffer\n");
    exit(1);
}

struct SwsContext *frameConvertContext = sws_getContext(width, height,
      PIX_FMT_RGB24,
      c->width, c->height,
      c->pix_fmt,
      SWS_LANCZOS | SWS_ACCURATE_RND, NULL, NULL, NULL);

for (i = 0; i < framesNr; i++) {
    // Read an image
    std::stringstream filename;
    filename << "input/image-" << (i+1) << ".jpg";
    Image* img;
    img = Image::fromJpeg((char*) filename.str().c_str());
    int srcSliceY[1] = { img->getWidth() * 3 };
    const uint8_t* imgbuf[1] = { (uint8_t*) img->getData(sizeof(uint8_t)) };

    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

    fflush(stdout);
    frameConvertContext = sws_getCachedContext(frameConvertContext, width, height,
      PIX_FMT_RGB24,
      c->width, c->height,
      c->pix_fmt,
      SWS_LANCZOS | SWS_ACCURATE_RND, NULL, NULL, NULL);

    sws_scale(frameConvertContext, imgbuf, srcSliceY, 0, img->getHeight(), frame->data, frame->linesize);

    frame->pts = i;

    /* encode the image */
    ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
    if (ret < 0) {
        fprintf(stderr, "Error encoding frame\n");
        exit(1);
    }

    if (got_output) {
        printf(".");
        fwrite(pkt.data, 1, pkt.size, f);
        av_free_packet(&pkt);
    }

    // Free the memory
    delete img;
}

Any tips? Thanks a lot!

Upvotes: 2

Views: 1315

Answers (1)

szatmary
szatmary

Reputation: 31101

400kb mpeg 1? Yes the quality will be abysmal. Use a higher bitrate or a better codec.

Upvotes: 4

Related Questions