CentOS下编译安装ffmpeg 并向YouTube推流

准备一台海外服务器,我使用 vultr

安装 yasm

1
2
3
4
5
6
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar -xvf /yasm-1.3.0.tar.gz
cd /yasm-1.3.0
./configure --prefix=/usr/local
make
make install

安装nasm

1
2
3
4
5
6
wget https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz
tar -xvf nasm-2.15.05.tar.gz
cd nasm-2.15.05
./configure
make
make install

安装libx264

使用 ffmpeg进行推流时会报 Unknown encoder ‘libx264’,所以这一块还需要安装libx264

官网:https://www.videolan.org/developers/x264.html

1
2
3
4
5
6
7
8
9
10
11
git clone https://code.videolan.org/videolan/x264.git
cd x264
./configure --prefix=/usr/local/x264/ --includedir=/usr/local/include --libdir=/usr/local/lib --enable-shared
make
make install

# 安装ffmpeg,可能会报
WARNING: using libx264 without pkg-config
#解决,添加环境变量
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

安装ffmpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wget http://ffmpeg.org/releases/ffmpeg-4.4.tar.gz
tar -xvf ffmpeg-4.4.tar.gz
cd ffmpeg-4.4
./configure --enable-shared --enable-gpl --prefix=/usr/local/ffmpeg --enable-shared --enable-libx264
make
make install
echo "/usr/local/ffmpeg/lib" >> /etc/ld.so.conf
ldconfig

#添加环境变量
vi /etc/profile

export PATH=$PATH:/usr/local/ffmpeg/bin

source /ect/profile

测试是否成功

1
2
# 向YouTube推流
ffmpeg -re -i test1.mp4 -c:v libx264 -b:v 2M -c:a copy -strict -2 -f flv rtmp://a.rtmp.youtube.com/live2/{streamId}

如果发生以下错误:

ffmpeg: error while loading shared libraries: libx264.so.164: cannot open shared object file: No such file or directory

解决方法:

1
2
3
4
5
6
sudo vi /etc/ld.so.conf
# 添加以下内容,/usr/local/ffmpeg/lib ,ffmpeg的安装目录
/usr/local/ffmpeg/lib
/usr/local/lib
# 执行ldconfig
ldconfig

重新执行推流代码,success。

如果出现:

1
2
3
[tcp @ 000001a196c6cc40] Connection to tcp://a.rtmp.youtube.com:1935 failed: Error number -138 occurred
[rtmp @ 000001a196c6c700] Cannot open connection tcp://a.rtmp.youtube.com:1935
rtmp://a.rtmp.youtube.com/live2/tsmq-2d3b-x0d5-jtxa-d8pj: Unknown error

image-20211101162608069

原因:ping youtube.com ,ping不通,解决:换海外服务器或…

作者

Fahsa

发布于

2021-11-01

更新于

2021-11-01

许可协议

评论