首页 > 其他分享 >ffmpeg把读取的视频流保存为jpeg文件

ffmpeg把读取的视频流保存为jpeg文件

时间:2023-08-22 16:15:46浏览次数:50  
标签:pCodeCtx return ffmpeg parameters avcodec 视频流 jpeg ret pFormatCtx

int img_savejpeg(AVFrame *pFrame, char *out_filename) {
//视频流保存为jpeg

int width = pFrame->width;
int height = pFrame->height;
AVCodecContext *pCodeCtx = NULL;

AVFormatContext *pFormatCtx = avformat_alloc_context();
// 设置输出文件格式
pFormatCtx->oformat = av_guess_format(“mjpeg”, NULL, NULL);

// 创建并初始化输出AVIOContext
if (avio_open(&pFormatCtx->pb, out_filename, AVIO_FLAG_READ_WRITE) < 0) {
printf(“Couldn’t open output file.”);
return -1;
}

// 构建一个新stream
AVStream *pAVStream = avformat_new_stream(pFormatCtx, 0);
if (pAVStream == NULL) {
return -1;
}

AVCodecParameters *parameters = pAVStream->codecpar;
parameters->codec_id = pFormatCtx->oformat->video_codec;
parameters->codec_type = AVMEDIA_TYPE_VIDEO;
parameters->format = AV_PIX_FMT_YUVJ420P;
parameters->width = pFrame->width;
parameters->height = pFrame->height;

AVCodec *pCodec = avcodec_find_encoder(pAVStream->codecpar->codec_id);

if (!pCodec) {
printf(“Could not find encoder\n”);
return -1;
}

pCodeCtx = avcodec_alloc_context3(pCodec);
if (!pCodeCtx) {
fprintf(stderr, “Could not allocate video codec context\n”);
exit(1);
}

if ((avcodec_parameters_to_context(pCodeCtx, pAVStream->codecpar)) < 0) {
fprintf(stderr, “Failed to copy %s codec parameters to decoder context\n”,
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return -1;
}
AVRational Rat;
Rat.num =1;
Rat.den =25;
pCodeCtx->time_base = Rat;

if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) {
printf(“Could not open codec.”);
return -1;
}

int ret = avformat_write_header(pFormatCtx, NULL);
if (ret < 0) {
printf(“write_header fail\n”);
return -1;
}

int y_size = width * height;

//Encode
// 给AVPacket分配足够大的空间
AVPacket pkt;
av_new_packet(&pkt, y_size * 3);

// 编码数据
ret = avcodec_send_frame(pCodeCtx, pFrame);
if (ret < 0) {
printf(“Could not avcodec_send_frame.”);
return -1;
}

// 得到编码后数据
ret = avcodec_receive_packet(pCodeCtx, &pkt);
if (ret < 0) {
printf(“Could not avcodec_receive_packet”);
return -1;
}

ret = av_write_frame(pFormatCtx, &pkt);

if (ret < 0) {
printf(“Could not av_write_frame”);
return -1;
}

av_packet_unref(&pkt);

//Write Trailer
av_write_trailer(pFormatCtx);

avcodec_close(pCodeCtx);
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);

return 0;

}

标签:pCodeCtx,return,ffmpeg,parameters,avcodec,视频流,jpeg,ret,pFormatCtx
From: https://www.cnblogs.com/lidabo/p/17648753.html

相关文章

  • 使用ffmpeg将MP4文件的每一帧保存为jpg图片
    #include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<fcntl.h>#include<sys/ioctl.h>#include<string.h>#include<sys/mman.h>#include<assert.h>#include<liba......
  • 国标GB28181视频平台EasyGBS国标平台添加针对H.265视频流的告警信息的具体操作流程
    EasyGBS国标视频云服务支持设备/平台通过国标GB28181协议注册接入,可实现视频的实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。平台支持将接入的视频流进行全终端、全平台分发,分发的视频流包括RTSP、RTMP、FLV、HLS、WebRTC等格式。在EasyGBS平台中接入......
  • windows 32位系统和64位系统ffmpeg下载
    64位下载Releases·BtbN/FFmpeg-Builds(github.com)  32位下载https://github.com/sudo-nautilus/FFmpeg-Builds-Win32/releases/tag/latest  ......
  • 优化视频流:利用美颜SDK提升直播质量的方法
    随着互联网的迅猛发展,视频直播已成为人们分享、交流和娱乐的重要方式。然而,在实际的直播过程中,视频画质可能受到诸多因素的影响,例如摄像头品质、网络状况等。为了提升观众的体验和吸引更多的观众,美颜技术逐渐成为了直播平台不可或缺的一部分。本文将探讨如何利用美颜SDK来优化视频......
  • Win11+ VS2022编译 FFmpeg6.0 静态库
    目录编译前言为什么项目编译?前期准备环境配置ffmpeg外部库额外的编译选项-for渲染opengl(需要glext)ffnvcodec(需要nv-codec-headers)AMFsdk头文件编译工具链开始编译step1.选择编译类型Debug/ReleaseDll/libstep2.打包SDK验证结尾参考链接编译前言编译作为自己持有......
  • 在html5中播放RTSP/RTMP/HLS/HTTP视频流媒体的几种方案,并支持H.265
    经过多年的项目实战和研发经验的积累,总结了一下对于H5视频可视化在视频播放上如何做到无插件H5展示的方法,尤其是契合安防行业的方案;除了HTTP、WebSocket类的传输协议,其他是无法通用地传输到浏览器的,所以,如果要做一款通用的H5视频播放器,基本上就是一款HTTP/WebSocket协议的视频播放......
  • Qt+rtsp+ffmpeg 播放声音
    #include<QtCore/QCoreApplication>#include<QtMultimedia/QAudioFormat>#include<QtMultimedia/QAudioOutput>#include<QtCore/QFile>#include<iostream>#ifdef__cplusplusextern"C"{#endif//__cplusplus#include"li......
  • [Multimedia][ffmpeg] 音频音量获取
    ffmpeg-i~/media/test.mp4-filter_complexvolumedetect-c:vcopy-fnull/dev/null...[Parsed_volumedetect_0@0x7f83a481c000]n_samples:23887872[Parsed_volumedetect_0@0x7f83a481c000]mean_volume:-16.7dB[Parsed_volumedetect_0@0x7f83a481c000]......
  • Windows上使用FFmpeg实现本地视频推送模拟海康协议rtsp视频流
    场景Nginx搭建RTMP服务器+FFmpeg实现海康威视摄像头预览:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/121202130上面记录的是使用FFmpeg拉取海康协议摄像头的rtsp流并推流到流媒体服务器。如果在其它业务场景下需要本地的视频文件模拟海康的rtsp流协议格式进行......
  • ffmpeg 之 sdl
    使用ffmpeg解码视频渲染到sdl窗口前言使用ffmpeg解码视频并渲染视频到窗口,网上是有不少例子的,但是大部分例子的细节都不是很完善,比如资源释放、flush解码缓存、多线程优化等都没有。特别是想要快速搭建一个demo时,总是要重新编写不少代码,比较不方便,所以在这里提供一个完善的例子,......