O 背景
相信很多人不滿足于FFmpeg已有的Filters池充,對于現(xiàn)有FFmpeg支持Animation動(dòng)畫能力很不滿足
確實(shí),F(xiàn)Fmpeg本身的Filter濾波器并非為動(dòng)畫而作旋膳,而是核心服務(wù)于編解碼/轉(zhuǎn)碼
的用戶需求
同時(shí)我們也知道,OpenGL
作為動(dòng)畫 2D/3D特效算法開發(fā)的首選能力途事,正好可以彌補(bǔ)FFmpeg對于特效渲染的短板验懊。
今天要給的解決方案:如何在FFmpeg Cmd中使用 OpenGL 自定義 Shader 渲染視頻媒體素材。
主要介紹
FFmpeg-Plus-OpenGL
濾鏡對于這個(gè)困境的解決方案尸变。
1 FFmpeg-Plus-OpenGL
FFmpeg-Plus-OpenGL
是一個(gè) FFmpeg 下支持OpenGL
的擴(kuò)展濾波器义图,此項(xiàng)目為一個(gè)Patch
,可以直接將濾鏡編譯到自己的FFmpeg4+
(FFmpeg3的濾鏡 Patch有所不同召烂,略微更改即可)
- 看下這套技術(shù)方案具體給我們帶來了哪些效果 (不支持GIF圖 可以到 https://github.com/numberwolf/FFmpeg-Plus-OpenGL 看)
source | render |
---|---|
image.png
|
image.png
|
使用步驟如下
安裝 編譯
-
相關(guān)依賴
-
Centos 7.x+ || Linux
-
第一步 安裝相關(guān)依賴
yum install -y glew* yum install -y glfw* # # If can not compile , you need # yum install -y libGLEW* yum install -y mesa yum install -y mesa-libGLU mesa-libGLU-devel
-
第二步(可選)
如果你在無顯卡環(huán)境下跑此濾鏡(比如你的生產(chǎn)環(huán)境服務(wù)器),需要xvfb的支持
yum install -y xorg-x11-server-Xvfb
-
-
Ubuntu || Linux
-
第一步 安裝相關(guān)依賴
apt-get install libglfw3-dev libglfw3 apt-get install libglew2.0 libglew-dev
-
第二步(可選)
如果你在無顯卡環(huán)境下跑此濾鏡(比如你的生產(chǎn)環(huán)境服務(wù)器),需要xvfb的支持
apt-get install xvfb
-
-
MacOS下編譯 (需要brew支持)
brew install glew glfw
-
-
編譯構(gòu)建
-
下載源碼
git clone https://github.com/numberwolf/FFmpeg-Plus-OpenGL.git git clone https://github.com/FFmpeg/FFmpeg.git # for 4.x+ cd FFmpeg # # Patch # cp ../FFmpeg-Plus-OpenGL/Plus-GL-Shader/vf_plusglshader.c libavfilter/ git apply ../FFmpeg-Plus-OpenGL/Plus-GL-Shader/libavfilter.diff
-
編譯
-
編譯的
必選項(xiàng)
- 和此濾鏡相關(guān)--enable-opengl
--extra-libs='-lGLEW -lglfw'
--enable-filter=plusglshader -
編譯示例 - 可以不參考此示例碱工,但是必須加上
必選項(xiàng)
#!/bin/bash ./configure \ --enable-cross-compile \ --pkg-config-flags="--static" \ --extra-ldflags="-lm -lz -llzma -lpthread" \ --extra-libs=-lpthread \ --extra-libs=-lm \ --enable-gpl \ --enable-libfdk_aac \ --enable-libfreetype \ --enable-libmp3lame \ --enable-libopus \ --enable-libvpx \ --enable-libx264 \ --enable-libx265 \ --enable-libass \ --enable-libfreetype \ --enable-libfontconfig \ --enable-libfribidi \ --enable-libwebp \ --enable-nonfree \ --disable-shared \ --enable-static \ --enable-opengl \ --extra-libs='-lGLEW -lglfw' \ --enable-filter=plusglshader make clean make -j16 make install
-
-
運(yùn)行
-
檢查
Plus-GL-Shader
:plusglshader
濾鏡是否安裝成功-
檢查命令
ffmpeg -help filter=plusglshader
-
正確輸出
ffmpeg version a0d68e65 Copyright (c) 2000-2020 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.10.44.4) configuration: --enable-cross-compile --pkg-config-flags=--static --extra-ldflags='-lm -lz -llzma -lpthread' --extra-libs=-lpthread --extra-libs=-lm --enable-gpl --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libx264 --enable-libx265 --enable-libass --enable-libfreetype --enable-libfontconfig --enable-libfribidi --enable-libwebp --enable-nonfree --disable-shared --enable-static --enable-opengl --extra-libs='-lGLEW -lglfw' --enable-filter=plusglshader libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100 Filter plusglshader Generic OpenGL shader filter Inputs: #0: default (video) Outputs: #0: default (video) plusglshader AVOptions: sdsource <string> ..FV...... gl fragment shader source path (default is render gray color) vxsource <string> ..FV...... gl vertex shader source path (default is render gray color) This filter has support for timeline through the 'enable' option.
-
-
使用
plusglfilter
轉(zhuǎn)碼渲染 (Transcoding)plusglshader=sdsource='./test_shader.gl':vxsource='./test_vertex.gl'
-
參數(shù)項(xiàng)
-
sdsource
: Fragment shader file path (Default is gray) -
vxsource
: Vertex shader file path (Default is gray)
-
-
濾鏡使用規(guī)則
plusglshader
plusglshader=sdsource='./test_shader.gl':vxsource='./test_vertex.gl'
-
用你自己的Shader渲染
-
- Fragment shader
Notice: playTime is timestamp, from 0 -> end
uniform sampler2D tex; uniform float playTime; varying vec2 texCoord; void main() { gl_FragColor = texture2D(tex, texCoord * 0.5 + 0.5); float usePts = max(playTime, 0.4); float gray = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / (usePts * usePts); gl_FragColor.r = gray; gl_FragColor.g = gray; gl_FragColor.b = gray; }
-
- Vertex shader
attribute vec2 position; varying vec2 texCoord; void main(void) { gl_Position = vec4(position, 0, 1); texCoord = position; }
-
-
如果在無頭(無顯示器顯卡環(huán)境下)環(huán)境下使用
你需要xvfb server支持,使用如下命令
xvfb-run -a --server-args="-screen 0 1280x720x24 -ac -nolisten tcp -dpi 96 +extension RANDR" \ ffmpeg -v debug \ -i 1000p10s_9k.mp4 \ -filter_complex "[0:v]plusglshader=sdsource='./test_shader.gl':vxsource='./test_vertex.gl'[1outgl];[1outgl]scale=1280:-2" \ -vcodec libx264 \ -an \ -pix_fmt yuv420p \ -y test.mp4
-
在有頭(顯卡 顯示器)環(huán)境下運(yùn)行
ffmpeg -v debug \ -i 1000p10s_9k.mp4 \ -filter_complex "[0:v]plusglshader=sdsource='./test_shader.gl':vxsource='./test_vertex.gl'[1outgl];[1outgl]scale=1280:-2" \ -vcodec libx264 \ -an \ -pix_fmt yuv420p \ -y test.mp4
-
輸出示例
Tag Src Dst Frame 原圖渲染
-
2 相關(guān)幫助
FFmpeg編譯相關(guān)幫助
- 如果你不知道如何編譯FFmpeg,請點(diǎn)這里https://trac.ffmpeg.org/wiki/CompilationGuide