Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to copy HDR10 metadata from source to a transcode.
#!/bin/bash
# Pre-defined RGB & WP values for BT.2020
BT2020_xW=0.3127
BT2020_yW=0.3290
BT2020_xR=0.708
BT2020_yR=0.292
BT2020_xG=0.17
BT2020_yG=0.797
BT2020_xB=0.131
BT2020_yB=0.046
# Pre-defined RGB & WP values for P3-D65
P3D65_xW=0.3127
P3D65_yW=0.3290
P3D65_xR=0.680
P3D65_yR=0.320
P3D65_xG=0.265
P3D65_yG=0.690
P3D65_xB=0.150
P3D65_yB=0.060
# Extract values from source video
colourSpace=$(mediainfo --Inform="Video;%MasteringDisplay_ColorPrimaries%" "$1")
luminance=$(mediainfo --Inform="Video;%MasteringDisplay_Luminance%" "$1")
maxCLL=$(mediainfo --Inform="Video;%MaxCLL%" "$1")
maxFALL=$(mediainfo --Inform="Video;%MaxFALL%" "$1")
# Print results from MediaInfo
echo "Colour Space: $colourSpace"
echo "Luminance: $luminance"
echo "MaxCLL: $maxCLL"
echo "MaxFALL: $maxFALL"
# Change IFS to split strings on spaces
IFS_old="$IFS"
IFS=' '
# Extract max and min lumanances
read -ra luminances <<< "$luminance"
minLuminance=${luminances[1]}
maxLuminance=${luminances[4]}
# Extract MaxCLL
read -ra maxcll <<< "$maxCLL"
maxCLL_noUnit=${maxcll[0]}
# Extract MaxFALL
read -ra maxfall <<< "$maxFALL"
maxFALL_noUnit=${maxfall[0]}
# Reset IFS
IFS=$IFS_old
# Print extracted values
echo "Minimum luminance: $minLuminance"
echo "Maximum luminance: $maxLuminance"
echo "MaxCLL: $maxCLL_noUnit"
echo "MaxFALL: $maxFALL_noUnit"
# Edit second input based on values extracted from MediaInfo
if [ "$colourSpace" == "BT.2020" ]; then
mkvpropedit "$2" --edit track:v1 --set chromaticity-coordinates-red-x="$BT2020_xR" \
--set chromaticity-coordinates-red-y="$BT2020_yR" \
--set chromaticity-coordinates-green-x="$BT2020_xG" \
--set chromaticity-coordinates-green-y="$BT2020_yG" \
--set chromaticity-coordinates-blue-x="$BT2020_xB" \
--set chromaticity-coordinates-blue-y="$BT2020_yB" \
--set white-coordinates-x="$BT2020_xW" \
--set white-coordinates-y="$BT2020_yW" \
--set max-luminance="$maxLuminance" \
--set min-luminance="$minLuminance" \
--set max-content-light="$maxCLL_noUnit" \
--set max-frame-light="$maxFALL_noUnit"
elif [ "$colourSpace" == "Display P3" ]; then
mkvpropedit "$2" --edit track:v1 --set chromaticity-coordinates-red-x="$P3D65_xR" \
--set chromaticity-coordinates-red-y="$P3D65_yR" \
--set chromaticity-coordinates-green-x="$P3D65_xG" \
--set chromaticity-coordinates-green-y="$P3D65_yG" \
--set chromaticity-coordinates-blue-x="$P3D65_xB" \
--set chromaticity-coordinates-blue-y="$P3D65_yB" \
--set white-coordinates-x="$P3D65_xW" \
--set white-coordinates-y="$P3D65_yW" \
--set max-luminance="$maxLuminance" \
--set min-luminance="$minLuminance" \
--set max-content-light="$maxCLL_noUnit" \
--set max-frame-light="$maxFALL_noUnit"
else
echo "Unknown colour space: $colourSpace"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment