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:SS int 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. "); return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("Couldn't find stream information. "); 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. "); return -1; } pCodecCtx = pFormatCtx->streams[videoIndex]->codec; //指向AVCodecContext的指针 pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //指向AVCodec的指针.查找解码器 if (pCodec == NULL) { printf("Codec not found. "); return -1; } //打开解码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Could not open codec. "); 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. "); return -1; } //打印结构体信息 puts("AVFormatContext信息:"); puts("---------------------------------------------"); printf("文件名:%s ", pFormatCtx->filename); iTotalSeconds = (int)pFormatCtx->duration / 1000000; iHour = iTotalSeconds / 3600;//小时 iMinute = iTotalSeconds % 3600 / 60;//分钟 iSecond = iTotalSeconds % 60;//秒 printf("持续时间:%02d:%02d:%02d ", iHour, iMinute, iSecond); printf("平均混合码率:%d kb/s ", pFormatCtx->bit_rate / 1000); printf("视音频个数:%d ", pFormatCtx->nb_streams); puts("---------------------------------------------"); puts("FFMPEG支持的所有的输入文件格式:"); puts("---------------------------------------------"); pInputFormat = pFormatCtx->iformat; while (pInputFormat) { printf("%s ", pInputFormat->name); pInputFormat = pInputFormat->next; } puts(" ---------------------------------------------"); puts("AVInputFormat信息:"); puts("---------------------------------------------"); printf("封装格式名称:%s ", pFormatCtx->iformat->name); printf("封装格式长名称:%s ", pFormatCtx->iformat->long_name); printf("封装格式扩展名:%s ", pFormatCtx->iformat->extensions); printf("封装格式ID:%d ", pFormatCtx->iformat->raw_codec_id); puts("---------------------------------------------"); puts("AVStream信息:"); puts("---------------------------------------------"); printf("视频流标识符:%d ", pFormatCtx->streams[videoIndex]->index); printf("音频流标识符:%d ", pFormatCtx->streams[audioIndex]->index); printf("视频流长度:%d微秒 ", pFormatCtx->streams[videoIndex]->duration); printf("音频流长度:%d微秒 ", pFormatCtx->streams[audioIndex]->duration); puts("---------------------------------------------"); puts("AVCodecContext信息:"); puts("---------------------------------------------"); printf("视频码率:%d kb/s ", pCodecCtx->bit_rate / 1000); printf("视频大小:%d * %d ", pCodecCtx->width, pCodecCtx->height); puts("---------------------------------------------"); puts("AVCodec信息:"); puts("---------------------------------------------"); printf("视频编码格式:%s ", pCodec->name); printf("视频编码详细格式:%s ", pCodec->long_name); puts("---------------------------------------------"); printf("视频时长:%d微秒 ", pFormatCtx->streams[videoIndex]->duration); printf("音频时长:%d微秒 ", pFormatCtx->streams[audioIndex]->duration); printf("音频采样率:%d ", pFormatCtx->streams[audioIndex]->codec->sample_rate); printf("音频信道数目:%d ", pFormatCtx->streams[audioIndex]->codec->channels); puts("AVFormatContext元数据:"); puts("---------------------------------------------"); while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX)) { printf("[%s] = %s ", 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 ", 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 ", dict->key, dict->value); } puts("---------------------------------------------"); av_dump_format(pFormatCtx, -1, fileName, 0); printf(" 编译信息: %s ", 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 ", 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 mpl2 mpsub 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 : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf57.34.100 title : BadApple comment : FFMPEG TEST Duration: 00:03:39.06, start: 0.000000, bitrate: 803 kb/s Stream #-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 : VideoHandler Stream #-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入门第一个程序。