FFMPEG学习----打印视频信息
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                FFMPEG学习----打印视频信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                FFMPEG學習資料少之又少,在此推薦雷神的博客:
http://blog.csdn.net/leixiaohua1020
在這里,我們把打印視頻里的相關信息作為學習FFMPEG的 Hello World程序。
#include <stdio.h> #include <string.h>extern "C" { #include "libavformat/avformat.h" #include "libavutil/dict.h" };#pragma comment(lib, "avformat.lib") #pragma comment(lib, "avutil.lib") #pragma comment(lib, "avcodec.lib")int main() {AVFormatContext *pFormatCtx = NULL;AVCodecContext *pCodecCtx = NULL;AVCodec *pCodec;AVDictionaryEntry *dict = NULL;AVInputFormat *pInputFormat = NULL;int iHour, iMinute, iSecond, iTotalSeconds;//HH:MM:SSint videoIndex, audioIndex;char *fileName = "bad.mp4";//char *fileName = "Titanic.ts";av_register_all();//注冊所有組件if (avformat_open_input(&pFormatCtx, fileName, NULL, NULL) != 0)//打開輸入視頻文件{printf("Couldn't open input stream.\n");return -1;}if (avformat_find_stream_info(pFormatCtx, NULL) < 0){printf("Couldn't find stream information.\n");return -1;}videoIndex = -1;for (int i = 0; i < pFormatCtx->nb_streams; i++){if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)//查找音頻{videoIndex = i;break;}}if (videoIndex == -1){printf("Couldn't find a video stream.\n");return -1;}pCodecCtx = pFormatCtx->streams[videoIndex]->codec; //指向AVCodecContext的指針pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //指向AVCodec的指針.查找解碼器if (pCodec == NULL){printf("Codec not found.\n");return -1;}//打開解碼器if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){printf("Could not open codec.\n");return -1;}audioIndex = -1;for (int i = 0; i < pFormatCtx->nb_streams; i++){if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO){audioIndex = i;break;}}if (audioIndex == -1){printf("Couldn't find a audio stream.\n");return -1;}//打印結構體信息puts("AVFormatContext信息:");puts("---------------------------------------------");printf("文件名:%s\n", pFormatCtx->filename);iTotalSeconds = (int)pFormatCtx->duration / 1000000;iHour = iTotalSeconds / 3600;//小時iMinute = iTotalSeconds % 3600 / 60;//分鐘iSecond = iTotalSeconds % 60;//秒printf("持續時間:%02d:%02d:%02d\n", iHour, iMinute, iSecond);printf("平均混合碼率:%d kb/s\n", pFormatCtx->bit_rate / 1000);printf("視音頻個數:%d\n", pFormatCtx->nb_streams);puts("---------------------------------------------");puts("FFMPEG支持的所有的輸入文件格式:");puts("---------------------------------------------");pInputFormat = pFormatCtx->iformat;while (pInputFormat){printf("%s ", pInputFormat->name);pInputFormat = pInputFormat->next;}puts("\n---------------------------------------------");puts("AVInputFormat信息:");puts("---------------------------------------------");printf("封裝格式名稱:%s\n", pFormatCtx->iformat->name);printf("封裝格式長名稱:%s\n", pFormatCtx->iformat->long_name);printf("封裝格式擴展名:%s\n", pFormatCtx->iformat->extensions);printf("封裝格式ID:%d\n", pFormatCtx->iformat->raw_codec_id);puts("---------------------------------------------");puts("AVStream信息:");puts("---------------------------------------------");printf("視頻流標識符:%d\n", pFormatCtx->streams[videoIndex]->index);printf("音頻流標識符:%d\n", pFormatCtx->streams[audioIndex]->index);printf("視頻流長度:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);printf("音頻流長度:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);puts("---------------------------------------------");puts("AVCodecContext信息:");puts("---------------------------------------------");printf("視頻碼率:%d kb/s\n", pCodecCtx->bit_rate / 1000);printf("視頻大小:%d * %d\n", pCodecCtx->width, pCodecCtx->height);puts("---------------------------------------------");puts("AVCodec信息:");puts("---------------------------------------------");printf("視頻編碼格式:%s\n", pCodec->name);printf("視頻編碼詳細格式:%s\n", pCodec->long_name);puts("---------------------------------------------");printf("視頻時長:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);printf("音頻時長:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);printf("音頻采樣率:%d\n", pFormatCtx->streams[audioIndex]->codec->sample_rate);printf("音頻信道數目:%d\n", pFormatCtx->streams[audioIndex]->codec->channels);puts("AVFormatContext元數據:");puts("---------------------------------------------");while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX)){printf("[%s] = %s\n", dict->key, dict->value);}puts("---------------------------------------------");puts("AVStream視頻元數據:");puts("---------------------------------------------");dict = NULL;while (dict = av_dict_get(pFormatCtx->streams[videoIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX)){printf("[%s] = %s\n", dict->key, dict->value);}puts("---------------------------------------------");puts("AVStream音頻元數據:");puts("---------------------------------------------");dict = NULL;while (dict = av_dict_get(pFormatCtx->streams[audioIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX)){printf("[%s] = %s\n", dict->key, dict->value);}puts("---------------------------------------------");av_dump_format(pFormatCtx, -1, fileName, 0);printf("\n\n編譯信息:\n%s\n\n", avcodec_configuration());avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0; }/* *打印FFmpeg支持的解碼器 int main(void) {char *info = (char *)malloc(40000);memset(info, 0, 40000);av_register_all();AVCodec *c_temp = av_codec_next(NULL);while (c_temp != NULL){if (c_temp->decode != NULL){strcat(info, "[Decode]");}else{strcat(info, "[Encode]");}switch (c_temp->type){case AVMEDIA_TYPE_VIDEO:strcat(info, "[Video]");break;case AVMEDIA_TYPE_AUDIO:strcat(info, "[Audeo]");break;default:strcat(info, "[Other]");break;}sprintf(info, "%s %10s\n", info, c_temp->long_name);c_temp = c_temp->next;}puts(info);free(info);return 0; } *//* avcodec:編解碼(最重要的庫) avformat:封裝格式處理的庫 avfilter:濾鏡特效處理的庫 avdevice:各種設備的輸入輸出 avutil:工具庫(大部分庫都依賴這個庫) postproc:后加工 swresample:音頻采樣數據格式轉換 swscale:視頻像素數據格式轉換 */VS2013環境下error
error C4996: 'AVStream::codec': 被聲明為已否決
解決:
輸出:
AVFormatContext信息: --------------------------------------------- 文件名:bad.mp4 持續時間:00:03:39 平均混合碼率:803 kb/s 視音頻個數:2 --------------------------------------------- FFMPEG支持的所有的輸入文件格式: --------------------------------------------- mov,mp4,m4a,3gp,3g2,mj2 mp3 mpc mpc8 mpeg mpegts mpegtsraw mpegvideo mpjpeg mpl2mpsub msf msnwctcp mtv musx mv mvi mxf mxg nc nistsphere nsv nut nuv ogg oma pa f alaw mulaw f64be f64le f32be f32le s32be s32le s24be s24le s16be s16le s8 u32b e u32le u24be u24le u16be u16le u8 pjs pmp pva pvf qcp r3d rawvideo realtext red spark rl2 rm roq rpl rsd rso rtp rtsp sami sap sbg sdp sdr2 film_cpk shn siff sl n smk smjpeg smush sol sox spdif srt psxstr stl subviewer1 subviewer sup svag sw f tak tedcaptions thp 3dostr tiertexseq tmv truehd tta txd tty v210 v210x vag vc 1 vc1test vivo vmd vobsub voc vpk vplayer vqf w64 wav wc3movie webm_dash_manifes t webvtt wsaud wsvqa wtv wve wv xa xbin xmv xvag xwma yop yuv4mpegpipe bmp_pipe dds_pipe dpx_pipe exr_pipe j2k_pipe jpeg_pipe jpegls_pipe pcx_pipe pictor_pipe p ng_pipe qdraw_pipe sgi_pipe sunrast_pipe tiff_pipe webp_pipe libgme libmodplug --------------------------------------------- AVInputFormat信息: --------------------------------------------- 封裝格式名稱:mov,mp4,m4a,3gp,3g2,mj2 封裝格式長名稱:QuickTime / MOV 封裝格式擴展名:mov,mp4,m4a,3gp,3g2,mj2 封裝格式ID:0 --------------------------------------------- AVStream信息: --------------------------------------------- 視頻流標識符:0 音頻流標識符:1 視頻流長度:3362816微秒 音頻流長度:9660425微秒 --------------------------------------------- AVCodecContext信息: --------------------------------------------- 視頻碼率:669 kb/s 視頻大小:1440 * 1080 --------------------------------------------- AVCodec信息: --------------------------------------------- 視頻編碼格式:h264 視頻編碼詳細格式:H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 --------------------------------------------- 視頻時長:3362816微秒 音頻時長:9660425微秒 音頻采樣率:44100 音頻信道數目:2 AVFormatContext元數據: --------------------------------------------- [major_brand] = isom [minor_version] = 512 [compatible_brands] = isomiso2avc1mp41 [encoder] = Lavf57.34.100 [title] = BadApple [comment] = FFMPEG TEST --------------------------------------------- AVStream視頻元數據: --------------------------------------------- [language] = und [handler_name] = VideoHandler --------------------------------------------- AVStream音頻元數據: --------------------------------------------- [language] = und [handler_name] = SoundHandler --------------------------------------------- Input #-1, mov,mp4,m4a,3gp,3g2,mj2, from 'bad.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf57.34.100title : BadApplecomment : FFMPEG TESTDuration: 00:03:39.06, start: 0.000000, bitrate: 803 kb/sStream #-1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1440x108 0 [SAR 1:1 DAR 4:3], 669 kb/s, 15 fps, 15 tbr, 15360 tbn (default)Metadata:handler_name : VideoHandlerStream #-1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fl tp, 130 kb/s (default)Metadata:handler_name : SoundHandler編譯信息: --disable-static --enable-shared --enable-gpl --enable-version3 --disable-w32thr eads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enab le-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --e nable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libi lbc --enable-libmodplug --enable-libmfx --enable-libmp3lame --enable-libopencore -amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable -librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-l ibspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libv o-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwe bp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable- libzimg --enable-lzma --enable-decklink --enable-zlib請按任意鍵繼續. . .FFMPEG入門第一個程序。
總結
以上是生活随笔為你收集整理的FFMPEG学习----打印视频信息的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: juniper 认证学习、报名地址汇总
 - 下一篇: 一个Web服务的性能瓶颈分析及对策