ffmpeg 报错Encoder (codec h264) not found for output stream #0:0

使用ffmpeg 制作流媒体的视频文件,同样的命令在本地的windows环境是正常的,在linux 上就不行了。

报错了

根据最后一行的提示,

Encoder (codec h264) not found for output stream #0:0

大概可以确定是缺少了h264编码器 。

本地window是下载编译好的ffmpeg 直接使用的,但是linux是自己编译的,肯定是配置不一致导致的。我在linux 使用的的版本是

windows 使用的版本和配置是

所以现在的关键编程了需要配置一个  --enable-libx264 

另外还有一个思路,这个 ffmpeg 的编译有点复杂,可能还有他的问题,所以直接下载一个静态编译版本,具体参考这个博客就可以了:



Linux安装已编译好的FFmpeg,基于centos7 - 凉城 - 博客园

还有这个网站

John Van Sickle - FFmpeg Static Builds

 查看一下解压后的内容和配置

再试一下:

一切正常!

以下是合成两张图片的完整 C 代码,使用了 FFmpeg 库: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <libavutil/imgutils.h> #include <libavutil/samplefmt.h> #include <libavutil/timestamp.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #define WIDTH 640 #define HEIGHT 480 int main(int argc, char **argv) { AVFormatContext *fmt_ctx = NULL; AVCodecContext *codec_ctx = NULL; AVCodec *codec = NULL; AVFrame *frame = NULL, *frame2 = NULL, *frame_out = NULL; AVPacket pkt; int ret, i, j, k; uint8_t *buffer = NULL; int buffer_size; int video_stream_index = -1; int frame_count = ; int64_t start_time = ; struct SwsContext *sws_ctx = NULL; if (argc < 3) { fprintf(stderr, "Usage: %s input1 input2 output\n", argv[]); exit(1); } av_register_all(); if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)) < ) { fprintf(stderr, "Could not open input file '%s'\n", argv[1]); goto end; } if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < ) { fprintf(stderr, "Could not find stream information\n"); goto end; } av_dump_format(fmt_ctx, , argv[1], ); for (i = ; i < fmt_ctx->nb_streams; i++) { if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { fprintf(stderr, "Could not find video stream\n"); goto end; } codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id); if (!codec) { fprintf(stderr, "Codec not found\n"); goto end; } codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); goto end; } if ((ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar)) < ) { fprintf(stderr, "Could not copy codec parameters to codec context\n"); goto end; } if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < ) { fprintf(stderr, "Could not open codec\n"); goto end; } frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate frame\n"); goto end; } frame2 = av_frame_alloc(); if (!frame2) { fprintf(stderr, "Could not allocate frame\n"); goto end; } frame_out = av_frame_alloc(); if (!frame_out) { fprintf(stderr, "Could not allocate frame\n"); goto end; } buffer_size = av_image_get_buffer_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); buffer = av_malloc(buffer_size); if (!buffer) { fprintf(stderr, "Could not allocate buffer\n"); goto end; } av_image_fill_arrays(frame->data, frame->linesize, buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); if (!sws_ctx) { fprintf(stderr, "Could not initialize sws context\n"); goto end; } if ((ret = avformat_open_input(&fmt_ctx, argv[2], NULL, NULL)) < ) { fprintf(stderr, "Could not open input file '%s'\n", argv[2]); goto end; } if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < ) { fprintf(stderr, "Could not find stream information\n"); goto end; } av_dump_format(fmt_ctx, , argv[2], ); for (i = ; i < fmt_ctx->nb_streams; i++) { if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { fprintf(stderr, "Could not find video stream\n"); goto end; } codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id); if (!codec) { fprintf(stderr, "Codec not found\n"); goto end; } codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); goto end; } if ((ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar)) < ) { fprintf(stderr, "Could not copy codec parameters to codec context\n"); goto end; } if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < ) { fprintf(stderr, "Could not open codec\n"); goto end; } buffer_size = av_image_get_buffer_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); buffer = av_malloc(buffer_size); if (!buffer) { fprintf(stderr, "Could not allocate buffer\n"); goto end; } av_image_fill_arrays(frame2->data, frame2->linesize, buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); if (!sws_ctx) { fprintf(stderr, "Could not initialize sws context\n"); goto end; } if ((ret = avformat_alloc_output_context2(&fmt_ctx, NULL, NULL, argv[3])) < ) { fprintf(stderr, "Could not create output context\n"); goto end; } codec = avcodec_find_encoder(fmt_ctx->oformat->video_codec); if (!codec) { fprintf(stderr, "Codec not found\n"); goto end; } codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); goto end; } codec_ctx->width = WIDTH; codec_ctx->height = HEIGHT; codec_ctx->time_base = (AVRational){1, 25}; codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < ) { fprintf(stderr, "Could not open codec\n"); goto end; } AVStream *stream = avformat_new_stream(fmt_ctx, codec); if (!stream) { fprintf(stderr, "Could not allocate stream\n"); goto end; } if ((ret = avcodec_parameters_from_context(stream->codecpar, codec_ctx)) < ) { fprintf(stderr, "Could not copy codec parameters to stream\n"); goto end; } if ((ret = avio_open(&fmt_ctx->pb, argv[3], AVIO_FLAG_WRITE)) < ) { fprintf(stderr, "Could not open output file '%s'\n", argv[3]); goto end; } if ((ret = avformat_write_header(fmt_ctx, NULL)) < ) { fprintf(stderr, "Could not write header\n"); goto end; } while (1) { if ((ret = av_read_frame(fmt_ctx, &pkt)) < ) break; if (pkt.stream_index == video_stream_index) { ret = avcodec_send_packet(codec_ctx, &pkt); if (ret < ) { fprintf(stderr, "Error sending packet to decoder\n"); goto end; } while (ret >= ) { ret = avcodec_receive_frame(codec_ctx, frame_out); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < ) { fprintf(stderr, "Error receiving frame from decoder\n"); goto end; } if (frame_count == ) { av_image_fill_arrays(frame->data, frame->linesize, pkt.data, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); } else if (frame_count == 1) { av_image_fill_arrays(frame2->data, frame2->linesize, pkt.data, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); } frame_count++; if (frame_count == 2) { frame_count = ; av_image_fill_arrays(frame_out->data, frame_out->linesize, buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); sws_scale(sws_ctx, (const uint8_t *const *)frame->data, frame->linesize, , codec_ctx->height, frame_out->data, frame_out->linesize); sws_scale(sws_ctx, (const uint8_t *const *)frame2->data, frame2->linesize, , codec_ctx->height, frame_out->data, frame_out->linesize); frame_out->pts = av_rescale_q(frame_out->pts, fmt_ctx->streams[video_stream_index]->time_base, stream->time_base); frame_out->pkt_dts = av_rescale_q(frame_out->pkt_dts, fmt_ctx->streams[video_stream_index]->time_base, stream->time_base); frame_out->pkt_duration = av_rescale_q(frame_out->pkt_duration, fmt_ctx->streams[video_stream_index]->time_base, stream->time_base); ret = avcodec_send_frame(codec_ctx, frame_out); if (ret < ) { fprintf(stderr, "Error sending frame to encoder\n"); goto end; } while (ret >= ) { ret = avcodec_receive_packet(codec_ctx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < ) { fprintf(stderr, "Error receiving packet from encoder\n"); goto end; } pkt.stream_index = stream->index; av_packet_rescale_ts(&pkt, codec_ctx->time_base, stream->time_base); ret = av_interleaved_write_frame(fmt_ctx, &pkt); if (ret < ) { fprintf(stderr, "Error writing packet to output file\n"); goto end; } av_packet_unref(&pkt); } } } } av_packet_unref(&pkt); } ret = av_write_trailer(fmt_ctx); if (ret < ) { fprintf(stderr, "Error writing trailer\n"); goto end; } end: avformat_close_input(&fmt_ctx); avcodec_free_context(&codec_ctx); av_frame_free(&frame); av_frame_free(&frame2); av_frame_free(&frame_out); av_free(buffer); sws_freeContext(sws_ctx); if (ret < ) { fprintf(stderr, "Error occurred: %s\n", av_err2str(ret)); return 1; } return ; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值