1.安装FFmpeg依赖
sudo apt-get install yasm
sudo apt-get install libx11-dev xorg-dev
sudo apt-get install libasound2-dev
没有 libasound2-dev 则可能会报下面的错误,不能播放声音:(然后可能进一步导致 ffplay无法生成)
SDL_OpenAudio (2 channels, 32000 Hz): No such audio device
SDL_OpenAudio (1 channels, 32000 Hz): No such audio device
No more combinations to try, audio open failed
下载源码 SDL2-2.0.9.tar.gz (http://www.libsdl.org/release/SDL2-2.0.9.zip) 安装,
./configure --prefix=/usr/local/
make && sudo make install
(SDL 的一些插件可以在这里下载 https://www.libsdl.org/projects/)
或者直接
#apt-get install libsdl1.2-dev
apt-get install libsdl2-dev
apt-get install libsdl2-ttf-dev
apt-get install libsdl2-gfx-dev
2.安装x264编码库
(下面安装x264编码库,其他的库安装可以参看 http://www.56cto.com/499
或 https://www.cnblogs.com/tlnshuju/p/6962178.html
或 https://www.cnblogs.com/candycaicai/p/4689459.html)
git clone git://git.videolan.org/x264.git
./configure --enable-shared --enable-static --enable-strip --disable-cli --disable-asm
make && make install
或者 sudo apt-get install libx264-dev
sudo apt-get install libx265-dev
sudo apt-get install libmp3lame-dev
安装完毕需要将 /usr/local/lib 添加到 LD_LIBRARY_PATH ,
这样ffmpeg可以使用x264的 .so lib, 或者在 /etc/ld.so.conf.d 添加.conf 文件指定so所在路径
补充:若要使能ffmpeg的硬件加速,务必先安装好硬件驱动以及api(eg. libva or vdpau)
3.下载并安装ffmpeg
git clone https://git.ffmpeg.org/ffmpeg.git #然后我切换到release/6.1分支
(--enable-libx264 表示允许h264编码,--enable-gpl是enable x264需要的, 不添加 --enable-ffplay 则不生成 ffplay)
# ./configure --enable-gpl --enable-nonfree --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-pic --enable-static --disable-optimizations --disable-stripping
./configure --enable-gpl --enable-nonfree --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-pic --enable-static --disable-optimizations --disable-stripping --extra-cflags=-g --extra-ldflags=-g --enable-debug
make -j8
sudo make install //将ffmpeg替换到系统
cd /etc/ld.so.conf.d
add another file: ffmpeg.conf
inside, put :
/usr/local/ffmpeg/lib
save, then do #ldconfig
#试一试
ffmpeg --version
ffplay ../视频/HEVC_bbb-1920x1080-cfg02_Video2248.5kbps_60FPS_Audio238.58kbps_aac_6channels.mkv
pkg-config --cflags --libs libavcodec
#二次开发
gcc main.c -o main `pkg-config --cflags --libs libavcodec libavformat libavutil libavfilter libavdevice libpostproc libswresample libswscale`
#c++作为调用者也是一样,使用gcc换成g++即可,有一点需要切记就是include ffmpeg相关头文件的时候需要
extern "C" { //这是必须要加的
#include <xxx>
}
int main{
//xxx
}
4.命令行使用FFMPEG
~/tmp/ffmpeg/ffmpeg/host/bin$ ./ffmpeg
./ffmpeg: error while loading shared libraries: libavdevice.so.57: cannot open shared object file: No such file or directory
发现系统提示找不到动态库,可以用
ldd ffmpeg
来查看运行当前可执行文件需要哪些动态库
问题原因是因为 ubuntu通过源码安装软件未进行环境变量配置,找不到启动路径
sudo gedit /etc/ld.so.conf.d/ffmpeg.conf(新建的),然后把so所在路径添加进去,多个目录就添加多行,eg. /usr/local/lib。最后 sudo ldconfig
5.使用ffmpeg库
//a simple ffmpeg test:
//gcc a.c -g -o a.out -I /usr/local/ffmpeg/include -L /usr/local/ffmpeg/lib -lavcodec -lavutil -lpthread
#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <string.h>
int main(void)
{
printf("%s", avcodec_configuration());
getchar();
return 0;
}
//a simple sdl test:
//gcc a.c -lSDL2
#include <SDL2/SDL.h>
int main()
{
SDL_Window* window =0;
SDL_Renderer* render=0;
SDL_Init(SDL_INIT_EVERYTHING);
window=SDL_CreateWindow("hello",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN);
render=SDL_CreateRenderer(window,-1,0);
SDL_SetRenderDrawColor(render,0,255,0,255);
SDL_RenderClear(render);
SDL_RenderPresent(render);
SDL_Delay(3000);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
return 0;
}
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
#ifdef __cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
#ifdef __cplusplus
};
#endif
//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=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;
}
编译方法
~/tmp/ffmpeg/ffmpeg/host/test$ g++ -I ../include/ hello_world.cpp -o hello_world -L../lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutil
-I 指定头文件的搜索路径, -L指定动态库的搜索路径 -l指定要链接的动态库
//use SDL2.0
gcc tutorial01.c -g -o a.out -I /usr/local/ffmpeg/include -L /usr/local/ffmpeg/lib -lSDL2main -lSDL2 -lz -lm -lavformat -lavcodec -lavutil -lswscale
//use SDL1.2
gcc tutorial02.c -g -o a.out -I /usr/local/ffmpeg/include -I/usr/local/include/SDL -L /usr/local/ffmpeg/lib -lSDL -lz -lm -lavformat -lavcodec -lavutil -lswscale
===========================================
参考 <https://www.cnblogs.com/CoderTian/p/6655568.html>
参考 <http://www.cnblogs.com/CoderTian/p/6651343.html> (AndroidStudio 中使用FFMPEG)