FFMpeg编码的视频将只在FFPlay中播放
FFMpeg encoded video will only play in FFPlay
我已经调试了我的程序几个星期了,现在输出视频只显示一个空白屏幕(正在测试VLC, WMP和WMPClassic)。我碰巧尝试使用FFPlay,你看,视频工作完美。我读到这通常是由不正确的像素格式引起的,切换到PIX_FMT_YUV420P将使其普遍工作…但我已经在编码过程中使用了像素格式。还有其他原因导致这个吗?
AVCodec* codec;
AVCodecContext* c = NULL;
uint8_t* outbuf;
int i, out_size, outbuf_size;
avcodec_register_all();
printf("Video encodingn");
// Find the mpeg1 video encoder
codec = avcodec_find_encoder(CODEC_ID_H264);
if (!codec) {
fprintf(stderr, "Codec not foundn");
exit(1);
}
else printf("H264 codec foundn");
c = avcodec_alloc_context3(codec);
c->bit_rate = 400000;
c->width = 1920; // resolution must be a multiple of two (1280x720),(1900x1080),(720x480)
c->height = 1200;
c->time_base.num = 1; // framerate numerator
c->time_base.den = 25; // framerate denominator
c->gop_size = 10; // emit one intra frame every ten frames
c->max_b_frames = 1; // maximum number of b-frames between non b-frames
//c->keyint_min = 1; // minimum GOP size
//c->i_quant_factor = (float)0.71; // qscale factor between P and I frames
//c->b_frame_strategy = 20;
//c->qcompress = (float)0.6;
//c->qmin = 20; // minimum quantizer
//c->qmax = 51; // maximum quantizer
//c->max_qdiff = 4; // maximum quantizer difference between frames
//c->refs = 4; // number of reference frames
//c->trellis = 1; // trellis RD Quantization
c->pix_fmt = PIX_FMT_YUV420P;
c->codec_id = CODEC_ID_H264;
//c->codec_type = AVMEDIA_TYPE_VIDEO;
// Open the encoder
if (avcodec_open2(c, codec,NULL) < 0) {
fprintf(stderr, "Could not open codecn");
exit(1);
}
else printf("H264 codec openedn");
outbuf_size = 100000 + c->width*c->height*(32>>3);//*(32>>3); // alloc image and output buffer
outbuf = static_cast<uint8_t *>(malloc(outbuf_size));
printf("Setting buffer size to: %dn",outbuf_size);
FILE* f = fopen("example.mpg","wb");
if(!f) printf("x - Cannot open video file for writingn");
else printf("Opened video file for writingn");
// encode 5 seconds of video
for(i=0;i<STREAM_FRAME_RATE*STREAM_DURATION;i++) {
fflush(stdout);
screenCap();
int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);
uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes*sizeof(uint8_t));
AVFrame* inpic = avcodec_alloc_frame();
AVFrame* outpic = avcodec_alloc_frame();
outpic->pts = (int64_t)((float)i * (1000.0/((float)(c->time_base.den))) * 90);
avpicture_fill((AVPicture*)inpic, (uint8_t*)pPixels, PIX_FMT_RGB32, c->width, c->height); // Fill picture with image
avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c->width, c->height);
av_image_alloc(outpic->data, outpic->linesize, c->width, c->height, c->pix_fmt, 1);
inpic->data[0] += inpic->linesize[0]*(screenHeight-1); // Flipping frame
inpic->linesize[0] = -inpic->linesize[0]; // Flipping frame
struct SwsContext* fooContext = sws_getContext(screenWidth, screenHeight, PIX_FMT_RGB32, c->width, c->height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(fooContext, inpic->data, inpic->linesize, 0, c->height, outpic->data, outpic->linesize);
// encode the image
out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
printf("Encoding frame %3d (size=%5d)n", i, out_size);
fwrite(outbuf, 1, out_size, f);
delete [] pPixels;
av_free(outbuffer);
av_free(inpic);
av_free(outpic);
}
// get the delayed frames
for(; out_size; i++) {
fflush(stdout);
out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
printf("Writing frame %3d (size=%5d)n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
// add sequence end code to have a real mpeg file
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, f);
fclose(f);
avcodec_close(c);
free(outbuf);
av_free(c);
printf("Closed codec and Freedn");
尝试将文件保存为example.h264
,而不是example.mpg
。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- SFML纹理像播放器
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 如何收听Windows当前正在播放的声音?
- GStreamer在开始任何播放之前进行搜索
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何使用libav编写.mov文件,如果文件未正确完成,则可以播放
- C++新手,想知道如何使用VS code 2019播放音频文件
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 按列随机播放 2D 矢量
- 如何以编程方式将音频从任何录制设备路由到任何播放设备
- 使用 IMFSinkWriter 编码的视频的播放速度会根据宽度而变化
- 测试驱动开发 c++:如何将对象添加到向量中,将歌曲添加到播放列表并对其进行测试
- 如何在C++中播放声音?
- 旋转播放器模型以指向一个点
- 大声音频无法播放
- 怎么可能只让设备使用 pjsua2 捕获或播放
- 播放器未在程序中的"X"和"O"之间切换
- 为什么这个函数会循环播放?
- FFMpeg编码的视频将只在FFPlay中播放