用ffmpeg编码:视频质量下降
Encoding with ffmpeg: video quality drops
我正在尝试用ffmpeg将一组图片编码为视频。我是新手,但我还是学会了。我只有一个问题:视频的前一两秒看起来不错,但随着时间的推移,视频质量不断下降。最后(这是一个大约16秒的视频),质量真的很差,我不明白为什么。
我使用AV_CODEC_ID_MPEG1VIDEO作为视频编解码器(坦率地说,这是我唯一可以工作的),这里是我的代码示例:
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec contextn");
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 codecn");
exit(1);
}
fopen_s(&f, filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %sn", filename);
exit(1);
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video framen");
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 buffern");
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 framen");
exit(1);
}
if (got_output) {
printf(".");
fwrite(pkt.data, 1, pkt.size, f);
av_free_packet(&pkt);
}
// Free the memory
delete img;
}
提示吗?非常感谢!
400kb mpeg 1?是的,质量会很差。使用更高的比特率或更好的编解码器。
相关文章:
- FFmpeg——使用硬件加速进行视频解码
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- FFmpeg原始视频大小参数
- C++ ffmpeg 视频缺少帧,无法在 Quicktime 中播放
- 如何将视频级别分配给 ffmpeg 中的结构 AVCodecContext
- 如何使用FFMPEG从编码的视频中提取AVPACKET的重要信息
- 如何在不重写整个视频文件的情况下仅替换流中的一个数据框架?(ffmpeg)
- ffmpeg/c++ 使用 ffmpeg 对视频帧的附加信息进行编码
- 使用 ffmpeg 将平面 RGB 图像组合成无损视频
- 如何使用C 中的FFMPEG获取视频fps
- C++/C FFmpeg 伪影跨视频帧构建
- 如何使用FFMPEG与C拦截编码的视频数据
- 使用FFMPEG编写多线程视频和音频数据包
- 使用最新的 ffmpeg API (2017) 创建视频
- 视频太快 FFmpeg
- 如何将opencv视频发送到ffmpeg
- FFMPEG如何有效地解码视频框架
- FFmpeg + OpenAL - 播放视频中的流声音不起作用
- 寻求使用 FFmpeg 在 mp4 容器中 h264 编解码器的视频帧.数据包 pts 始终为 0
- FFMPEG 无法显示视频的持续时间