FFmpeg 关于打开文件异常处理(获取异常信息)
(2016-01-06 10:45:50)分类: 2016-01 |
ffmpeg基本库的介绍:
获取视频时间源码:
1.准备文件名
2.初始化编解码器
过去使用av_open_input_file()函数打开视频文件,在新版的avformat中,使用下面的函数进行视频文件的打开。
AVFormatContext这个结构体描述了一个媒体文件或媒体流的构成和基本信息,这个结构由avformat_open_input在内部创建并
以缺省值初始化部分成员,创建函数为avformat_alloc_context()。如果调用者希望自己创建该结构,则需要显式为该结构的一些成员置
缺省值——如果没有缺省值的话,会导致之后的动作产生异常。
注:该接口的最后两个参数不能为NULL,否则会出现段错误。
4.出错处理
大型程序需要很多出错处理,一是避免错误蔓延,二是可以提供debug信息。如果打开视频文件头信息过程出现错误,打印错误信息并将返回值置为-1,默认认为0是成功返回。最后跳转到fail进行内存释放。
5.获取播放时长
- libavcodec:codec其实是Coder/Decoder的缩写,也就是编码解码器;
- libavdevice:对输出输入设备的支持;
- libavformat:对音频视频格式的解析
- libavutil:集项工具;
- libpostproc:后期效果处理;
- libswscale:视频场景比例缩放、色彩映射转换;
获取视频时间源码:
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- #include "libavutil/avstring.h"
-
- void print_error(const
char *filename, int
err)
- {
-
char errbuf[128]; -
const char *errbuf_ptr = errbuf; -
-
if (av_strerror(err, errbuf, sizeof(errbuf)) <</span> 0) -
errbuf_ptr = strerror(AVUNERROR(err)); -
av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr); - }
- void print_duration(int
seconds)
- {
-
av_log(NULL,0,"duration [hh]:[mm]:[ss] d:d:d\n", seconds/60/60,seconds/60%60,seconds%60); - }
- int main(int
argc, char *argv[])
- {
-
int ret = 0, err = 0; -
char filename[1024]; -
AVFormatContext *ic = NULL; -
AVInputFormat *iformat = NULL; -
AVDictionary *format_opts = NULL; -
AVDictionaryEntry *t; -
-
if(argc <</span> 2) { -
printf("usage:%s filename\n", argv[0]); -
return -1; -
}
-
av_strlcpy(filename, argv[1], strlen(argv[1])); -
-
av_register_all();
-
-
err = avformat_open_input(&ic, filename, iformat, &format_opts); -
if (err <</span> 0) { -
print_error(filename, err); -
ret = -1; -
goto fail; -
}
-
print_duration(ic->duration/1000/1000);
-
fail: -
if (ic) { -
avformat_close_input(&ic);
-
}
-
return ret; - }
1.准备文件名
- $ grep "av_strlcpy" * -i -n -r -s --include "*.h"
- libavutil/avstring.h:84:size_t av_strlcpy(char *dst, const char *src, size_t size);
- int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)