简介
最近项目需要对实时流进行截图,网上找了些资料,整理了一份基于ffmpeg的视频截图代码,写个博客记录一下。
本份代码功能为每隔30帧截取一帧图片,我是在Ubuntu中使用,ffmpeg版本为2.6.1。
代码
getpic.h(由于是编译成动态库,加了个简单的头文件)
#include <stdio.h>
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavcodec/avcodec.h"
#include "libavutil/time.h"
int getPicsFromStream(char *filepath, char *outfile);
getpic.c
#include "getpic.h"
int SavetoJPEG(AVFrame *pFrameYUV,AVStream *pVStream,char *filepath,int width, int height){
AVFormatContext* pFormatCtx;
AVOutputFormat* fmt;
AVStream* video_st;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
AVPacket pkt;
int y_size;
int got_picture=0;
int size;
int ret=0;
int in_w=width,in_h=height; //YUV's width and height
char out_file[50]; //Output file
sprintf(out_file, "%s_%ld.jpg", filepath, av_gettime_relative());
pFormatCtx = avformat_alloc_context();
fmt = av_guess_format("mjpeg", NULL, NULL);
pFormatCtx->oformat = fmt;
if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){
printf("Couldn't open output file.");
return -1;
}
video_st = avformat_new_stream(pFormatCtx, 0);
video_st->time_base.num = pVStream->time_base.num;
video_st->time_base.den = pVStream->time_base.den;
if (video_st==NULL){
return -1;
}
pCodecCtx = video_st->codec;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = PIX_FMT_YUVJ420P;
pCodecCtx->width = in_w;
pCodecCtx->height = in_h;
pCodecCtx->time_base.num = pVStream->codec->time_base.num;
pCodecCtx->time_base.den = pVStream->codec->time_base.den;
//Output some information
av_dump_format(pFormatCtx, 0, out_file, 1);
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec){
printf("Codec not found.");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){
printf("Could not open codec.");
return -1;
}
pCodecCtx->qmin = pCodecCtx->qmax = 3;
pCodecCtx->flags|=CODEC_FLAG_QSCALE;
avformat_write_header(pFormatCtx,NULL);
y_size = pCodecCtx->width * pCodecCtx->height;
av_new_packet(&pkt,y_size*3);
//Encode
ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrameYUV, &got_picture);
if(ret < 0){
printf("Encode Error.\n");
return -1;
}
if (got_picture==1){
pkt.stream_index = video_st->index;
ret = av_write_frame(pFormatCtx, &pkt);
}
av_packet_unref(&pkt);
//Write Trailer
av_write_trailer(pFormatCtx);
printf("Encode Successful.\n");
avcodec_close(video_st->codec);
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
return 0;
}
int getPicsFromStream(char *filepath, char *outfile) {
AVFormatContext *pFormatCtx;
int i, videoindex,PictureSize;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVPacket packet;
int ret, got_picture;
int64_t time_,time_total;
struct SwsContext *pSwsCtx;
uint8_t *outBuff;
time_total = av_gettime_relative();
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
time_ = av_gettime_relative();
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
if(avformat_find_stream_info(pFormatCtx,NULL)<0){
printf("Couldn't find stream information.\n");
return -1;
}
videoindex=-1;
for(i=0; i<pFormatCtx->nb_streams; i++){
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
videoindex=i;
break;
}
}
if(videoindex == -1){
printf("Didn't find a video stream.\n");
return -1;
}
pCodecCtx=pFormatCtx->streams[videoindex]->codec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL){
printf("Codec not found.\n");
return -1;
}
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
printf("Could not open codec.\n");
return -1;
}
pFrame=av_frame_alloc();
if( pFrame == NULL)
{
printf("avframe malloc failed!\n");
return -1;
}
if(outBuff == NULL){
printf("av malloc failed!\n");
return -1;
}
time_ = av_gettime_relative() - time_;
printf("open file and find info, cost time: %0.3fs\n",time_/1000000.0);
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = NULL;
avpkt.size = 0;
time_ = av_gettime_relative();
int count = 0;
while (av_read_frame(pFormatCtx, &packet) >=0 ) {
if(packet.stream_index == videoindex ){
printf("video frame,count is %d\n", count);
if((ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet))<0){
printf("Decode Error!\n");
return -1;
}
if (count != 0) {
count = (count + 1) % 30;
av_packet_unref(&packet);
continue;
}
if(got_picture){
printf("decode success\n");
time_ = av_gettime_relative()-time_;
printf("read and decode frame, costs time: %0.3fs\n",time_/1000000.0);
time_ = av_gettime_relative();
if (SavetoJPEG(pFrame,pFormatCtx->streams[videoindex],outfile,pCodecCtx->width,pCodecCtx->height)<0) {
printf("Write Image Error!\n");
return -1;
}
time_ = av_gettime_relative()-time_;
printf("write frame, costs time: %0.3fs\n",time_/1000000.0);
count = (count + 1) % 30;
}
}
av_packet_unref(&packet);
}
sws_freeContext(pSwsCtx);
av_packet_unref(&avpkt);
av_free(pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
time_total = av_gettime_relative()-time_total;
printf("all done, costs time: %0.3fs\n",time_total/1000000.0);
return 0;
}
编译
编译命令:
gcc getpic.c -fPIC -shared -g -o libgetpic.so -lavformat -lavutil -lavcodec -lswscale
测试代码
#include <getpic.h>
int main()
{
return getPicsFromStream("/root/test1.flv", "/opt/test");
}