首页 > 技术文章 > 最简单的基于FFMPEG的Helloworld程序

lgh1992314 2016-08-10 12:50 原文

学习雷神的FFMPEG入门教程,本文基于命令行实现。

文件结构

G:\Coding\FFMpeg\Proj\Console>dir
 驱动器 G 中的卷没有标签。
 卷的序列号是 0FD5-0CC8

 G:\Coding\FFMpeg\Proj\Console 的目录

2016/08/10  12:46    <DIR>          .
2016/08/10  12:46    <DIR>          ..
2016/04/14  11:08        24,464,896 avcodec-57.dll
2016/04/14  11:08           168,948 avcodec.lib
2016/04/14  11:08         1,363,968 avdevice-57.dll
2016/04/14  11:08            15,628 avdevice.lib
2016/04/14  11:08         3,799,552 avfilter-6.dll
2016/04/14  11:08            50,364 avfilter.lib
2016/04/14  11:08         4,929,536 avformat-57.dll
2016/04/14  11:08           133,888 avformat.lib
2016/04/14  11:08           604,672 avutil-55.dll
2016/04/14  11:08           359,056 avutil.lib
2016/06/23  09:59        21,987,736 bad.mp4
2016/08/09  18:25               424 compile.bat
2016/04/14  11:08    <DIR>          libavcodec
2016/04/14  11:08    <DIR>          libavdevice
2016/04/14  11:08    <DIR>          libavfilter
2016/04/14  11:08    <DIR>          libavformat
2016/04/14  11:08    <DIR>          libavutil
2016/04/14  11:08    <DIR>          libpostproc
2016/04/14  11:08    <DIR>          libswresample
2016/04/14  11:08    <DIR>          libswscale
2016/04/14  11:08           110,080 postproc-54.dll
2016/04/14  11:08             8,904 postproc.lib
2016/04/14  11:08           287,232 swresample-2.dll
2016/04/14  11:08            17,944 swresample.lib
2016/04/14  11:08           513,536 swscale-4.dll
2016/04/14  11:08            27,064 swscale.lib
2013/09/01  09:07             5,800 test.swf
2016/08/09  18:01             4,334 tutorial.c
2016/08/10  12:01             3,075 tutorial.cpp
              21 个文件     58,856,637 字节
              10 个目录 10,641,301,504 可用字节

G:\Coding\FFMpeg\Proj\Console>

自己利用批处理写的一小段编译链接代码:

@echo off
color a
call D:\VS2013\VC\vcvarsall.bat x86
cl /c tutorial.cpp
if %errorlevel% == 0 (goto :LINK) else ((echo CL error) && goto :END)

pause

:LINK
link tutorial.obj avformat.lib avfilter.lib avcodec.lib avutil.lib postproc.lib swresample.lib swscale.lib avdevice.lib
if %errorlevel% == 0 (goto :RUN) else ((echo LINK error) && goto :END)

:RUN
tutorial.exe
tutorial.exe > 1.txt
1.txt

:END
@pause
=====================================================

最简单的基于FFmpeg的视频播放器系列文章列表:

100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)

最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)

最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器

最简单的基于FFMPEG的Helloworld程序

=====================================================

本文记录一个基于FFmpeg的HelloWorld程序。该程序可以打印出FFmpeg类库的基本信息。使用该程序通常可以验证FFmpeg是否正确的安装配置。

源代码

/**
 * 最简单的FFmpeg Helloworld程序
 * Simplest FFmpeg HelloWorld
 *
 * 雷霄骅 Lei Xiaohua
 * leixiaohua1020@126.com
 * 中国传媒大学/数字电视技术
 * Communication University of China / Digital TV Technology
 * http://blog.csdn.net/leixiaohua1020
 *
 * 
 * 本程序是基于FFmpeg函数的最简单的程序。它可以打印出FFmpeg类库的下列信息:
 * Protocol:  FFmpeg类库支持的协议
 * AVFormat:  FFmpeg类库支持的封装格式
 * AVCodec:   FFmpeg类库支持的编解码器
 * AVFilter:  FFmpeg类库支持的滤镜
 * Configure: FFmpeg类库的配置信息
 * 
 * This is the simplest program based on FFmpeg API. It can show following 
 * informations about FFmpeg library:
 * Protocol:  Protocols supported by FFmpeg.
 * AVFormat:  Container format supported by FFmpeg.
 * AVCodec:   Encoder/Decoder supported by FFmpeg.
 * AVFilter:  Filters supported by FFmpeg.
 * Configure: configure information of FFmpeg.
 *
 */

#include <stdio.h>

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#ifdef __cplusplus
};
#endif
#endif

//FIX
struct URLProtocol;
/**
 * Protocol Support Information
 */
char * urlprotocolinfo(){
	
	char *info=(char *)malloc(40000);
	memset(info,0,40000);

	av_register_all();

	struct URLProtocol *pup = NULL;
	//Input
	struct URLProtocol **p_temp = &pup;
	avio_enum_protocols((void **)p_temp, 0);
	while ((*p_temp) != NULL){
		sprintf(info, "%s[In ][%10s]\n", info, avio_enum_protocols((void **)p_temp, 0));
	}
	pup = NULL;
	//Output
	avio_enum_protocols((void **)p_temp, 1);
	while ((*p_temp) != NULL){
		sprintf(info, "%s[Out][%10s]\n", info, avio_enum_protocols((void **)p_temp, 1));
	}

	return info;
}

/**
 * AVFormat Support Information
 */
char * avformatinfo(){

	char *info=(char *)malloc(40000);
	memset(info,0,40000);

	av_register_all();

	AVInputFormat *if_temp = av_iformat_next(NULL);
	AVOutputFormat *of_temp = av_oformat_next(NULL);
	//Input
	while(if_temp!=NULL){
		sprintf(info, "%s[In ] %10s\n", info, if_temp->name);
		if_temp=if_temp->next;
	}
	//Output
	while (of_temp != NULL){
		sprintf(info, "%s[Out] %10s\n", info, of_temp->name);
		of_temp = of_temp->next;
	}
	return info;
}

/**
 * AVCodec Support Information
 */
char * avcodecinfo()
{
	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){
			sprintf(info, "%s[Dec]", info);
		}
		else{
			sprintf(info, "%s[Enc]", info);
		}
		switch (c_temp->type){
		case AVMEDIA_TYPE_VIDEO:
			sprintf(info, "%s[Video]", info);
			break;
		case AVMEDIA_TYPE_AUDIO:
			sprintf(info, "%s[Audio]", info);
			break;
		default:
			sprintf(info, "%s[Other]", info);
			break;
		}

		sprintf(info, "%s %10s\n", info, c_temp->name);

		c_temp=c_temp->next;
	}
	return info;
}

/**
 * AVFilter Support Information
 */
char * avfilterinfo()
{
	char *info=(char *)malloc(40000);
	memset(info,0,40000);

	avfilter_register_all();

	AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);
	
	while (f_temp != NULL){
		sprintf(info, "%s[%15s]\n", info, f_temp->name);
		f_temp=f_temp->next;
	}
	return info;
}

/**
 * Configuration Information
 */
char * configurationinfo()
{
	char *info=(char *)malloc(40000);
	memset(info,0,40000);

	av_register_all();

	sprintf(info, "%s\n", avcodec_configuration());

	return info;
}

int main(int argc, char* argv[])
{
	char *infostr=NULL;
	infostr=configurationinfo();
	printf("\n<<Configuration>>\n%s",infostr);
	free(infostr);

	infostr=urlprotocolinfo();
	printf("\n<<URLProtocol>>\n%s",infostr);
	free(infostr);

	infostr=avformatinfo();
	printf("\n<<AVFormat>>\n%s",infostr);
	free(infostr);

	infostr=avcodecinfo();
	printf("\n<<AVCodec>>\n%s",infostr);
	free(infostr);

	infostr=avfilterinfo();
	printf("\n<<AVFilter>>\n%s",infostr);
	free(infostr);

	return 0;
}

输出




推荐阅读