LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

MKV playback, recompression, remuxing, codec packs, players, howtos, etc.
plittlefield
Posts: 68
Joined: Wed May 10, 2017 11:04 am

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by plittlefield »

A few weeks on and I have some tweaks to the script, which now includes 'max-cll' variables.

I will create a github at some point, but for now, here are the important parts...

Code: Select all

# set main variables
TITLE="Movie Name (YEAR)"
INPUT="/home/user/${TITLE} UHD.mkv"
OUTPUT="/home/user/${TITLE} 4K ffmpeg 2 Audio.mkv"

# read hdr variables from source file
ffprobe -loglevel quiet -read_intervals "%+#2" -select_streams v:0 -show_entries side_data "${TITLE} UHD.mkv" | egrep -m 1 -A 10 'Mastering display metadata' | grep -v 'Mastering display metadata' >/tmp/variables.txt
ffprobe -loglevel quiet -read_intervals "%+#2" -select_streams v:0 -show_entries side_data "${TITLE} UHD.mkv" | egrep -m 1 -A 2 'Content light level metadata' | grep -v 'Content light level metadata' >>/tmp/variables.txt
source /tmp/variables.txt
cat /tmp/variables.txt

# ffmpeg transcode
ffmpeg -y -i "${INPUT}" -t 00:05:00 -hide_banner -stats -loglevel quiet -analyzeduration 100M -probesize 100M -max_muxing_queue_size 9999 -map 0:v:0 -map 0:a:0 -map 0:a:0 -vf "crop=3840:1600:0:280" -pix_fmt yuv420p10le -vcodec libx265 -preset slow -x265-params "keyint=60:bframes=3:vbv-bufsize=75000:vbv-maxrate=75000:colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display='G("$green_x","$green_y")B("$blue_x","$blue_y")R("$red_x","$red_y")WP("$white_point_x","$white_point_y")L("$max_luminance","$min_luminance")':max-cll='"$max_content","$max_average"'" -crf 24 -force_key_frames "expr:gte(t,n_forced*2)" -c:a:0 copy -c:a:1 dca -ab 1509k -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -metadata:s:a:1 language=eng -map_chapters -1 -sn -strict -2 "${OUTPUT}"
...please adjust as required.

I do a 5 minute test plus have dual audio for my current and future setup :-)

Enjoy.

Stay safe,

Paully
Solverz
Posts: 5
Joined: Tue Nov 12, 2019 8:02 pm

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by Solverz »

Sorry if this sounds obvious :lol:

But if I run that handy script, does it compress a 4k hdr mkv file? With acceptable quality?
plittlefield
Posts: 68
Joined: Wed May 10, 2017 11:04 am

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by plittlefield »

Solverz wrote:
Sat May 09, 2020 9:04 pm
Sorry if this sounds obvious :lol:

But if I run that handy script, does it compress a 4k hdr mkv file? With acceptable quality?
Yes.

A typical UHD REMUX goes down from 40GB to about 8GB.

Paully
plittlefield
Posts: 68
Joined: Wed May 10, 2017 11:04 am

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by plittlefield »

Solverz wrote:
Sat May 09, 2020 9:04 pm
Sorry if this sounds obvious :lol:

But if I run that handy script, does it compress a 4k hdr mkv file? With acceptable quality?
...and don't forget that 8GB size includes an extra audio track for MY tastes, so if you only wanted 1 audio track then that size would come down!

:-)

Paully
iXDanXi
Posts: 4
Joined: Thu May 14, 2020 3:36 pm

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by iXDanXi »

Hi all,

This seems like the best thread to ask, what would be the command line for ffmpeg to keep the 4k video untouched, take the first of multiple audio files but convert that hopefully lossless from dts to ac3 or any other lossless codec (any advice on a codec appreciated)

Thabks
Woodstock
Posts: 10326
Joined: Sun Jul 24, 2011 11:21 pm

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by Woodstock »

https://ffmpeg.org/ffmpeg.html#toc-Examples-1

There are a lot of possible command combinations. You're essentially asking for conversion to FLAC if you insist on lossless. For me, I haven't "heard" a reason to move away from a decent lossy format, because my hearing is perfect anyway.
plittlefield
Posts: 68
Joined: Wed May 10, 2017 11:04 am

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by plittlefield »

iXDanXi wrote:
Thu May 14, 2020 3:44 pm
Hi all,

This seems like the best thread to ask, what would be the command line for ffmpeg to keep the 4k video untouched, take the first of multiple audio files but convert that hopefully lossless from dts to ac3 or any other lossless codec (any advice on a codec appreciated)

Thabks
Wow, your ears must be good... because I find DTS absolutely amazing.

That aside, Dolby TrueHD found in most UHD's is lossless, at least according to mediainfo...

Code: Select all

Audio #1
ID                                       : 2
Format                                   : MLP FBA 16-ch
Format/Info                              : Meridian Lossless Packing FBA with 16-channel presentation
Commercial name                          : Dolby TrueHD with Dolby Atmos
Codec ID                                 : A_TRUEHD
Duration                                 : 1 h 37 min
Bit rate mode                            : Variable
Bit rate                                 : 7 644 kb/s
Maximum bit rate                         : 9 270 kb/s
Channel(s)                               : 8 channels
Channel layout                           : L R C LFE Ls Rs Lb Rb
Sampling rate                            : 48.0 kHz
Frame rate                               : 1 200.000 FPS (40 SPF)
Compression mode                         : Lossless
Stream size                              : 5.20 GiB (30%)
Language                                 : English
Default                                  : Yes
Forced                                   : No
Number of dynamic objects                : 13
Bed channel count                        : 1 channel
Bed channel configuration                : LFE
...so a straight acodec copy would be fine for you...

..which is, in fact, what part of my ffmpeg command line does!

:-)

Paully
plittlefield
Posts: 68
Joined: Wed May 10, 2017 11:04 am

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by plittlefield »

plittlefield wrote:
Sat Apr 25, 2020 9:36 am
A few weeks on and I have some tweaks to the script, which now includes 'max-cll' variables.
A tweak to the readhdrvalues function, which strips out anything after the / in the ffprobe output...

Code: Select all

function readhdrvalues () {
  echo "Reading HDR values..."
  echo $1
  ffprobe -loglevel quiet -read_intervals "%+#2" -select_streams v:0 -show_entries side_data "$1" | egrep -m 1 -A 10 'Mastering display metadata' | grep -v 'Mastering display 
metadata' | sed 's/\/.*//g' >/tmp/variables.txt
  ffprobe -loglevel quiet -read_intervals "%+#2" -select_streams v:0 -show_entries side_data "$1" | egrep -m 1 -A 2 'Content light level metadata' | grep -v 'Content light lev
el metadata' | sed 's/\/.*//g' >>/tmp/variables.txt
  source /tmp/variables.txt
  cat /tmp/variables.txt
  echo "...done."
}
Regards,

Paully
centuryx476
Posts: 25
Joined: Fri Apr 24, 2020 7:11 pm

Re: LINUX - ffmpeg - 4K HDR HEVC X265 - Plex

Post by centuryx476 »

I search the forum and found this excellent post.
I am doing hardware acceleration with these commands provided. Trying to encode 4K HDR HEVC, But I noticed something odd;

I am using -cq as a command and no matter what value I put in, the bitrate stays the same (At around 20,000) I can lower it all the way to 0 and it remains the same.

Has anyone noticed this ? Is this a bug ?

Edit:
I think I fixed it.
I have to include -maxrate and -bufsize
Now the -cq is working!
Post Reply