[SNIPPET] HDR detection

All about user-defined episode / movie format expressions
Post Reply
devster
Posts: 391
Joined: 06 Jun 2017, 22:56

[SNIPPET] HDR detection

Post by devster »

As Mediainfo was updated to include HDR detection, this is a snippet to take full advantage of it.
The map currently detects HDR10, HDR10+ and Dolby Vision.

Code: Select all

{
	def _HDRMap = [
		"HDR10": "HDR10",
		"SMPTE ST 2086": "HDR10",
		"SMPTE ST 2094 App 4": "HDR10+",
		"Dolby Vision / SMPTE ST 2086": "Dolby Vision",
		"Dolby Vision / HDR10": "Dolby Vision",
	]
	def vid = video.first()
	allOf
		{ vf }
		{ vc }
		{ 
			String _HDR
			switch (bitdepth) {
				case { it > 8 }:
					_HDR = any
				   		{ vid["HDR_Format_Commercial"] }
				   		{ vid["HDR_Format"] }
				   		{ hdr }
				   		{ null }
						// { vid["HDR_Format/String"] }
						// { vid["HDR_Format_Compatibility"] }
						// following for both HDR10+ (misses compatibility) and Dolby Vision
						// { vid["HDR_Format_Version"] }
						// following only for Dolby Vision
						// { vid["HDR_Format_Profile"] }
						// { vid["HDR_Format_Level"] }
						// { vid["HDR_Format_Settings"] }
					break
				default:
					"$bitdepth-bit"
				break
			}
			// _HDRMap.get(_HDR, _HDR)
			_HDRMap.find { k, v ->
				k =~ _HDR
			}?.value
		}
	.join(" ")
}
Very unsure about the groovy at the end, this bit in particular:

Code: Select all

			_HDRMap.get(_HDR, _HDR)
			// OR
			_HDRMap.find { k, v ->
				k =~ _HDR
			}.value
which seems wonky but works for now.

UPDATE 2019-09-03 -> changed _HDRMap.find{...}.value to _HDRMap.find{...}?.value, should be null if no key is found.
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 19349
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: [SNIPPET] HDR detection

Post by rednoah »

What possible values does HDR_Format_Commercial in your test cases?

:!: If it can include a / SLASH then I'll need to update {hdr} accordingly to strip them before the result is being used as file path.
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 391
Joined: 06 Jun 2017, 22:56

Re: [SNIPPET] HDR detection

Post by devster »

The field is usually populated except for esoteric formats such as Hybrid Log-Gamma and SL-HDR1.

As corollary, the HDR designation only requires at least 10 bits, colour_primaries BT.2020 and PQ as transfer function (IIRC).
The last field should be available in mediainfo and could be an optional confirmation field.

UPDATE - some examples:

Code: Select all

HDR_Format                       : SMPTE ST 2086
HDR_Format/String                : SMPTE ST 2086, HDR10 compatible
HDR_Format_Commercial            : HDR10
HDR_Format_Compatibility         : HDR10
----------------------------------------
HDR_Format                       : Dolby Vision / SMPTE ST 2086
HDR_Format/String                : Dolby Vision, Version 1.0, dvhe.08.05, BL+RPU / SMPTE ST 2086, HDR10 compatible
HDR_Format_Commercial            : Dolby Vision / HDR10
HDR_Format_Version               : 1.0 /
HDR_Format_Profile               : dvhe.08 /
HDR_Format_Level                 : 05 /
HDR_Format_Settings              : BL+RPU /
HDR_Format_Compatibility         :  / HDR10
----------------------------------------
HDR_Format                       : SMPTE ST 2094 App 4
HDR_Format/String                : SMPTE ST 2094 App 4, Version 0
HDR_Format_Commercial            : SMPTE ST 2094 App 4
HDR_Format_Version               : 0
these are only few test cases, but they should cover HDR10, HDR10+ and Dolby Vision (regardless of profile).
I only work in black and sometimes very, very dark grey. (Batman)
devster
Posts: 391
Joined: 06 Jun 2017, 22:56

Re: [SNIPPET] HDR detection

Post by devster »

Code: Select all

{
	allOf
		{ vf }
		{ vc }
		{
			def _HDRMap = [
				"HDR10": "HDR10",
				"SMPTE ST 2086": "HDR10",
				"SMPTE ST 2094 App 4": "HDR10+",
				"Dolby Vision / SMPTE ST 2086": "Dolby Vision",
				"Dolby Vision / HDR10": "Dolby Vision",
			]
			def vid = video.first()
			if (bitdepth > 8) {
				String _HDR
				switch (vid) {
					case { it.findAll { p -> p =~ /^HDR_/ }.size() > 0 }:
						_HDR = any
							{ vid["HDR_Format_Commercial"] }
							{ vid["HDR_Format"] }
							{ hdr }
							{ null }
							// { vid["HDR_Format/String"] }
							// { vid["HDR_Format_Compatibility"] }
							// following for both HDR10+ (lacks HDR_Format_Compatibility) and Dolby Vision
							// { vid["HDR_Format_Version"] }
							// following only for Dolby Vision
							// { vid["HDR_Format_Profile"] }
							// { vid["HDR_Format_Level"] }
							// { vid["HDR_Format_Settings"] }
						
						_HDRMap.find { k, v ->
						  k =~ _HDR
						}?.value
						break
					case { it["transfer_characteristics"].findMatch(/HLG/) && it["colour_primaries"] == "BT.2020" }:
						"HLG"
						break
					case { it["transfer_characteristics"] == "PQ" && it["colour_primaries"] == "BT.2020" }:
						"HDR"
						break
					default:
						"$bitdepth-bit"
						break
				}
			}
		}
	.join(" ")
}
Updated to support HLG HDR format, at least in test cases.
Last edited by devster on 04 Sep 2019, 08:09, edited 1 time in total.
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 19349
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: [SNIPPET] HDR detection

Post by rednoah »

OK, I'll make {hdr} yield only Dolby Vision in this case (i.e. the first part before the slash)

Code: Select all

HDR_Format_Commercial            : Dolby Vision / HDR10
:idea: Please read the FAQ and How to Request Help.
mterrill
Posts: 28
Joined: 21 May 2018, 21:08

Re: [SNIPPET] HDR detection

Post by mterrill »

devster wrote: 03 Sep 2019, 22:17
Hi devster, Is there any update to the above code? i.e. updated hrd formats etc? I just want to add the words "HDR10" or "HDR10+" or "Dolby Vision" to the file and folder name....
devster
Posts: 391
Joined: 06 Jun 2017, 22:56

Re: [SNIPPET] HDR detection

Post by devster »

No, sorry, I don't have that many files with those characteristics.
If you have samples I can work on that though.
I only work in black and sometimes very, very dark grey. (Batman)
Post Reply