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.\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 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入门第一个程序。

FFMPEG学习----打印视频信息的更多相关文章

  1. FFMPEG学习----解码视频

    基础概念 我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的容器格式规定了其中音视频数据的组织方式(也包括其他 ...

  2. FFMPEG学习----分离视频里的H.264与YUV数据

    #include <stdio.h> extern "C" { #include "libavcodec/avcodec.h" #include & ...

  3. 使用JavaCV实现读取视频信息及自动截取封面图

    概述 最近在对之前写的一个 Spring Boot 的视频网站项目做功能完善,需要利用 FFmpeg 实现读取视频信息和自动截图的功能,查阅资料后发现网上这部分的内容非常少,于是就有了这篇文章. 视频 ...

  4. 【转】学习FFmpeg API – 解码视频

    ffmpeg是编解码的利器,用了很久,以前看过dranger 的教程,非常精彩,受益颇多,是学习ffmpeg api很好的材料.可惜的是其针对的ffmpeg版本已经比较老了,而ffmpeg的更新又很快 ...

  5. .net core Docker 容器添加ffmpeg 获取视频信息和截图

    最近在处理上传视频,需要获取视频信息和截图,这里就需要用到ffmpeg; 由于我的项目是在docker compose中运行调试,所以ffmpeg也需要在docker中能调用: 网上找到的方法在Doc ...

  6. 学习FFmpeg API – 解码视频

    本文转载 视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的 ...

  7. FFmpeg Android 学习(一):Android 如何调用 FFMPEG 编辑音视频

    一.概述 在Android开发中,我们对一些音视频的处理比较无力,特别是编辑音视频这部分.而且在Android上对视频编辑方面,几乎没有任何API做支持,MediaCodec(硬编码)也没有做支持.那 ...

  8. Hadoop源码学习笔记(2) ——进入main函数打印包信息

    Hadoop源码学习笔记(2) ——进入main函数打印包信息 找到了main函数,也建立了快速启动的方法,然后我们就进去看一看. 进入NameNode和DataNode的主函数后,发现形式差不多: ...

  9. python学习(五)--打印错误信息

    from urllib import request #打印错误信息 except Exceptionlist = [ "http://www.baidu11.com/", &qu ...

随机推荐

  1. Linux学习之路--常用命令

    #ls  显示文件信息 #ll  显示文件(不包括隐藏文件)具体信息 等于 #ls -l #ll -a 显示所有文件(包括隐藏文件)具体信息 #ll -htr aa  显示最近修改的文件 h是易读的 ...

  2. 【一起学源码-微服务】Ribbon源码五:Ribbon源码解读汇总篇~

    前言 想说的话 [一起学源码-微服务-Ribbon]专栏到这里就已经全部结束了,共更新四篇文章. Ribbon比较小巧,这里是直接 读的spring cloud 内嵌封装的版本,里面的各种config ...

  3. 杂谈.netcore的Buffer相关新类型

    1 文章范围 本文将.netcore新出现的与Buffer操作相关的类型进行简单分析与讲解,由于资料有限,一些见解为个人见解,可能不是很准确.这些新类型将包括BinaryPrimitives.Span ...

  4. Redis系列(一):Redis简介及环境安装

    提到Redis,大家肯定都听过,并且应该都在项目中或多或少的使用过,也许你觉得Redis用起来挺简单的呀,但如果有人问你下面的几个问题(比如同事或者面试官),你能回答的上来吗? 什么是Redis? R ...

  5. 从操作系统层面理解Linux下的网络IO模型

    I/O( INPUT OUTPUT),包括文件I/O.网络I/O. 计算机世界里的速度鄙视: 内存读数据:纳秒级别. 千兆网卡读数据:微妙级别.1微秒=1000纳秒,网卡比内存慢了千倍. 磁盘读数据: ...

  6. 非关系数据库与redis安装

    1.什么是 NoSQL? NoSQL(NoSQL = Not Only SQL ),意为反 SQL 运动,是一项全新的数据库革命性运动,2000 年 前就有人提出,发展至 2009 年趋势越发高涨.它 ...

  7. Java 遍历集合时产生的ConcurrentModificationException异常

    前几天做Java实验的时候,打算用foreach遍历一个ArrayList集合,并且当集合中的某个元素符合某个值时删除这个元素.写完运行时抛出了ConcurrentModificationExcept ...

  8. 「BZOJ4173」数学

    题面 已知 \[\large{S(n,m)=\{k_{1},k_{2},\cdots k_{i}\}}\] 且每个 \(k\) 满足 \[\large{n \%k+m\%k\geq k}\] 求 \[ ...

  9. k8s~为服务添加ingress的实现

    为服务添加ingress的实现 1 当我们为指定的项目添加ingress支持之后,它会在“负载均衡”标签页出现,并显示出你的域名解析到的服务. 2 我们的ingress是支持https的,所以需要为你 ...

  10. dp-多重背包

    (推荐 : http://blog.csdn.net/insistgogo/article/details/11176693 ) 学会了前两个背包 , 学这个背包还是很轻松的 . 多重背包 , 顾名思 ...