亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用GPU硬件加速FFmpeg視頻轉碼及排坑

發布時間:2020-02-29 08:10:03 來源:網絡 閱讀:1411 作者:東666 欄目:大數據

Author: dongdong

Email: ldyldy828@126.com

本文內容包括:

  • 在Linux環境下安裝FFmpeg
  • 通過命令行實現視頻格式識別和轉碼
  • 有Nvidia顯卡的情況下,在Linux下使用GPU進行視頻轉碼加速的方法

FFmpeg編譯安裝

在FFmpeg官網https://ffmpeg.org/download.html可以下載到ubunto/debian的發行包,其他Linux發行版需自行編譯。同時,如果要使用GPU進行硬件加速的話,也是必須自己編譯FFmpeg的,所以本節將介紹從源碼編譯安裝FFmpeg的方法(基于RHEL/Centos)

安裝依賴工具

yum install autoconf automake bzip2 cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel

準備工作

在$HOME下創建ffmpeg_sources目錄

我自己定義的HOME=/home/local/

編譯并安裝依賴庫

本節中的依賴庫基本都是必須的,建議全部安裝

nasm

匯編編譯器,編譯某些依賴庫的時候需要

cd /home/local/ffmpeg_sources
curl -O -L http://www.nasm.us/pub/nasm/releasebuilds/2.13.02/nasm-2.13.02.tar.bz2
tar xjvf nasm-2.13.02.tar.bz2
cd nasm-2.13.02
./autogen.sh
./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin"
make
make install
yasm

匯編編譯器,編譯某些依賴庫的時候需要

cd /home/local/ffmpeg_sources
curl -O -L http://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/local/ffmpeg_build" --bindir="/home/local/bin"
make
make install
libx264

H.264視頻編碼器,如果需要輸出H.264編碼的視頻就需要此庫,所以可以說是必備

cd /home/local/ffmpeg_sources
git clone --depth 1 http://git.videolan.org/git/x264
cd x264
PKG_CONFIG_PATH="/home/local/ffmpeg_build/lib/pkgconfig" ./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin" --enable-static
make
make install
libx265

H.265/HEVC視頻編碼器。
如果不需要此編碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libx265

cd /home/local/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/home/local/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install
libfdk_acc

AAC音頻編碼器,必備

cd /home/local/ffmpeg_sources
git clone --depth 1 --branch v0.1.6 https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
autoreconf -fiv
./configure --prefix="/home/local/ffmpeg_build" --disable-shared
make
make install
libmp3lame

MP3音頻編碼器,必備

cd /home/local/ffmpeg_sources
curl -O -L http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin" --disable-shared --enable-nasm
make
make install
libops

OPUS音頻編碼器
如果不需要此編碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libopus

cd /home/local/ffmpeg_sources
curl -O -L https://archive.mozilla.org/pub/opus/opus-1.2.1.tar.gz
tar xzvf opus-1.2.1.tar.gz
cd opus-1.2.1
./configure --prefix="/home/local/ffmpeg_build" --disable-shared
make
make install
libogg

被libvorbis依賴

cd /home/local/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xzvf libogg-1.3.3.tar.gz
cd libogg-1.3.3
./configure --prefix="/home/local/ffmpeg_build" --disable-shared
make
make install
libvorbis

Vorbis音頻編碼器
如果不需要此編碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libvorbis

cd /home/local/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.5.tar.gz
tar xzvf libvorbis-1.3.5.tar.gz
cd libvorbis-1.3.5
./configure --prefix="/home/local/ffmpeg_build" --with-ogg="/home/local/ffmpeg_build" --disable-shared
make
make install
libvpx

VP8/VP9視頻編/解碼器
如果不需要此編/解碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libvpx

cd /home/local/ffmpeg_sources
git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx
./configure --prefix="/home/local/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make
make install

編譯安裝ffmpeg 3.3.8

cd  /home/local/ffmpeg_sources
curl -O -L https://ffmpeg.org/releases/ffmpeg-3.3.8.tar.bz2
tar xjvf ffmpeg-3.3.8.tar.bz2
cd ffmpeg-3.3.8
PATH="/home/local/ffmpeg_sources/bin:$PATH" PKG_CONFIG_PATH="/home/local/ffmpeg_sources/ffmpeg_build/lib/pkgconfig" ./configure   --prefix="/home/local/ffmpeg_sources/ffmpeg_build" --pkg-config-flags="--static"   --extra-cflags="-I /home/local/ffmpeg_sources/ffmpeg_build/include"   --extra-ldflags="-L /home/local/ffmpeg_sources/ffmpeg_build/lib"   --extra-libs=-lpthread   --extra-libs=-lm   --bindir="$HOME/bin"   --enable-gpl   --enable-libfdk_aac   --enable-libfreetype   --enable-libmp3lame   --enable-libopus   --enable-libvorbis   --enable-libvpx   --enable-libx264   --enable-libx265   --enable-nonfree  --enable-libfreetype
make
make install 
hash -r

驗證安裝

ffmpeg -h

使用FFmpeg

識別視頻信息

通過ffprobe命令識別并輸出視頻信息

ffprobe -v error -show_streams -print_format json <input>  

為方便程序解析,將視頻信息輸出為json格式,樣例如下:

{
    "streams": [
        {
            "index": 0,
            "codec_name": "h364",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "High",
            "codec_type": "video",
            "codec_time_base": "61127/3668400",
            "codec_tag_string": "avc1",
            "codec_tag": "0x31637661",
            "width": 1920,
            "height": 1080,
            "coded_width": 1920,
            "coded_height": 1080,
            "has_b_frames": 0,
            "sample_aspect_ratio": "0:1",
            "display_aspect_ratio": "0:1",
            "pix_fmt": "yuv420p",
            "level": 40,
            "color_range": "tv",
            "color_space": "bt709",
            "color_transfer": "bt709",
            "color_primaries": "bt709",
            "chroma_location": "left",
            "refs": 1,
            "is_avc": "true",
            "nal_length_size": "4",
            "r_frame_rate": "30/1",
            "avg_frame_rate": "1834200/61127",
            "time_base": "1/600",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 61127,
            "duration": "101.878333",
            "bit_rate": "16279946",
            "bits_per_raw_sample": "8",
            "nb_frames": "3057",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "rotate": "90",
                "creation_time": "2018-08-09T09:13:33.000000Z",
                "language": "und",
                "handler_name": "Core Media Data Handler",
                "encoder": "H.264"
            },
            "side_data_list": [
                {
                    "side_data_type": "Display Matrix",
                    "displaymatrix": "\n00000000:            0       65536           0\n00000001:       -65536           0           0\n00000002:     70778880           0  1073741824\n",
                    "rotation": -90
                }
            ]
        },
        {
            "index": 1,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "LC",
            "codec_type": "audio",
            "codec_time_base": "1/44100",
            "codec_tag_string": "mp4a",
            "codec_tag": "0x6134706d",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 1,
            "channel_layout": "mono",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/44100",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 4492835,
            "duration": "101.878345",
            "bit_rate": "91595",
            "max_bit_rate": "96000",
            "nb_frames": "4390",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "creation_time": "2018-08-09T09:13:33.000000Z",
                "language": "und",
                "handler_name": "Core Media Data Handler"
            }
        },
        {
            "index": 2,
            "codec_type": "data",
            "codec_tag_string": "mebx",
            "codec_tag": "0x7862656d",
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/600",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 61127,
            "duration": "101.878333",
            "bit_rate": "119",
            "nb_frames": "17",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "creation_time": "2018-08-09T09:13:33.000000Z",
                "language": "und",
                "handler_name": "Core Media Data Handler"
            }
        },
        {
            "index": 3,
            "codec_type": "data",
            "codec_tag_string": "mebx",
            "codec_tag": "0x7862656d",
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/600",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 61127,
            "duration": "101.878333",
            "nb_frames": "1",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "creation_time": "2018-08-09T09:13:33.000000Z",
                "language": "und",
                "handler_name": "Core Media Data Handler"
            }
        }
    ]
}

可以看到一共返回了4個流,其中第0個是視頻流,1是音頻流,2和3是附加數據,沒什么用
如果想指定分析視頻流或音頻流的話,可以加上參數-show_streams -v-show_streams -a,這樣就會只輸出視頻/音頻流的分析結果

視頻轉碼

ffmpeg -i <input> -c:v libx264 -b:v 2048k -vf scale=1280:-1 -y <output>

上述命令將輸入視頻轉碼為h364編碼的視頻

  • -c:v:指定編碼器,編碼器列表可以使用ffmpeg -codecs查看
  • -vf scale:指定輸出視頻的寬高,高-1代表按照比例自動適應
  • -b:v:指定輸出視頻的碼率,即輸出視頻每秒的bit數
  • libx264支持的其他參數請使用ffmpeg -h encoder=libx264命令查詢,如轉碼為其他編碼,也可使用類似命令查詢可用參數

使用Nvidia顯卡GPU進行轉碼

重頭戲來了,這塊的資料相當少,我也是費了一番力氣才搞定

CUDA

CUDA是Nvidia出的一個GPU計算庫,讓程序員可以驅動Nvidia顯卡的GPU進行各種工作,其中就包含了視頻的編解碼

安裝CUDA

首先驗證一下顯卡驅動是否裝好

nvidia-smi

如果驅動正常的話,此命令會輸出顯卡的型號、驅動版本、現存/GPU占用等信息。如何安裝顯卡驅動本文不描述,請參考其他資料。

到CUDA官網https://developer.nvidia.com/cuda-downloads下載對應平臺的發行包,這里我選擇

wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-rhel7-10-2-local-10.2.89-440.33.01-1.0-1.x86_64.rpm
sudo rpm -i cuda-repo-rhel7-10-2-local-10.2.89-440.33.01-1.0-1.x86_64.rpm
sudo yum clean all
sudo yum -y install nvidia-driver-latest-dkms cuda
sudo yum -y install cuda-drivers

一共大概要安裝90多個依賴庫,注意一下安裝完成后的報告,我首次安裝時有一個庫不知道為什么安裝失敗了,又單獨yum install了該庫一次才成功

驗證安裝
/usr/local/cuda-9.2/bin/nvcc -V

安裝成功的話,會輸出類似文本:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Tue_Jun_12_23:07:04_CDT_2018
Cuda compilation tools, release 9.2, V9.2.148

重新編譯ffmpeg

要讓ffmpeg能夠使用CUDA提供的GPU編解碼器,必須重新編譯ffmpeg,讓其能夠通過動態鏈接調用CUDA的能力

首先要編譯安裝nv-codec-headers庫

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
make PREFIX="/home/local/ffmpeg_build" BINDDIR="/home/local/bin"
make install PREFIX="/home/local/ffmpeg_build" BINDDIR="/home/local/bin" 

進入/home/local/ffmepg_sources/ffmpeg-3.3.8/目錄重新執行ffmpeg的編譯和安裝
注意configure命令參數和之前configure命令參數的區別

PATH="/home/local/ffmpeg_sources/bin:$PATH" PKG_CONFIG_PATH="/home/local/ffmpeg_sources/ffmpeg_build/lib/pkgconfig" ./configure   --prefix="/home/local/ffmpeg_sources/ffmpeg_build" --pkg-config-flags="--static"   --extra-cflags="-I /home/local/ffmpeg_sources/ffmpeg_build/include -I/usr/local/cuda/include"   --extra-ldflags="-L /home/local/ffmpeg_sources/ffmpeg_build/lib  -L/usr/local/cuda/lib64"   --extra-libs=-lpthread   --extra-libs=-lm   --bindir="$HOME/bin"   --enable-gpl   --enable-libfdk_aac   --enable-libfreetype   --enable-libmp3lame   --enable-libopus   --enable-libvorbis   --enable-libvpx   --enable-libx264   --enable-libx265   --enable-nonfree  --enable-libfreetype --enable-cuda --enable-cuvid  --enable-nvenc --enable-libnpp
驗證安裝

重新安裝完ffmpeg,使用ffmpeg -hwaccels命令查看支持的硬件加速選項

Hardware acceleration methods:
cuvid

可以看到多出來一種叫做cuvid的硬件加速選項,這就是CUDA提供的GPU視頻編解碼加速選項

然后查看cuvid提供的GPU編解碼器ffmpeg -codecs | grep cuvid

 DEV.LS h364                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h364 h364_cuvid ) (encoders: libx264 libx264rgb h364_nvenc nvenc nvenc_h364 )
 DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_cuvid ) (encoders: libx265 nvenc_hevc hevc_nvenc )
 DEVIL. mjpeg                Motion JPEG (decoders: mjpeg mjpeg_cuvid )
 DEV.L. mpeg1video           MPEG-1 video (decoders: mpeg1video mpeg1_cuvid )
 DEV.L. mpeg2video           MPEG-2 video (decoders: mpeg2video mpegvideo mpeg2_cuvid )
 DEV.L. mpeg4                MPEG-4 part 2 (decoders: mpeg4 mpeg4_cuvid )
 D.V.L. vc1                  SMPTE VC-1 (decoders: vc1 vc1_cuvid )
 DEV.L. vp8                  On2 VP8 (decoders: vp8 libvpx vp8_cuvid ) (encoders: libvpx )
 DEV.L. vp9                  Google VP9 (decoders: vp9 libvpx-vp9 vp9_cuvid ) (encoders: libvpx-vp9 )

所有帶有"cuvid"或"nvenc"的,都是CUDA提供的GPU編解碼器
可以看到,我們現在可以進行h364/hevc/mjpeg/mpeg1/mpeg2/mpeg4/vc1/vp8/vp9格式的GPU解碼,以及h364/hevc格式的GPU編碼

使用GPU進行視頻轉碼

用GPU進行轉碼的命令和軟轉碼命令不太一樣,CPU轉碼的時候,我們可以依賴ffmpeg識別輸入視頻的編碼格式并選擇對應的解碼器,但ffmpeg只會自動選擇CPU解碼器,要讓ffmpeg使用GPU解碼器,必須先用ffprobe識別出輸入視頻的編碼格式,然后在命令行中指定對應的GPU解碼器。

例如,將h364編碼的源視頻轉碼為指定尺寸和碼率的h364編碼視頻:

ffmpeg -hwaccel cuvid -c:v h364_cuvid -i <input> -c:v h364_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y <output>
  • -hwaccel cuvid:指定使用cuvid硬件加速
  • -c:v h364_cuvid:使用h364_cuvid進行視頻解碼
  • -c:v h364_nvenc:使用h364_nvenc進行視頻編碼
  • -vf scale_npp=1280:-1:指定輸出視頻的寬高,注意,這里和軟解碼時使用的-vf scale=x:x不一樣

轉碼期間使用nvidia-smi查看顯卡狀態,能夠看到ffmpeg確實是在使用GPU進行轉碼:

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0     62543      C   ffmpeg                                       193MiB |
+-----------------------------------------------------------------------------+

GPU轉碼效率測試

在配有兩顆Intel-E5-2630v3 CPU和兩塊Nvidia Tesla M4顯卡的服務器上,進行h364視頻轉碼測試,成績如下:

  • GPU轉碼平均耗時:8s
  • CPU轉碼平均耗時:25s

并行轉碼時,CPU軟轉的效率有所提高,3個轉碼任務并行時32顆核心全被占滿,此時的成績

  • GPU轉碼平均耗時:8s
  • CPU轉碼平均耗時:18s

不難看出,并行時GPU的轉碼速度并沒有提高,可見一顆GPU同時只能執行一個轉碼任務。那么,如果服務器上插有多塊顯卡,ffmpeg是否會使用多顆GPU進行并行轉碼呢?

很遺憾,答案是否。

ffmpeg并不具備自動向不同GPU分配轉碼任務的能力,但經過一番調查后,發現可以通過-hwaccel_device參數指定轉碼任務使用的GPU!

向不同GPU提交轉碼任務

ffmpeg -hwaccel cuvid -hwaccel_device 0 -c:v h364_cuvid -i <input> -c:v h364_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y <output>
ffmpeg -hwaccel cuvid -hwaccel_device 1 -c:v h364_cuvid -i <input> -c:v h364_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y <output>
  • -hwaccel_device N:指定某顆GPU執行轉碼任務,N為數字

此時nvidia-smi顯示:

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0     96931      C   ffmpeg                                       193MiB |
|    1     96930      C   ffmpeg                                       193MiB |
+-----------------------------------------------------------------------------+

可以進行并行GPU轉碼了!

那么在占滿服務器資源時,GPU轉碼和CPU轉碼的效率如下:

  • GPU轉碼平均耗時:4s
  • CPU轉碼平均耗時:18s

GPU效率是CPU的4.5倍

排坑

一、錯誤: ./autogen.sh: 4: ./autogen.sh: autoreconf: not found

解決:yum -y install autoconf  automake libtool  
然后執行

二、 錯誤:ERROR: freetype2 not found using pkg-config

yum install -y bzip2
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.10.0.tar.bz2
tar -jxvf freetype-2.10.0.tar.bz2
cd freetype-2.10.0
./configure --prefix= /home/local/ffmpeg_sources/ffmpeg_build
make 
make install 

三、錯誤: configure: error: C compiler cannot create executables See `config.log' for more details make: *** [setup] Error 77

解決:先卸載舊的c c++ gcc gcc++

然后重新安裝

四、錯誤:make: *** [libavcodec/libfdk-aacenc.o] Error 1

因本身3.*帶有BUG換成4.1 官網下載地址

(此步驟也可以使用git clone下載源碼包,本質上是一樣的 )

然后重新編譯即可。

參考鏈接:https://www.jianshu.com/p/59da3d350488

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

师宗县| 枣强县| 日土县| 株洲县| 泸西县| 剑川县| 南通市| 曲周县| 金昌市| 日土县| 吉林市| 鲁山县| 澎湖县| 光泽县| 泊头市| 泰安市| 县级市| 汉源县| 奉节县| 金川县| 英山县| 汨罗市| 长葛市| 广东省| 鲁山县| 嘉荫县| 浦城县| 中超| 岚皋县| 桑日县| 五指山市| 莱芜市| 博客| 福泉市| 牡丹江市| 富民县| 东丽区| 密山市| 宾阳县| 扎鲁特旗| 威远县|