View Single Post
Old 24th August 2021, 14:34   #3  |  Link
FranceBB
Broadcast Encoder
 
FranceBB's Avatar
 
Join Date: Nov 2013
Location: Royal Borough of Kensington & Chelsea, UK
Posts: 2,904
Well, it's been 10 days and alek2702 didn't reply with more details, so I'm gonna do some guess work and post a reply that will actually help people.

So, he said a 4K .mkv and he wants to get a FULL HD BD, so let's suppose we have a UHD 3840x2160 23.976fps progressive BT2100 HDR PQ 10bit H.265 file and that we want to go to H.264 FULL HD BT709 SDR for BD-25 compatibility.

Here is the AVS Script:

Code:
#Indexing
FFMpegSource2("UHD.mkv", atrack=-1)

#From 10bit planar to 16bit planar
ConvertBits(16)

#From YUV to XYZ with 16bit planar precision
ConvertYUVtoXYZ(Color=0, OutputMode=1, HDRMode=0, fullrange=false)

#From HDR PQ to SDR BT709 100 nits tonemapping with 16bit planar precision
ConvertXYZ_Reinhard_HDRtoSDR(exposure_X=2.5, contrast_X=0.9)

#From XYZ back to YUV with 16bit precision
ConvertXYZtoYUV(pColor=0)

#Downscale from UHD to FULL HD with 16bit precison
Spline64Resize(1920, 1080)

#From 4:4:4 planar 16bit to 4:2:0 planar 16bit
Converttoyuv420(matrix="Rec709", interlaced=false)

#Dithering down to 8bit planar with the Floyd Steinberg Error Diffusion
ConvertBits(bits=8, dither=1)

#Clipping
Limiter(min_luma=16, max_luma=235, min_chroma=16, max_chroma=240)

#Resampling
ResampleAudio(48000)

At this point, we just need x264 to create a BD-25 compatible file, so:



Quote:
avs4x264mod.exe "I:\AVS Script.avs" --x264-binary "x264.exe" --bitrate 25000 --preset medium --profile High --level 4.1 --keyint 24 --slices 4 --deblock -1:-1 --overscan show --range tv --log-level info --thread-input --opencl --colormatrix bt709 --transfer bt709 --colorprim bt709 --videoformat component --nal-hrd vbr --vbv-maxrate 40000 --vbv-bufsize 30000 --pass 1 --output NUL

avs4x264mod.exe "I:\AVS Script.avs" --x264-binary "x264.exe" --bitrate 25000 --preset medium --profile High --level 4.1 --keyint 24 --slices 4 --deblock -1:-1 --overscan show --range tv --log-level info --thread-input --opencl --colormatrix bt709 --transfer bt709 --colorprim bt709 --videoformat component --nal-hrd vbr --vbv-maxrate 40000 --vbv-bufsize 30000 --pass 2 --output "I:\temp\raw_video.h264"


ffmpeg.exe -i "I:\AVS Script.avs" -vn -sn -af loudnorm=I=-24:LRA=10:tp=-2 -c:a dca -b:a 1536k -ar 48000 -strict -2 "I:\temp\audio.dts"

ffmpeg.exe -i "I:\AVS Script.avs" -vn -sn -af loudnorm=I=-24:LRA=10:tp=-2 -c:a ac3 -b:a 384k -ac 2 -ar 48000 "I:\temp\audio.ac3"

C:/Programmi/MKVToolNix\mkvmerge.exe --output "\\VBOXSVR\Share_Windows_Linux\temp\raw_video.mkv" --language 0:und ^"^(^" \\VBOXSVR\Share_Windows_Linux\temp\raw_video.h264 ^"^)^"

ffmpeg.exe -i "\\VBOXSVR\Share_Windows_Linux\temp\raw_video.mkv" -i "\\VBOXSVR\Share_Windows_Linux\temp\audio.dts" -i "\\VBOXSVR\Share_Windows_Linux\temp\audio.ac3" -map 0:0 -map 1:0 -map 2:0 -c:v copy -c:a copy -c:a copy -metadata provider_name="Avisynth" -metadata service_provider="Avisynth" -metadata service_name="Avisynth" -mpegts_original_network_id 0x1122 -mpegts_transport_stream_id 0x3344 -mpegts_service_id 0x5566 -mpegts_pmt_start_pid 0x1500 -mpegts_start_pid 0x150 -mpegts_service_type "digital_tv" -mpegts_flags "resend_headers" "\\VBOXSVR\Share_Windows_Linux\temp\final.m2ts"

pause

This way we've been encoding the file with two passes in H.264 so that it's BD compliant and we've got the .h264 file. Then, we've encoded the audio in DTS and we've added a second track as AC3 for backward compatibility.
Last but not least we've been creating a temporary mkv file with our newly encoded video and audio which has then been remuxed to .m2ts called "final.m2ts" and it's ready to be burned on a disk or be played by a compatible Bluray player on the fly.


Cheers,
Frank

Last edited by FranceBB; 24th August 2021 at 14:37.
FranceBB is offline   Reply With Quote