1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| void demux( const char* filepath ) { AVFormatContext* fmt_context = NULL; int video_index = -1; int audio_index = -1; int ret = avformat_open_input(&fmt_context, filepath, NULL, NULL); if (ret < 0) { char buf[1024] = { 0 }; av_strerror(ret, buf, sizeof(buf)); printf("open %s failed: %s\n", filepath, buf); return; } ret = avformat_find_stream_info(fmt_context, NULL); if (ret < 0) { char buf[1024] = { 0 }; av_strerror(ret, buf, sizeof(buf)); printf("avformat_find_stream_info %s failed: %s\n", filepath, buf); return; } av_dump_format(fmt_context, 0, filepath, 0); { printf("media->url: %s\n", fmt_context->url); printf("media->bit_rate: %lld\n", fmt_context->bit_rate); printf("media->nb_streams: %d\n", fmt_context->nb_streams); int total_seconds = (fmt_context->duration) / AV_TIME_BASE; int hours = total_seconds / 3600; int minutes = total_seconds % 3600 / 60; int seconds = total_seconds % 3600 % 60; printf("media->duration: %d-%d-%d\n", hours, minutes, seconds); printf("\n"); } for (auto i = 0; i < fmt_context->nb_streams; i++) { AVStream* is = fmt_context->streams[i]; if (AVMEDIA_TYPE_AUDIO == is->codecpar->codec_type) { audio_index = i; printf("stream index:%d\n", is->index); printf("audio sample_rate:%d Hz\n", is->codecpar->sample_rate); switch (is->codecpar->format){ case AV_SAMPLE_FMT_FLTP: printf("sampleformat:AV_SAMPLE_FMT_FLTP\n"); break; case AV_SAMPLE_FMT_S16P: printf("sampleformat:AV_SAMPLE_FMT_S16P\n"); break; default:break;} printf("audio channels: %d\n", is->codecpar->channels); switch (is->codecpar->codec_id){ case AV_CODEC_ID_AAC:printf("audio codec:AAC\n"); break; case AV_CODEC_ID_MP3:printf("audio codec:MP3\n"); break; default:printf("audio codec_id:%d\n", is->codecpar->codec_id);break;} if (is->duration != AV_NOPTS_VALUE) { int duration_audio_seconds = is->duration * av_q2d(is->time_base); printf("audio duration: %02d:%02d:%02d\n", duration_audio_seconds / 3600, (duration_audio_seconds % 3600) / 60, (duration_audio_seconds % 60)); } else { printf("duration unknown\n"); }; } else if (AVMEDIA_TYPE_VIDEO == is->codecpar->codec_type) { video_index = i; printf("stream index:%d\n", is->index); printf("video fps: %llf / s\n", av_q2d(is->avg_frame_rate)); switch (is->codecpar->codec_id) { case AV_CODEC_ID_MPEG4: printf("video codec:MPEG4\n"); break; case AV_CODEC_ID_H264: printf("video codec:H264\n"); break; default:printf("video codec_id:%d\n", is->codecpar->codec_id);break;} printf("video width:%d px height:%d px \n", is->codecpar->width, is->codecpar->height); if (is->duration != AV_NOPTS_VALUE) { int duration_video_seconds = is->duration * av_q2d(is->time_base); printf("video duration: %02d:%02d:%02d\n", duration_video_seconds / 3600, (duration_video_seconds % 3600) / 60, (duration_video_seconds % 60)); } else { printf("duration unknown\n"); }; } } printf("\n"); AVPacket* pkt = av_packet_alloc(); int frame_max = 10; do { int ret = av_read_frame(fmt_context, pkt); if (ret < 0){printf("av_read_frame end\n");break;} if (pkt->stream_index == video_index) { printf("A audio packet\n"); } else if (pkt->stream_index == audio_index) { printf("A video packet\n"); } printf("pts:%lld\n", pkt->pts); printf("dts:%lld\n", pkt->dts); printf("size:%d\n", pkt->size); printf("pos:%lld\n", pkt->pos); printf("duration:%llf\n", pkt->duration * av_q2d(fmt_context->streams[pkt->stream_index]->time_base)); av_packet_unref(pkt); printf("\n"); frame_max--; } while (frame_max > 0);
if (pkt){av_packet_free(&pkt);}
if (fmt_context){avformat_close_input(&fmt_context);} }
|