-
-
Save dwbuiten/3d143b5acaec06743439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libavcodec/avcodec.h> | |
#include <libavformat/avformat.h> | |
int process_pkt(struct AVPacket *pkt) { | |
if (pkt->priv) | |
return 20; | |
return 40;; | |
} | |
int main() { | |
const char *filename = "test.mp4"; | |
int numstreams; | |
int vidx; | |
int i; | |
AVFormatContext *avfctx; | |
AVCodec *codec; | |
AVPacket pkt = { 0 }; | |
int ret; | |
av_register_all(); | |
avfctx = avformat_alloc_context(); | |
if (!avfctx) { | |
printf("Could not allocate format context.\n"); | |
return 1; | |
} | |
if ((ret = avformat_open_input(&avfctx, filename, NULL, NULL)) != 0) { | |
printf("Could not open context: %d\n", ret); | |
return 1; | |
} | |
if (avformat_find_stream_info(avfctx, NULL) < 0) { | |
printf("No streams?\n"); | |
return 1; | |
} | |
numstreams = avfctx->nb_streams; | |
vidx = -1; | |
for (i = 0; i < numstreams; i++) { | |
if (avfctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | |
vidx = i; | |
break; | |
} | |
} | |
if (vidx == -1) { | |
printf("No V stream.\n"); | |
return 1; | |
} | |
codec = avcodec_find_decoder(avfctx->streams[i]->codec->codec_id); | |
if (!codec) { | |
printf("No codec.\n"); | |
return 1; | |
} | |
if (avcodec_open2(avfctx->streams[vidx]->codec, codec, NULL) < 0) { | |
printf("Can't open codec.\n"); | |
return 1; | |
} | |
if (avformat_seek_file(avfctx, vidx, 0, 0, 0, AVSEEK_FLAG_FRAME) < 0) { | |
printf("Can't seek.\n"); | |
return 1; | |
} | |
avcodec_flush_buffers(avfctx->streams[vidx]->codec); | |
while (av_read_frame(avfctx, &pkt) == 0) { | |
if (process_pkt(&pkt) == 40) { | |
printf("WTF\n"); | |
return 1; | |
} | |
av_free_packet(&pkt); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment