Skip to content

Instantly share code, notes, and snippets.

@leandromoreira
Last active October 29, 2017 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandromoreira/818962406b4cc53f44fbd7ab6422ad4b to your computer and use it in GitHub Desktop.
Save leandromoreira/818962406b4cc53f44fbd7ab6422ad4b to your computer and use it in GitHub Desktop.
int main(int argc, const char *argv[])
{
av_register_all();
AVFormatContext *pFormatContext = avformat_alloc_context();
avformat_open_input(&pFormatContext, argv[1], NULL, NULL);
avformat_find_stream_info(pFormatContext, NULL);
AVCodec *pCodec = NULL;
AVStream *pStream = NULL;
int video_stream_index = -1;
// find the first video stream
for (int i = 0; i < pFormatContext->nb_streams; i++)
{
video_stream_index = i;
pStream = pFormatContext->streams[i];
AVCodecParameters *pCodecParameters = pStream->codecpar;
if (pCodecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {
pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
break;
}
}
AVCodecContext *pCodecContext = avcodec_alloc_context3(pCodec);
avcodec_open2(pCodecContext, pCodec, NULL);
AVFrame *pFrame = av_frame_alloc();
AVPacket *pPacket = av_packet_alloc();
while (av_read_frame(pFormatContext, pPacket) >= 0) {
int response = avcodec_send_packet(pCodecContext, pPacket);
if (response < 0) {
printf("error sending packet");
return -1;
}
while (response >= 0) {
response = avcodec_receive_frame(pCodecContext, pFrame);
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
printf("error receiving frame");
return -1;
} else if (response < 0) {
printf("Error during decoding\n");
return -1;
}
printf("saving frame %3d\n", pCodecContext->frame_number);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment