1:准备环境:
因为这里需要SDL相关图形界面的一些测试,所以最好准备带界面的ubuntu环境。
这里我一般采用的是16.04或者18.04的环境。
安装必要的软件:
hlp@hlp:/$ sudo apt-get update
hlp@hlp:/$ sudo apt-get -y install autoconf automake build-essential cmake git-core libass-dev libfreetype6-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev
2:进行安装:
1:注意:
1:建议采用源码方式安装,测试时发现,命令直接安装可能会在测试的时候多一些链接编译的问题。
比如问题报错:libx265.c:84: error: undefined reference to `x265_api_get_146'
解决方案: 卸载安装的x265, 用源码安装,然后重新编译下ffmpeg。
2:根据要使用的功能,ffmpeg 在编译的时候需要依赖一些第三方库以支持,这里介绍一些常用的第三方库。
2:安装常用第三方库(如何确定第三方库的版本呢?)
1:采用命令的方式进行安装。
==》可能会在测试的时候导致一些编译问题,需要更新对应依赖库版本。
1:NASM: 部分库会使用的汇编程序
sudo apt-get install nasm
库nasm版本>=2.13
2:Yasm :部分库会使用的汇编程序
sudo apt-get install yasm
yasm版本 ≥ 1.2.0
3:libx264 : H.264视频编码器
编译ffmpeg时配置:–enable-gpl --enable-libx264.
sudo apt-get install libx264-dev
libx264-dev版本 ≥ 118
4:libx265:H.265/HEVC 视频编码器
编译ffmpeg时配置:–enable-gpl --enable-libx265
sudo apt-get install libx265-dev libnuma-dev
系统提供的 libx265-dev版本≥ 68
注意这个版本可能不满足:
hlp@hlp:~/audioplay$ apt search libx265
Sorting... Done
Full Text Search... Done
libx265-146/bionic,now 2.6-3 amd64 [installed,auto-removable]
H.265/HEVC video stream encoder (shared library)
libx265-dev/bionic 2.6-3 amd64
H.265/HEVC video stream encoder (development files)
libx265-doc/bionic,bionic 2.6-3 all
H.265/HEVC video stream encoder (documentation)
5:libvpx:VP8/VP9视频编解码器
编译ffmpeg时配置: --enable-libvpx.
sudo apt-get install libvpx-dev
系统提供的libvpx-dev version ≥ 1.4.0
6:libfdk-aac: AAC音频编码器
编译ffmpeg时配置:–enable-libfdk-aac ( 如果你已经配置了 --enable-gpl则需要加上–enable-nonfree).
sudo apt-get install libfdk-aac-dev
只要系统提供
7:libmp3lame: MP3音频编码器
编译ffmpeg时配置:–enable-libmp3lame.
sudo apt-get install libmp3lame-dev
系统提供的libmp3lame-dev版本≥ 3.98.3
8:libopus: Opus音频编解码器.
编译ffmpeg时配置:–enable-libopus.
sudo apt-get install libopus-dev
系统提供的libopus-dev 版本≥ 1.1
9:libaom : AV1 视频编解码器:
编译ffmpeg时配置:–enable-libaom.
AV1编译有问题,这个用源码编译的方式吧。
2:采用源码的方式进行安装。
0:准备安装目录
hlp@hlp:~/audioplay$ mkdir -p bin ffmpeg_build ffmpeg_sources
hlp@hlp:~/audioplay$ ls
bin ffmpeg_build ffmpeg_sources
我在home目录下创建了audioplay文件夹,文件夹下创建bin ffmpeg_build ffmpeg_sources文件夹,以存放编译源码及编译结果
ffmpeg_sources:用于下载源文件
ffmpeg_build: 存储编译后的库文件
bin:存储二进制文件(ffmpeg,ffplay,ffprobe,X264,X265等)
1:NASM: 部分库会使用的汇编程序
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.bz2
tar xjvf nasm-2.14.02.tar.bz2
cd nasm-2.14.02
./autogen.sh
PATH="$HOME/audioplay/bin:$PATH" ./configure --prefix="$HOME/audioplay/ffmpeg_build" --bindir="$HOME/audioplay/bin"
make
make install
2:Yasm :部分库会使用的汇编程序
wget -O yasm-1.3.0.tar.gz https://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/audioplay/ffmpeg_build" --bindir="$HOME/audioplay/bin"
make
make install
3:libx264 : H.264视频编码器
编译ffmpeg时配置:–enable-gpl --enable-libx264.
git -C x264 pull 2> /dev/null || git clone --depth 1 https://gitee.com/mirrors_addons/x264.git
cd x264
PATH="$HOME/audioplay/bin:$PATH" PKG_CONFIG_PATH="$HOME/audioplay/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/audioplay/ffmpeg_build" --bindir="$HOME/audioplay/bin" --enable-static --enable-pic
PATH="$HOME/audioplay/bin:$PATH" make
make install
4:libx265:H.265/HEVC 视频编码器
编译ffmpeg时配置:–enable-gpl --enable-libx265
sudo apt-get install mercurial libnuma-dev
if cd x265 2> /dev/null; then git pull && cd ..; else git clone https://gitee.com/mirrors_videolan/x265.git; fi
cd x265/build/linux
PATH="$HOME/audioplay/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/audioplay/ffmpeg_build" -DENABLE_SHARED=off ../../source
PATH="$HOME/audioplay/bin:$PATH" make
make install
5:libvpx:VP8/VP9视频编解码器
编译ffmpeg时配置: --enable-libvpx.
git -C libvpx pull 2> /dev/null || git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx
PATH="$HOME/audioplay/bin:$PATH" ./configure --prefix="$HOME/audioplay/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
PATH="$HOME/audioplay/bin:$PATH" make
make install
6:libfdk-aac: AAC音频编码器
编译ffmpeg时配置:–enable-libfdk-aac ( 如果你已经配置了 --enable-gpl则需要加上–enable-nonfree).
git -C fdk-aac pull 2> /dev/null || git clone --depth 1 https://github.com/mstorsjo/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/audioplay/ffmpeg_build" --disable-shared
make
make install
7:libmp3lame: MP3音频编码器
编译ffmpeg时配置:–enable-libmp3lame.
git clone --depth 1 https://gitee.com/hqiu/lame.git
cd lame
PATH="$HOME/audioplay/bin:$PATH" ./configure --prefix="$HOME/audioplay/ffmpeg_build" --bindir="$HOME/audioplay/bin" --disable-shared --enable-nasm
PATH="$HOME/audioplay/bin:$PATH" make
make install
8:libopus: Opus音频编解码器.
编译ffmpeg时配置:–enable-libopus.
git -C opus pull 2> /dev/null || git clone --depth 1 https://github.com/xiph/opus.git
cd opus
./autogen.sh
./configure --prefix="$HOME/audioplay/ffmpeg_build" --disable-shared
make
make install
9:libaom : AV1 视频编解码器:
编译ffmpeg时配置:–enable-libaom.
git -C aom pull 2> /dev/null || git clone --depth 1 https://github.com/mozilla/aom.git
mkdir -p aom_build
cd aom_build
PATH="$HOME/audioplay/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/audioplay/ffmpeg_build" -DENABLE_SHARED=off -DENABLE_NASM=on ../aom
PATH="$HOME/audioplay/bin:$PATH" make
make install
据说测试有报错,参考网上的方法进行编译通过:
cd ~/ffmpeg_sources
git -C aom pull 2> /dev/null || git clone --depth 1 https://aomedia.googlesource.com/aom
mkdir -p aom_build
cd aom_build
PATH="$HOME/audioplay/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/audioplay/ffmpeg_build" -DENABLE_SHARED=off -DENABLE_NASM=on ../aom
PATH="$HOME/audioplay/bin:$PATH" make
make install
3:安装ffmpeg
这里带了av1的选项,–enable-libaom
wget -O ffmpeg-4.2.1.tar.bz2 https://ffmpeg.org/releases/ffmpeg-4.2.1.tar.bz2
tar xjvf ffmpeg-4.2.1.tar.bz2
cd ffmpeg-4.2.1
PATH="$HOME/audioplay/bin:$PATH" PKG_CONFIG_PATH="$HOME/audioplay/ffmpeg_build/lib/pkgconfig" ./configure \
--prefix="$HOME/audioplay/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I$HOME/audioplay/ffmpeg_build/include" \
--extra-ldflags="-L$HOME/audioplay/ffmpeg_build/lib" \
--extra-libs="-lpthread -lm" \
--bindir="$HOME/audioplay/bin" \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree \
--enable-libaom
PATH="$HOME/audioplay/bin:$PATH" make
make install
hash -r
4:安装SDL2
下载地址:https://www.libsdl.org/download-2.0.php
1. 下载SDL源码库,SDL2-2.0.10.tar.gz
2. 解压,然后依次执⾏命令
./autogen.sh
./configure --prefix=/home/hlp/audioplay/ffmpeg_build --bindir=/home/hlp/audioplay/bin
make
sudo make install
4:测试以及配置手册
配置环境变量:
export PATH=$HOME/audioplay/bin:$PATH
export LD_LIBRARY_PATH=$HOME/audioplay/ffmpeg_build/lib/
在有图形界面的ubuntu环境中进行ffplay流媒体播放测试。
湖南卫视 rtmp://58.200.131.2:1935/livetv/hunantv
ffplay rtmp://58.200.131.2:1935/livetv/hunantv
如果要使用man ffmpeg,需要配置手册,HTML 格式的文档位于 ~/ffmpeg_build/share/doc/ffmpeg.
echo "MANPATH_MAP $HOME/bin $HOME/ffmpeg_build/share/man" >> ~/.manpath
5:qt的安装以及用qt测试ffmpeg
1:下载:
下载地址:https://download.qt.io/archive/qt/
下载地址:https://download.qt.io/archive/qt/5.12/5.12.10/
2:安装:
在图形界面下,用命令行执行:
hlp@hlp:~/audiotools/qt$ chmod 777 qt-opensource-linux-x64-5.12.10.run
hlp@hlp:~/audiotools/qt$ ./qt-opensource-linux-x64-5.12.10.run
按照图形界面提示进行安装
3:可执行文件进行运行,然后查看报错:
解决方案,执行安装:
sudo apt install --reinstall libxcb-xinerama0
如果用sudo去安装qt,可能有权限问题,这个遇到过,但是本次没有测试:
sudo chown -R lqf:lqf ~/.config/
4:用qt测试ffmpeg
1:新建qt项目
2:修改pro文件,适配include和libs,修改测试c文件,进行debug调试
(分别对ffmpeg,sdl,以及MP4提取aac(要在qt中设置参数,需要包源文件拷贝到qt生成的debug项目中)进行测试)
3:转码成aac的测试代码如下:
pro文件:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.c
INCLUDEPATH += /home/hlp/audioplay/ffmpeg_build/include
LIBS += \
/home/hlp/audioplay/ffmpeg_build/lib/libavformat.a \
/home/hlp/audioplay/ffmpeg_build/lib/libavcodec.a \
/home/hlp/audioplay/ffmpeg_build/lib/libavdevice.a \
/home/hlp/audioplay/ffmpeg_build/lib/libavfilter.a \
/home/hlp/audioplay/ffmpeg_build/lib/libavutil.a \
/home/hlp/audioplay/ffmpeg_build/lib/libswresample.a \
/home/hlp/audioplay/ffmpeg_build/lib/libswscale.a \
/home/hlp/audioplay/ffmpeg_build/lib/libmp3lame.a \
/home/hlp/audioplay/ffmpeg_build/lib/libfdk-aac.a \
/home/hlp/audioplay/ffmpeg_build/lib/libopus.a \
/home/hlp/audioplay/ffmpeg_build/lib/libvpx.a \
/home/hlp/audioplay/ffmpeg_build/lib/libx265.a \
/home/hlp/audioplay/ffmpeg_build/lib/libx264.a \
/home/hlp/audioplay/ffmpeg_build/lib/libaom.a
LIBS += -lpthread -lnuma -lz -lm -lva-drm -lva -lva-x11 -lvdpau -lX11 -lXext -ldl -lvorbisenc -lvorbis
main.c
#include <stdio.h>
#include "libavutil/log.h"
#include "libavformat/avio.h"
#include "libavformat/avformat.h"
#define ADTS_HEADER_LEN 7;
const int sampling_frequencies[] = {
96000, // 0x0
88200, // 0x1
64000, // 0x2
48000, // 0x3
44100, // 0x4
32000, // 0x5
24000, // 0x6
22050, // 0x7
16000, // 0x8
12000, // 0x9
11025, // 0xa
8000 // 0xb
// 0xc d e f是保留的
};
int adts_header(char * const p_adts_header, const int data_length,
const int profile, const int samplerate,
const int channels)
{
int sampling_frequency_index = 3; // 默认使用48000hz
int adtsLen = data_length + 7;
int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);
int i = 0;
for(i = 0; i < frequencies_size; i++)
{
if(sampling_frequencies[i] == samplerate)
{
sampling_frequency_index = i;
break;
}
}
if(i >= frequencies_size)
{
printf("unsupport samplerate:%d\n", samplerate);
return -1;
}
p_adts_header[0] = 0xff; //syncword:0xfff 高8bits
p_adts_header[1] = 0xf0; //syncword:0xfff 低4bits
p_adts_header[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bit
p_adts_header[1] |= (0 << 1); //Layer:0 2bits
p_adts_header[1] |= 1; //protection absent:1 1bit
p_adts_header[2] = (profile)<<6; //profile:profile 2bits
p_adts_header[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index 4bits
p_adts_header[2] |= (0 << 1); //private bit:0 1bit
p_adts_header[2] |= (channels & 0x04)>>2; //channel configuration:channels 高1bit
p_adts_header[3] = (channels & 0x03)<<6; //channel configuration:channels 低2bits
p_adts_header[3] |= (0 << 5); //original:0 1bit
p_adts_header[3] |= (0 << 4); //home:0 1bit
p_adts_header[3] |= (0 << 3); //copyright id bit:0 1bit
p_adts_header[3] |= (0 << 2); //copyright id start:0 1bit
p_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bits
p_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bits
p_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bits
p_adts_header[5] |= 0x1f; //buffer fullness:0x7ff 高5bits
p_adts_header[6] = 0xfc; //11111100 //buffer fullness:0x7ff 低6bits
// number_of_raw_data_blocks_in_frame:
// 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧。
return 0;
}
int main(int argc, char *argv[])
{
int ret = -1;
char errors[1024];
char *in_filename = NULL;
char *aac_filename = NULL;
FILE *aac_fd = NULL;
int audio_index = -1;
int len = 0;
AVFormatContext *ifmt_ctx = NULL;
AVPacket pkt;
// 设置打印级别
av_log_set_level(AV_LOG_DEBUG);
if(argc < 3)
{
av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");
return -1;
}
in_filename = argv[1]; // 输入文件
aac_filename = argv[2]; // 输出文件
if(in_filename == NULL || aac_filename == NULL)
{
av_log(NULL, AV_LOG_DEBUG, "src or dts file is null, plz check them!\n");
return -1;
}
aac_fd = fopen(aac_filename, "wb");
if (!aac_fd)
{
av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", aac_filename);
return -1;
}
// 打开输入文件
if((ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL)) < 0)
{
av_strerror(ret, errors, 1024);
av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",
in_filename,
ret,
errors);
return -1;
}
// 获取解码器信息
if((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0)
{
av_strerror(ret, errors, 1024);
av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",
in_filename,
ret,
errors);
return -1;
}
// dump媒体信息
av_dump_format(ifmt_ctx, 0, in_filename, 0);
// 初始化packet
av_init_packet(&pkt);
// 查找audio对应的steam index
audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if(audio_index < 0)
{
av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",
av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
in_filename);
return AVERROR(EINVAL);
}
// 打印AAC级别
printf("audio profile:%d, FF_PROFILE_AAC_LOW:%d\n",
ifmt_ctx->streams[audio_index]->codecpar->profile,
FF_PROFILE_AAC_LOW);
if(ifmt_ctx->streams[audio_index]->codecpar->codec_id != AV_CODEC_ID_AAC)
{
printf("the media file no contain AAC stream, it's codec_id is %d\n",
ifmt_ctx->streams[audio_index]->codecpar->codec_id);
goto failed;
}
// 读取媒体文件,并把aac数据帧写入到本地文件
while(av_read_frame(ifmt_ctx, &pkt) >=0 )
{
if(pkt.stream_index == audio_index)
{
char adts_header_buf[7] = {0};
adts_header(adts_header_buf, pkt.size,
ifmt_ctx->streams[audio_index]->codecpar->profile,
ifmt_ctx->streams[audio_index]->codecpar->sample_rate,
ifmt_ctx->streams[audio_index]->codecpar->channels);
fwrite(adts_header_buf, 1, 7, aac_fd); // 写adts header , ts流不适用,ts流分离出来的packet带了adts header
len = fwrite( pkt.data, 1, pkt.size, aac_fd); // 写adts data
if(len != pkt.size)
{
av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
len,
pkt.size);
}
}
av_packet_unref(&pkt);
}
failed:
// 关闭输入文件
if(ifmt_ctx)
{
avformat_close_input(&ifmt_ctx);
}
if(aac_fd)
{
fclose(aac_fd);
}
return 0;
}
先编译查看有没有报错,然后配置qt执行时的入参,把输入文件拷贝到该项目对应的debug项目中。