【教程】使用VapourSynth+ffmpeg將HLG視頻壓制為SDR
本文首發(fā)于:https://github.com/nilaoda/Blog/issues/16

前言
鑒于目前HLG
視頻越來越多,本教程旨在幫助小白從零開始學(xué)會將HLG
視頻壓制為普通的SDR
視頻并完成色彩轉(zhuǎn)換
使用VapourSynth
的好處是更加強(qiáng)大,可以做更多的事情
下載資源
下載ffmpeg
https://ffmpeg.zeranoe.com/builds/
解壓后將bin
目錄下的ffmpeg.exe
拿出來即可
下載VapourSynth
https://github.com/vapoursynth/vapoursynth/releases

下載VapourSynth Editor
https://bitbucket.org/mystery_keeper/vapoursynth-editor/downloads/
下載L-SMASH-Works
https://www.dropbox.com/sh/3i81ttxf028m1eh/AAABkQn4Y5w1k-toVhYLasmwa?dl=0&lst=

下載vapoursynth-tonemap
https://github.com/ifb/vapoursynth-tonemap/releases

下載Python便攜版
https://www.python.org/ftp/python/3.7.6/python-3.7.6-embed-amd64.zip

開始
資源下載完畢后,解壓VapourSynth64-Portable-R48.7z、python-3.7.6-embed-amd64.zip、VapourSynthEditor-r19-64bit
將python-3.7.6-embed-amd64、VapourSynthEditor-r19-64bit中的所有內(nèi)容移動(dòng)到VapourSynth64-Portable-R48文件夾中:

此文件夾下,在地址欄輸入cmd并回車:

測試環(huán)境
在cmd下,輸入python.exe進(jìn)入交互模式,并輸入:

配置插件
解壓L-SMASH-Works-r929-20170224-64bit.7z,將其中的\VapourSynth\vslsmashsource.dll復(fù)制到VapourSynth64-Portable-R48\vapoursynth64\plugins

測試
打開vsedit.exe,輸入如下代碼,加載我們的視頻文件,按下F6檢測腳本(需要等待其制作索引文件):
import vapoursynth as vs?
core = vs.get_core()?
src = core.lsmas.LWLibavSource(source=r"E:\星月神話_1080pHLG10.mp4")
src.set_output()
下方變綠說明沒有問題:

按下F5可以預(yù)覽視頻:

顯示色彩是欠飽和的,接下來就想辦法給他轉(zhuǎn)換到SDR

轉(zhuǎn)換
配置插件
解壓下載好的tonemap-r2-win64.7z,解壓后的tonemap.dll復(fù)制到VapourSynth64-Portable-R48\vapoursynth64\plugins

修改腳本
方案1:
import vapoursynth as vs
core = vs.get_core()
src = core.lsmas.LWLibavSource(source=r"E:\星月神話_1080pHLG10.mp4")
#參數(shù)需要根據(jù)不同視頻進(jìn)行微調(diào),不能做到普適
src = core.resize.Lanczos(clip=src, format=vs.RGBS, width=None, height=None,matrix_in_s="2020ncl",transfer_in_s="std-b67", transfer_s="linear",nominal_luminance=1000)
src = core.tonemap.Mobius(src, exposure=6, transition=0.2, peak=300)
src = core.resize.Lanczos(clip=src, format=vs.YUV420P10, matrix_s="709",primaries_in_s="2020",? primaries_s="709",transfer_in_s="linear", transfer_s="709")
src.set_output()
F5預(yù)覽效果:

方案2:
import vapoursynth as vs
core = vs.get_core()
src = core.lsmas.LWLibavSource(source=r"E:\星月神話_1080pHLG10.mp4")
#參數(shù)需要根據(jù)不同視頻進(jìn)行微調(diào),不能做到普適
src = core.resize.Lanczos(clip=src, format=vs.RGBS, width=None, height=None,matrix_in_s="2020ncl",transfer_in_s="std-b67", transfer_s="linear",nominal_luminance=1200)
src = core.tonemap.Hable(src, exposure=9, a=0.22, b=0.3, c=0.1, d=0.2, e=0.01, f=0.3, w=11.5)
src = core.resize.Lanczos(clip=src, format=vs.YUV420P10, matrix_s="709",primaries_in_s="2020",? primaries_s="709",transfer_in_s="linear", transfer_s="709")
src.set_output()
F5預(yù)覽效果:

方案3:
import vapoursynth as vs
core = vs.get_core()
src = core.lsmas.LWLibavSource(source=r"E:\星月神話_1080pHLG10.mp4")
#參數(shù)需要根據(jù)不同視頻進(jìn)行微調(diào),不能做到普適
src = core.resize.Lanczos(clip=src, format=vs.RGBS, width=None, height=None,matrix_in_s="2020ncl",transfer_in_s="std-b67", transfer_s="linear",nominal_luminance=1200)
src = core.tonemap.Reinhard(src, exposure=2.7, contrast=0.5, peak=1.0)
src = core.resize.Lanczos(clip=src, format=vs.YUV420P10, matrix_s="709",primaries_in_s="2020",? primaries_s="709",transfer_in_s="linear", transfer_s="709")
src.set_output()
F5預(yù)覽效果:


壓制
將腳本保存好(此為為test.vpy),就可以開始壓制了。我新建了Encode文件夾,方便整理。
通過VSPipe.exe喂給ffmpeg壓制:
..\VSPipe.exe --y4m "test.vpy" - | "ffmpeg" -y -i - -color_range pc -pix_fmt yuv420p test.mp4
參數(shù)什么的自己設(shè)置吧


打包分享 開箱即用
(自行修改為zip后綴解壓)
下載地址:http://pan.iqiyi.com/ext/paopao/?token=eJxjYGBgmBQtsZUBBNZ9qgMAFooD2Q.zip