#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/mathematics.h>
#include <libavutil/time.h>
#include <libavutil/opt.h>
int is_h264_file(char *filename)
{
AVFormatContext *ifmt_ctx = NULL;
AVDictionary *input_options = NULL;
int ret = 0;
av_register_all();
avformat_network_init();
if ((ret = av_dict_set(&input_options, "rtmp_live", "live", 0)) < 0) {
printf("Failed to set rtmp_live mode for server\n");
}
if ((ret = av_dict_set(&input_options, "rw_timeout", "10000", 0)) < 0) {
printf("failed to set rw_timeout value(10 seconds) for server\n");
}
if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, &input_options)) < 0) {
printf( "Could not open input file.");
return 0;
}
printf("ifmt_ctx->video_codec_id:%d\n",ifmt_ctx->video_codec_id);
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
printf( "Failed to retrieve input stream information");
avformat_close_input(&ifmt_ctx);
return 0;
}
//ifmt_ctx->streams[0]->codecpar->codec_id;
av_dump_format(ifmt_ctx, 0, filename, 0);
int videoStream = -1;
for (int i = 0; i < ifmt_ctx->nb_streams; i++)
{
if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
if (videoStream == -1)
return -1; // 没有查找到视频流video stream
printf("3ifmt_ctx->video_codec_id:%d\n",ifmt_ctx->streams[videoStream]->codecpar->codec_id);
if(ifmt_ctx->streams[videoStream]->codecpar->codec_id==AV_CODEC_ID_H264)
{
printf("is h264 file\n");
avformat_close_input(&ifmt_ctx);
return 1;
}
else if (ifmt_ctx->streams[videoStream]->codecpar->codec_id==AV_CODEC_ID_HEVC)
{
printf("is hevc file\n");
avformat_close_input(&ifmt_ctx);
return 1;
}
else
{
printf("is not h264/hevc file\n");
avformat_close_input(&ifmt_ctx);
return 0;
}
}
int main(int argc,char *argv[])
{
int ret = is_h264_file(argv[1]);
printf("ret : %d", ret);
return 0;
}
linux下编译:gcc -g isH264.c -o isH264 -I ./include -L ./lib -lavformat -lavcodec -lavutil -lswscale
测试使用:./isH264 rtmp://127.0.0.1:1935/live/rtmp