如何把视频转化成H264编码web端可播放的MP4格式,安装带H264编码的FFmpeg_Unknown encoder ‘h264‘

本文详细记录了在Linux环境下安装和配置FFmpeg的过程,包括下载x264库,解决编译期间的错误,以及更新ffmpeg版本以实现H264编码。重点在于解决编译时的`x264_bit_depth`错误和环境变量设置。
摘要由CSDN通过智能技术生成
没安装前
## 没安装前
[root@lm media]# ffmpeg -codecs | grep h264
ffmpeg version 3.1 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)
  configuration: --prefix=/usr/local/ffmpeg
  libavutil      55. 27.100 / 55. 27.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 40.101 / 57. 40.101
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 46.102 /  6. 46.102
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
 D.V.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
安装H264
## 下载x264
git clone https://github.com/mirror/x264.git
cd x264/
./configure --prefix=/usr/local/ffmpeg/x264static --enable-shared --enable-static --disable-asm
make && make install
## 重新编译ffmpeg , 没有ffmpeg的请自行下载
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/ #避免报错without pkg-config
### 配置
./configure --prefix=/usr/local/ffmpeg/ffmpeg --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/ffmpeg/x264static/include --extra-ldflags=-L/usr/local/ffmpeg/x264static/lib
### 编译安装
make && make install

#### 报错
libavcodec/libx264.c: 在函数‘X264_frame’中:
libavcodec/libx264.c:282:9: 错误:‘x264_bit_depth’未声明(在此函数内第一次使用)
     if (x264_bit_depth > 8)
下载比较新版的ffmpeg
wget http://www.ffmpeg.org/releases/ffmpeg-4.2.tar.gz
tar -zxvf ffmpeg-4.2.tar.gz
cd ffmpeg-4.2
./configure --prefix=/usr/local/ffmpeg/ffmpeg --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/ffmpeg/x264static/include --extra-ldflags=-L/usr/local/ffmpeg/x264static/lib
## https://blog.csdn.net/qq_26900081/article/details/104670240

make && make install

cd /usr/local/ffmpeg/ffmpeg/bin
## 预防异常解决方案:error while loading shared libraries: libavdevice.so.58: cannot open shared
vim /etc/ld.so.conf.d/ffmpeg.conf    
#写入内容“/usr/local/ffmpeg/ffmpeg/lib”后保存退出
 
## 预防异常解决方案:error while loading shared libraries: libx264.so.159: cannot open shared object file: No such file or directory
vim /etc/ld.so.conf
#在最后面写入内容“/usr/local/ffmpeg/x264static/lib”后保存退出

ldconfig   #更新环境变量
./ffmpeg -version

## 安装好之后显示编码
[root@lm bin]# ./ffmpeg -codecs | grep h264
ffmpeg version 4.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)
  configuration: --prefix=/usr/local/ffmpeg/ffmpeg --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/ffmpeg/x264static/include --extra-ldflags=-L/usr/local/ffmpeg/x264static/lib
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
 DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (encoders: libx264 libx264rgb )
修改环境变量
## 修改环境变量
vim ~/.bashrc
### 加入到最后 `export PATH=$PATH:/usr/local/ffmpeg/ffmpeg/bin`
[root@lm bin]# cat ~/.bashrc | tail -n 5
#export PATH=$PATH:/usr/local/ffmpeg/bin

## ffmpeg4.2
export PATH=$PATH:/usr/local/ffmpeg/ffmpeg/bin
[root@lm bin]# echo $PATH
/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/git/bin:/usr/local/ffmpeg/bin:/root/bin:/usr/local/src/git/bin:/usr/local/ffmpeg/bin:/root/bin:/usr/local/ffmpeg/bin:/usr/local/ffmpeg/ffmpeg/bin

## 发现前面的`/usr/local/ffmpeg/bin`没有清楚掉,所以手动设置一波$PATH
[root@lm bin]# export PATH=/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/git/bin
[root@lm bin]# echo $PATH
/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/git/bin
[root@lm bin]# source ~/.bashrc
[root@lm bin]# echo $PATH
/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/root/.vscode-server/bin/3c4e3df9e89829dce27b7b5c24508306b151f30d/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/src/git/bin:/usr/local/ffmpeg/ffmpeg/bin
[root@lm bin]# ffmpeg -version
ffmpeg version 4.2 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)
configuration: --prefix=/usr/local/ffmpeg/ffmpeg --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/ffmpeg/x264static/include --extra-ldflags=-L/usr/local/ffmpeg/x264static/lib
libavutil      56. 31.100 / 56. 31.100
libavcodec     58. 54.100 / 58. 54.100
libavformat    58. 29.100 / 58. 29.100
libavdevice    58.  8.100 / 58.  8.100
libavfilter     7. 57.100 /  7. 57.100
libswscale      5.  5.100 /  5.  5.100
libswresample   3.  5.100 /  3.  5.100
libpostproc    55.  5.100 / 55.  5.100

### 如果你的PATH一直很顽固,你可以试试 reboot, 我用了

操作
ffmpeg -y -i test_result.avi -vcodec h264 -strict -2 ffmpeg4.2_test.mp4

也可以是如下python封装函数

def avi_to_web_mp4(self, input_file_path):
    '''
    ffmpeg -i test_result.avi -vcodec h264 test_result.mp4
    @param: [in] input_file_path 带avi或mp4的非H264编码的视频的全路径
    @return: [output] output_file_path 生成的H264编码视频的全路径
    '''
    output_file_path = input_file_path[:-3] + 'mp4'
    cmd = 'ffmpeg -y -i {} -vcodec h264 {}'.format(input_file_path, output_file_path)
    subprocess.call(cmd, shell=True)
    return output_file_path
参考链接

https://blog.csdn.net/weixin_33127753/article/details/84637351
https://blog.csdn.net/qq_26900081/article/details/104670240
http://bbs.chinaffmpeg.com/forum.php?mod=viewthread&tid=443

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值