ffmpeg 从视频流中抓取图片
2017-09-29 00:00
756 查看
从视频中不断抓取图片的基本流程:打开视频流地址->获取视频流packt->解码成图片帧->输出图片
一.初始化Ffmpeg
如果你不想输出log,设置log级别为AV_LOG_PANIC。
二.打开视频。
三 .读取视频包:
说明一下:不一定要用智能指针。我确定这样写不会有内存泄露,所以就不改了,随手写的代码会有bug。
四. 解码
1. 初始化解码器
2. 解码视频包
输出图片:
context是全局变量,如果有问题,加群流媒体/Ffmpeg/音视频 127903734进行交流
视频地址:http://pan.baidu.com/s/1jH4dYN8
源码下载地址:http://pan.baidu.com/s/1o8Lkozw
一.初始化Ffmpeg
void ffmpegInit() { av_register_all(); avformat_network_init(); av_log_set_level(AV_LOG_ERROR); }
如果你不想输出log,设置log级别为AV_LOG_PANIC。
二.打开视频。
int Open(char* url) { context = avformat_alloc_context(); context->interrupt_callback.opaque = this; //C++ context->interrupt_callback.callback = interruptCallback;//设置回调函数,否则有可能ffmpeg一直被挂住。 context->start_time_realtime = av_gettime(); AVDictionary* options = nullptr; av_dict_set(&options, "rtsp_transport", "udp", 0); //以udp方式打开,如果以tcp方式打开将udp替换为tcp av_dict_set(&options, "stimeout", "3000000", 0); //设置超时断开连接时间 int ret = avformat_open_input(&context, url, nullptr, &options); //avformat_open_input 返回0表示open成功,小于0表示open失败 if(ret < 0) return ret; ret = avformat_find_stream_info(context, options); ///avformat_find_stream_info 返回0表示查抄stream info成功 小于0表示失败。 if(options!= nullptr) { av_dict_free(options); } return ret; }
int interrupt_cb(void *ctx) { if((av_gettime() - ffmpeg->lastFrameRealtime) > 10 * 1000 * 1000) //10s超时退出 { return AVERROR_EOF; } return 0; }
三 .读取视频包:
shared_ptr<AVPacket> ReadPacket() { shared_ptr<AVPacket> packet((AVPacket*)av_malloc(sizeof(AVPacket)), [&](AVPacket *p){av_free_packet(p);av_freep(&p);}); av_init_packet(packet.get()); lastFrameRealtime = av_gettime(); int ret = av_read_frame(context, packet.get()); if(ret >= 0) { return packet; } else { return nullptr; } }
说明一下:不一定要用智能指针。我确定这样写不会有内存泄露,所以就不改了,随手写的代码会有bug。
四. 解码
1. 初始化解码器
InitDecoderCodec { int ret = -1; for(int i = 0; i < context->nb_streams; ++i) { AVCodecContext *codecContext = context->streams[i]->codec; if(codecContext->codec_type == AVMEDIA_TYPE_VIDEO) { //返回小于0,打开解码器失败 ret = avcodec_open2(codecContext, avcodec_find_decoder(codecContext->codec_id), &options); } } return ret; }
2. 解码视频包
AVFrame* DecodeVideoPacket(AVCodecContext* codecContext) { AVFrame* videoFrame = av_frame_alloc(); auto hr = avcodec_decode_video2(codecContext, frame, &gotFrame, packet); if(hr >= 0 && gotFrame != 0) { return videoFrame; } else { avcodec_free_frame(&videoFrame); return nullptr } }
输出图片:
uint8_t *GetPictureData(int width,int height, int *bufferSize) { pFrameYUV= av_frame_alloc(); uint8_t *out_buffer; out_buffer = new uint8_t[avpicture_get_size(PIX_FMT_RGB32, width, height)]; avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_RGB32, width, height); sws_scale(sws, (const uint8_t* const*)frame->data, frame->linesize, 0, height, pFrameYUV->data, pFrameYUV->linesize); *bufferSize = width * height * 4; return pFrameYUV->data[0]; }
context是全局变量,如果有问题,加群流媒体/Ffmpeg/音视频 127903734进行交流
视频地址:http://pan.baidu.com/s/1jH4dYN8
源码下载地址:http://pan.baidu.com/s/1o8Lkozw
相关文章推荐
- ffmpeg.exe抓取视频流图片
- ffmpeg 从视频流中抓取图片
- QT开发用ffmpeg将图片制作成视频
- python 调用系统ffmpeg进行视频截图,并进行图片http发送
- ffmpeg 查看YUV图片/视频
- 用FFmpeg从视频截取任意一帧图片的解决办法~
- (工具ffmpeg)视频转换为flv格式,视频截取图片
- 利用ffmpeg截取视频图像并保存为ppm和jpg图片格式
- ffmpeg 解析RTSP视频流并间隔保存成视频文件、图片
- ffmpeg视频转换为flv格式,视频截取图片
- ASP.net 视频上传转换flv并且抓取第一帧生成图片源码
- 使用ffmpeg来将mp4视频转换成gif格式图片
- DirectShow:图片的抓取---从摄像头流中捕捉一张图片zzDirectshow中的视频捕捉
- 把图片转换成视频-ffmpeg
- # ffmpeg 将多幅图片压缩成视频 h264 avi
- 利用ffmpeg提取视频中的图片
- javaCV开发详解之4:转流器实现(也可作为本地收流器、推流器,新增添加图片及文字水印,视频图像帧保存),实现rtsp/rtmp/本地文件转发到rtmp流媒体服务器(基于javaCV-FFMPEG)
- 利用ffmpeg对视频进行处理,比如截取图片作为缩略图
- ffmpeg(ubuntu16.04):视频抽成帧(图片)/图片合成视频
- 把图片转换成视频-ffmpeg