ffmpeg中解码具体的某一帧,用到av_seek_frame,但是不清楚具体步骤是什么样的。
希望能给个具体的例子,看看到底要用到哪些函数
以下是ffmpeg中的seek的例子,在哪些地方加解码函数才能获得想要的具体某一帧的图像?
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "libavformat/avformat.h"
#undef exit
int main(int argc, char **argv)
{
const char *filename;
AVFormatContext *ic;
int i, ret, stream_id;
int64_t timestamp;
AVFormatParameters params, *ap= ¶ms;
memset(ap, 0, sizeof(params));
ap->channels=1;
ap->sample_rate= 22050;
/* initialize libavcodec, and register all codecs and formats */
av_register_all();
if (argc != 2) {
printf("usage: %s input_file\n"
"\n", argv[0]);
exit(1);
}
filename = argv[1];
/* allocate the media context */
ic = avformat_alloc_context();
if (!ic) {
fprintf(stderr, "Memory error\n");
exit(1);
}
ret = av_open_input_file(&ic, filename, NULL, 0, ap);
if (ret < 0) {
fprintf(stderr, "cannot open %s\n", filename);
exit(1);
}
ret = av_find_stream_info(ic);
if (ret < 0) {
fprintf(stderr, "%s: could not find codec parameters\n", filename);
exit(1);
}
for(i=0; ; i++){
AVPacket pkt;
AVStream *st;
memset(&pkt, 0, sizeof(pkt));
if(ret>=0){
ret= av_read_frame(ic, &pkt);
printf("ret:%2d", ret);
if(ret>=0){
st= ic->streams[pkt.stream_index];
printf(" st:%2d dts:%f pts:%f pos:%" PRId64 " size:%d flags:%d", pkt.stream_index, pkt.dts*av_q2d(st->time_base), pkt.pts*av_q2d(st->time_base), pkt.pos, pkt.size, pkt.flags);
}
printf("\n");
}
if(i>25) break;
stream_id= (i>>1)%(ic->nb_streams+1) - 1;
timestamp= (i*19362894167LL) % (4*AV_TIME_BASE) - AV_TIME_BASE;
if(stream_id>=0){
st= ic->streams[stream_id];
timestamp= av_rescale_q(timestamp, AV_TIME_BASE_Q, st->time_base);
}
ret = av_seek_frame(ic, stream_id, timestamp, (i&1)*AVSEEK_FLAG_BACKWARD);
printf("ret:%2d st:%2d ts:%f flags:%d\n", ret, stream_id, timestamp*(stream_id<0 ? 1.0/AV_TIME_BASE : av_q2d(st->time_base)), i&1);
}
return 0;
}
谢谢
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_1" name="aswift_1" style="box-sizing: content-box; left: 0px; position: absolute; top: 0px;">
-
id="iframeu1636200_0" src="http://pos.baidu.com/ocom?rdid=1636200&dc=2&di=u1636200&dri=0&dis=0&dai=3&ps=2782x660&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1475479734465&ti=ffmpeg%E4%B8%ADav_seek_frame%EF%BC%88%EF%BC%89%20%E7%9A%84%E9%97%AE%E9%A2%98-CSDN%E8%AE%BA%E5%9D%9B-CSDN.NET-%E4%B8%AD%E5%9B%BD%E6%9C%80%E5%A4%A7%E7%9A%84IT%E6%8A%80%E6%9C%AF%E7%A4%BE%E5%8C%BA&ari=2&dbv=2&drs=3&pcs=1899x913&pss=1899x13691&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1475479734&rw=913<u=http%3A%2F%2Fbbs.csdn.net%2Ftopics%2F350182063<r=http%3A%2F%2Fcn.bing.com%2Fsearch%3Fq%3Dav_seek_frame%26go%3D%25E6%258F%2590%25E4%25BA%25A4%26qs%3Dn%26form%3DQBLH%26pq%3Dav_seek_frame%26sc%3D0-13%26sp%3D-1%26sk%3D%26cvid%3D7D839F39BB574E2987E4CD68F1A469C1&ecd=1&psr=1920x1080&par=1920x1032&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1475479735&qn=5a42deca2c873440&tt=1475479734163.651.722.723" width="200" height="22" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: content-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
-
id="iframeu1636201_0" src="http://pos.baidu.com/ocom?rdid=1636201&dc=2&di=u1636201&dri=0&dis=0&dai=4&ps=2782x914&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1475479734465&ti=ffmpeg%E4%B8%ADav_seek_frame%EF%BC%88%EF%BC%89%20%E7%9A%84%E9%97%AE%E9%A2%98-CSDN%E8%AE%BA%E5%9D%9B-CSDN.NET-%E4%B8%AD%E5%9B%BD%E6%9C%80%E5%A4%A7%E7%9A%84IT%E6%8A%80%E6%9C%AF%E7%A4%BE%E5%8C%BA&ari=2&dbv=2&drs=3&pcs=1899x913&pss=1899x13691&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1475479734&rw=913<u=http%3A%2F%2Fbbs.csdn.net%2Ftopics%2F350182063<r=http%3A%2F%2Fcn.bing.com%2Fsearch%3Fq%3Dav_seek_frame%26go%3D%25E6%258F%2590%25E4%25BA%25A4%26qs%3Dn%26form%3DQBLH%26pq%3Dav_seek_frame%26sc%3D0-13%26sp%3D-1%26sk%3D%26cvid%3D7D839F39BB574E2987E4CD68F1A469C1&ecd=1&psr=1920x1080&par=1920x1032&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1475479735&qn=4adbd6904d9f4324&tt=1475479734163.652.726.727" width="200" height="22" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: content-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
-
id="iframeu1636204_0" src="http://pos.baidu.com/ocom?rdid=1636204&dc=2&di=u1636204&dri=0&dis=0&dai=5&ps=2782x1168&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1475479734465&ti=ffmpeg%E4%B8%ADav_seek_frame%EF%BC%88%EF%BC%89%20%E7%9A%84%E9%97%AE%E9%A2%98-CSDN%E8%AE%BA%E5%9D%9B-CSDN.NET-%E4%B8%AD%E5%9B%BD%E6%9C%80%E5%A4%A7%E7%9A%84IT%E6%8A%80%E6%9C%AF%E7%A4%BE%E5%8C%BA&ari=2&dbv=2&drs=3&pcs=1899x913&pss=1899x13691&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1475479734&rw=913<u=http%3A%2F%2Fbbs.csdn.net%2Ftopics%2F350182063<r=http%3A%2F%2Fcn.bing.com%2Fsearch%3Fq%3Dav_seek_frame%26go%3D%25E6%258F%2590%25E4%25BA%25A4%26qs%3Dn%26form%3DQBLH%26pq%3Dav_seek_frame%26sc%3D0-13%26sp%3D-1%26sk%3D%26cvid%3D7D839F39BB574E2987E4CD68F1A469C1&ecd=1&psr=1920x1080&par=1920x1032&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1475479735&qn=e1cee76fcbf6f3ed&tt=1475479734163.653.728.728" width="200" height="22" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: content-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">