Log in

View Full Version : add black bars on top and bottom


varekai
30th December 2018, 13:53
Hello!

I want to add black bars on top and bottom so I can keep my own subtitles in bottom black area.
The AR of mkv is 3840x1598 and I want to change that to 3840x2160.
Using ffmpeg the change works as desired expect for the colors that are washed out.

The mkv info is: 3840*1598 (2:40:1 at 24.000 FPS, HEVC (Main 10@L5.1High) (HDR10)
| Format: HEVC | Color space: YUV | Chroma subsampling 4:2:0 (Type 2) | Bit depth 10 bits | Bits/(Pixel*Frame): 0.127 |

Is it possible to add something in ffmpeg bat file to keep original colors?
Changing libx264 to libx265 doesn't make any difference to colors.

This is the bat file I use:
@echo off
set SOURCEPATH=X:\SOURCEPATH
set TARGETPATH=Y:\TARGETPATH

if not exist "%SOURCEPATH%" goto ErrorSource
if not exist "%TARGETPATH%" goto ErrorTarget

for %%i in ("%SOURCEPATH%\*.mkv") do (
start/b/wait/low ffmpeg -i "%%i" -vf "pad=3840:2160:(ow-iw)/2:(oh-ih)/2" -c:v libx264 -preset slow -crf 10 -c:a copy "%TARGETPATH%\%%~ni.mkv"
echo.
)
goto Exit

:ErrorSource
echo Source path "%SOURCEPATH%" does not exist.
goto :eof

:ErrorTarget
echo Target path "%TARGETPATH%" does not exist.
goto :eof

:Exit
echo Done.
echo.

qyot27
30th December 2018, 19:25
You have to preserve the HDR metadata to get the colors right (and then, obviously, play it back on an HDR monitor/TV or in a software player - like VLC or mpv - that can do tone-mapping for a non-HDR display). FFmpeg doesn't do that automatically. And it also will only work when the target format is HEVC. If you're dead-set on transcoding to H.264, you have to tone-map the video first with AviSynth or some other tool.

Use mediainfo to see what the parameters are; the end of the Video section should have fields that look like this (this was Blade Runner 2049, every movie can have their own individual settings):
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : BT.709
Mastering display luminance : min: 0.0050 cd/m2, max: 10000 cd/m2
Maximum Content Light Level : 500 cd/m2
Maximum Frame-Average Light Level : 200 cd/m2

These map into parameters given to FFmpeg/libx265's -x265-params setting as follows:
Color primaries = colorprim
Transfer characteristics = transfer
Matrix coefficients = colormatrix
Mastering display color primaries, Mastering display luminance = master-display (the luminance value is tacked onto the end)
Maximum Content Light Level, Maximum Frame-Average Light Level = max-cll (cll,fall)

'PQ' in Transfer characteristics is the same thing as SMPTE ST 2084. FFmpeg uses the latter notation, as seen below. The x265 docs state that the Display P3/BT.709 color primaries that 'Mastering display color primaries' references above is equal to setting G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1), although the L value is represented separately in Mediainfo as 'Master display luminance' (and can differ from 10000000,1, so be careful with that part). The max-cll parameter covers both the Content Light Level (CLL) and Frame-Average Light Level (FALL), with the values separated by a comma.

If the Master display luminance differs from the default value given for BT.709, you have to convert the values because Mediainfo displays the value in cd/m², while the value given to the master-display parameter uses whole numbers rather than decimals (min: 0.0001 cd/m2, max: 1000 cd/m2 == L(10000000,1)). I would assume that for movies that use custom values for the main display primaries that aren't equivalent to BT.709, Mediainfo actually shows the values and not a named placeholder, in which case it should be as simple as plugging in the values Mediainfo reports. I don't know if any of the discs I own have mastering display values that diverge from BT.709; the ones I've looked at didn't.

Using Blade Runner 2049 as the example again, this was the command given to -x265-params to preserve the HDR information given above when transcoding with FFmpeg:
-x265-params "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(100000000,50):max-cll=500,200"
As you can see, the Master display luminance in Mediainfo was min: 0.0050 cd/m2, max: 10000 cd/m2, and the value given to FFmpeg/libx265 was L(100000000,50).

varekai
30th December 2018, 20:27
@qyot27
Thank you so much for your input, much appreciated!
The mkv in question plays perfectly in MPC-HC and PotPlayer.
Colors are shown as they should.
When running through the ffmpeg bat file colors get washed out.
Was hoping I could add some lines in bat file to correct that.
I have to do some testing on the info you gave.
Maybe you could advise on what to put in the bat file?
And I'll consult my two best friends Trial & Error.

Kind regards

Edit, from MediaInfo:
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0050 cd/m2, max: 4000 cd/m2
Maximum Content Light Level : 1125 cd/m2
Maximum Frame-Average Light Level : 248 cd/m2

qyot27
30th December 2018, 21:08
That's what I'm saying; unless you tell FFmpeg the parameters of the HDR information, the result will be that washed-out look, because it doesn't preserve the HDR information from the source and reuse it if the output is compatible.

In the case of that above,
ffmpeg -i "%%i" -vf "pad=3840:2160:(ow-iw)/2:(oh-ih)/2" -c:v libx265 -preset slow -crf 10 -x265-params "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(4000000,5):max-cll=1125,248" -c:a copy "%TARGETPATH%\%%~ni.mkv"
Pass that to FFmpeg, encode a short (like, 10-second) clip by using -t 10, and run mediainfo on the new test file to see if it matches the original. Hopefully that build of FFmpeg has a 10-bit-capable libx265.

varekai
30th December 2018, 22:39
That worked and the short sample looks good but values from MediaInfo is different.
This is what I get from MediaInfo.
MediaInfo original mkv:
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0050 cd/m2, max: 4000 cd/m2
Maximum Content Light Level : 1125 cd/m2
Maximum Frame-Average Light Level : 248 cd/m2

MediaInfo through ffmpeg:
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0005 cd/m2, max: 400 cd/m2
Maximum Content Light Level : 1125 cd/m2
Maximum Frame-Average Light Level : 248 cd/m2

I'm not the smartest guy on the block but I saw the zeros not matching so I called my friends Trial & Error and guess what?
They said why not add a 0 in ffmpeg syntax... and voilą...
MediaInfo through ffmpeg:
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0005 cd/m2, max: 4000 cd/m2
Maximum Content Light Level : 1125 cd/m2
Maximum Frame-Average Light Level : 248 cd/m2

Thanks a lot for guiding me! Much appreciated!
Kind regards and a Happy New Year,
varekai

qyot27
30th December 2018, 23:48
You also need to make that minimum value of 5 a 50 in the FFmpeg portion to match the original. 0.005 =/= 0.0005. That's what I get for trying to simplify the multiplication.

varekai
30th December 2018, 23:58
You also need to make that minimum value of 5 a 50 in the FFmpeg portion to match the original. 0.005 =/= 0.0005. That's what I get for trying to simplify the multiplication.OK! Got it!
(40000000,50)
Will run the bat file overnight!
Thanks!

Edit:
Success! The video looks great! Thanks for your help!
From MediaInfo after running ffmpeg:
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0050 cd/m2, max: 4000 cd/m2
Maximum Content Light Level : 1125 cd/m2
Maximum Frame-Average Light Level : 248 cd/m2

varekai
10th February 2019, 11:12
Hello!

Using:
Win 7
ffmpeg-20190205-8522d21-win64

Adding black bars at top & bottom.
I'm curious about the values from MediaInfo.
Original video has:
Mastering display luminance: min: 0.0050 cd/m2, max: 3999 cd/m2
Don't know where to add that value in bat file?
It seems to change to:
Mastering display luminance: min: 0.0050 cd/m2, max: 4000 cd/m2
Tried to change the value:
L(40000000,50)
to:
L(39990000,50)
But the result is still:
Mastering display luminance: min: 0.0050 cd/m2, max: 4000 cd/m2

Just wanted to ask for input before I run a complete job.
Thanks in advance for any input...

---------------------------------------------------------

From MediaInfo original video

Color range : Limited
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0050 cd/m2, max: 3999 cd/m2
Maximum Content Light Level : 4000 cd/m2
Maximum Frame-Average Light Level : 1000 cd/m2

---------------------------------------------------------

@echo off
set SOURCEPATH=E:\SOURCEPATH
set TARGETPATH=D:\TARGETPATH

if not exist "%SOURCEPATH%" goto ErrorSource
if not exist "%TARGETPATH%" goto ErrorTarget

for %%i in ("%SOURCEPATH%\*.mkv") do (
start/b/wait/low ffmpeg -i "%%i" -vf "pad=3840:2160:(ow-iw)/2:(oh-ih)/2" -c:v libx265 -preset slow -crf 18 -x265-params

"colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(40000000,50):max-cll=4000,1000" -c:a

copy "%TARGETPATH%\%%~ni.mkv"
)
goto Exit

:ErrorSource
echo Source path "%SOURCEPATH%" does not exist.
goto :eof

:ErrorTarget
echo Target path "%TARGETPATH%" does not exist.
goto :eof

:Exit
echo Done.
echo.

---------------------------------------------------------

From MediaInfo after ffmpeg pad

Color range : Limited
Color primaries : BT.2020
Transfer characteristics : PQ
Matrix coefficients : BT.2020 non-constant
Mastering display color primaries : Display P3
Mastering display luminance : min: 0.0050 cd/m2, max: 4000 cd/m2
Maximum Content Light Level : 4000 cd/m2
Maximum Frame-Average Light Level : 1000 cd/m2

---------------------------------------------------------

varekai
14th February 2019, 10:24
Got it to work, took forever though...
Case closed! :D