效果如下
执行效果打印日志
zh@zh-lpc:~/project/ffmpeg$ ./frmi test.mp4 open test.mp4 success. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 title : BigBuckBunny_115k.mov encoder : Lavf58.76.100 Duration: 00:05:52.96, bitrate: N/A Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 240x160, 87 kb/s, 23.88 fps, 24 tbr, 90k tbn (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 12000 Hz, 2 channels, 32 kb/s (default) Metadata: handler_name : SoundHandler zh@zh-lpc:~/project/ffmpeg$
简单分析
在真正实操之前,我们先来分析一下,我们正常使用C语言读取一个文件都需要哪些步骤:
- 1、打开文件;
- 2、读取文件信息;
- 3、关闭文件。
一般就是:open --> read --> close三步走。那么在ffmpeg中至少也需要这三步。
Linux中原始读取文件的小实验
1、创建一个文件
zh@zh-lpc:~/project/unixapi$ echo "aaaaa三生三世十里桃花123456" > info.txt
2、创建C语言编程文件
#include <stdio.h> #include <fcntl.h> #define BUFFSIZE 1024 int main() { int ret = 0; FILE *file; char buf[BUFFSIZE]; char *fileName = "./info.txt"; //open file file = fopen(fileName,"r"); //read file fread(buf, BUFFSIZE+1, 1, file); printf("%s\n", buf); //close file fclose(file); return 0; }
3、编译
zh@zh-lpc:~/project/unixapi$ make unix_file cc unix_file.c -o unix_file zh@zh-lpc:~/project/unixapi$
4、执行
zh@zh-lpc:~/project/unixapi$ ./unix_file aaaaa三生三世十里桃花123456 zh@zh-lpc:~/project/unixapi$
ffmpeg读取视频文件信息—代码
ffmpeg_read_media_info.c:
/** * use ffmpeg codeing read flow file info **/ #include <stdio.h> #include <libavutil/log.h> #include <libavformat/avformat.h> int main(int argc, char *argv[]) { int ret = 0; const char* fileName = ""; AVFormatContext *ac = NULL; //set log level av_log_set_level(AV_LOG_INFO); //diff params = 2 is params != 2 if(argc != 2) { av_log(NULL,AV_LOG_WARNING,"params not enough. \n\n"); return -1; } //file name fileName = argv[1]; //open video file ret = avformat_open_input(&ac,fileName,NULL,NULL); //diff open success or error if(ret < 0) { av_log(NULL,AV_LOG_ERROR,"open %s error. \n\n",fileName); return -1; }else{ av_log(NULL,AV_LOG_INFO,"open %s success. \n\n",fileName); } av_dump_format(ac,0,fileName,0); //close file flow avformat_close_input(&ac); return 0; }
编译:
gcc -g -o frmi ffmpeg_read_media_info.c -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib -lavformat -lavutil
执行
zh@zh-lpc:~/project/ffmpeg$ ls -l test.mp4 -rwx------ 1 zh zh 5431627 9月 10 23:05 test.mp4 zh@zh-lpc:~/project/ffmpeg$ zh@zh-lpc:~/project/ffmpeg$ ls -l frmi -rwxrwxr-x 1 zh zh 65152 9月 16 22:00 frmi zh@zh-lpc:~/project/ffmpeg$ zh@zh-lpc:~/project/ffmpeg$ ls -l ffmpeg_read_media_info.c -rwx------ 1 zh zh 955 9月 16 22:02 ffmpeg_read_media_info.c zh@zh-lpc:~/project/ffmpeg$ zh@zh-lpc:~/project/ffmpeg$ zh@zh-lpc:~/project/ffmpeg$ ./frmi test.mp4 open test.mp4 success. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 title : BigBuckBunny_115k.mov encoder : Lavf58.76.100 Duration: 00:05:52.96, bitrate: N/A Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 240x160, 87 kb/s, 23.88 fps, 24 tbr, 90k tbn (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 12000 Hz, 2 channels, 32 kb/s (default) Metadata: handler_name : SoundHandler zh@zh-lpc:~/project/ffmpeg$