(转载)FFMpeg取一张图片
int writeJPEG(AVFrame* frame, int width, int height){ const char* out_file = "hello_world.jpg"; //新建一个输出的AVFormatContext 并分配内存 AVFormatContext* output_cxt = avformat_alloc_context(); avformat_alloc_output_context2(&output_cxt, NULL, "singlejpeg", out_file); //设置输出文件的格式 // output_cxt->oformat = av_guess_format("mjpeg",NULL,NULL); //创建和初始化一个和该URL相关的AVIOContext if (avio_open(&output_cxt->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){ av_log(NULL, AV_LOG_ERROR, "不能打开文件 \n"); return -1; } //构建新的Stream AVStream* stream = avformat_new_stream(output_cxt, NULL); if (stream == NULL){ av_log(NULL, AV_LOG_ERROR, "创建AVStream失败 \n"); return -1; } //初始化AVStream信息 AVCodecContext* codec_cxt = stream->codec; codec_cxt->codec_id = output_cxt->oformat->video_codec; codec_cxt->codec_type = AVMEDIA_TYPE_VIDEO; codec_cxt->pix_fmt = AV_PIX_FMT_YUVJ420P; codec_cxt->height = height; codec_cxt->width = width; codec_cxt->time_base.num = 1; codec_cxt->time_base.den = 25; //打印输出文件信息 av_dump_format(output_cxt, 0, out_file, 1); AVCodec* codec = avcodec_find_encoder(codec_cxt->codec_id); if (!codec){ av_log(NULL, AV_LOG_ERROR, "找不到编码器 \n"); return -1; } if (avcodec_open2(codec_cxt, codec, NULL) < 0){ av_log(NULL, AV_LOG_ERROR, "不能打开编码器 \n"); return -1; } avcodec_parameters_from_context(stream->codecpar, codec_cxt); //写入文件头 avformat_write_header(output_cxt, NULL); int size = codec_cxt->width * codec_cxt->height; AVPacket* packet; //av_new_packet(packet, size * 3); av_new_packet(packet, size); int got_picture = 0; int result = avcodec_encode_video2(codec_cxt, packet, frame, &got_picture); if (result < 0){ av_log(NULL, AV_LOG_ERROR, "编码失败 \n"); return -1; } printf("got_picture %d \n", got_picture); if (got_picture == 1){ //将packet中的数据写入本地文件 result = av_write_frame(output_cxt, packet); } av_free_packet(packet); //将流尾写入输出媒体文件并释放文件数据 av_write_trailer(output_cxt); if (frame){ av_frame_unref(frame); } avio_close(output_cxt->pb); avformat_free_context(output_cxt); return 0; } void FFmpegDemo::on_pictureBtn1_clicked() { // char* filename = "./jichi.mp4"; //如果本地没有视频可以使用这个直播流地址 char* filename = "./2.mp4"; AVFormatContext* avf_cxt = avformat_alloc_context(); int ret = avformat_open_input(&avf_cxt, filename, NULL, NULL); if (ret < 0){ av_log(NULL, AV_LOG_ERROR, "不能打开文件\n"); return; } ret = avformat_find_stream_info(avf_cxt, NULL); if (ret < 0){ av_log(NULL, AV_LOG_ERROR, "找不到流数据\n"); goto _end; } //打印视频信息 av_dump_format(avf_cxt, 0, filename, 0); int video_index = -1; for (int i = 0; i < avf_cxt->nb_streams; i++){ if (avf_cxt->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){ video_index = i; break; } } if (video_index == -1){ av_log(NULL, AV_LOG_ERROR, "没有找到视频流\n"); goto _end; } AVCodecContext* avc_cxt = avf_cxt->streams[video_index]->codec; enum AVCodecID codecId = avc_cxt->codec_id; AVCodec* codec = avcodec_find_decoder(codecId); if (!codec){ av_log(NULL, AV_LOG_ERROR, "没有找到解码器\n"); goto _end; } ret = avcodec_open2(avc_cxt, codec, NULL); if (ret < 0){ av_log(NULL, AV_LOG_ERROR, "解码器无法打开\n"); goto _end; } //为avpacket分配内存 AVPacket* packet = av_packet_alloc(); //为avFrame分配内存 AVFrame* frame = av_frame_alloc(); while (av_read_frame(avf_cxt, packet) >= 0){ if (packet && packet->stream_index == video_index){ int gotFrame = 0; ret = avcodec_decode_video2(avc_cxt, frame, &gotFrame, packet); if (gotFrame){ ret = writeJPEG(frame, avc_cxt->width, avc_cxt->height); if (ret == 0){ break; } } } } _end: av_frame_free(&frame); avcodec_close(avc_cxt); avformat_free_context(avf_cxt); // return 0; }
来自:
https://www.iteye.com/blog/cmzx3444-965792
https://zhuanlan.zhihu.com/p/37697332
https://blog.csdn.net/qqqq245425070/article/details/87529209
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!