一.AVStream
AVCodecContext *codec // 已過時,使用另一個 codecpar 結構體代替。
AVRational time_base // 時間基數。
int64_t duration // 總時長。流的總時長,該參數不可靠。
AVRational avg_frame_rate // 幀率。
AVCodecParameters *codecpar; // 包含音視頻參數的結構體。很重要,可以用來獲取音視頻參數中的寬度、高度、采樣率、編碼格式等信息。
二. AVCodecParameters
enum AVMediaType codec_type; // 編碼類型。說明這段流數據究竟是音頻還是視頻。
enum AVCodecID codec_id // 編碼格式。說明這段流的編碼格式,h264,MPEG4, MJPEG,etc...
uint32_t codecTag; // 一般不用
int format; // 格式。對於視頻來說指的就是像素格式(YUV420,YUV422...),對於音頻來說,指的就是音頻的采樣格式。
int width, int height; // 視頻的寬高,只有視頻有
uint64_t channel_layout; // 取默認值即可
int channels; // 聲道數
int sample_rate; // 樣本率
int frame_size; // 只針對音頻,一幀音頻的大小
例:打印視頻的寬高、編碼格式,音頻的采樣率等信息
AVFormatContext *ic = NULL; char path[] = "sdcard/1080.mp4"; // char path[] = "/sdcard/qingfeng.flv"; int ret = avformat_open_input(&ic, path, 0, 0); if (ret != 0) { LOGE("avformat_open_input() called failed: %s", av_err2str(ret)); return env->NewStringUTF(hello.c_str()); } LOGI("avformat_open_input() called success."); LOGI("duration is: %lld, nb_stream is: %d", ic->duration, ic->nb_streams); if (avformat_find_stream_info(ic, 0) >=0 ) { LOGI("duration is: %lld, nb_stream is: %d", ic->duration, ic->nb_streams); } /**幀率*/ int fps = 0; int videoStream = 0; int audioStream = 1; for (int i = 0; i < ic->nb_streams; i++) { AVStream *as = ic->streams[i]; if (as->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { LOGI("視頻數據"); videoStream = i; fps = (int)r2d(as->avg_frame_rate); LOGI("fps = %d, width = %d, height = %d, codecid = %d, format = %d", fps, as->codecpar->width, as->codecpar->height, as->codecpar->codec_id, as->codecpar->format); AVSampleFormat } else if (as->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { LOGI("音頻數據"); audioStream = i; LOGI("sample_rate = %d, channels = %d, sample_format = %d", as->codecpar->sample_rate, as->codecpar->channels, as->codecpar->format ); } }