cedarrapidsboy / pms-postprocessing

Post processing scripts to augment the Plex Media Server DVR functionality.
MIT License
18 stars 4 forks source link

Is there any plan to add QSV encoding ? #14

Closed rprihoda closed 3 years ago

rprihoda commented 3 years ago

Most Plex users use intel chips and with plex pass the hardware encoding feature uses Quick Sync to transcode. I was wonder if there was any chance of adding QSV support or at least test for it like you do with the nvidia encoder and use it if present. Example transcode with QSV:

 /usr/lib/plexmediaserver/Plex Transcoder -codec:0 h264 -hwaccel:0 vaapi -hwaccel_fallback_threshold:0 10 -hwaccel_output_format:0 vaapi -codec:1 dca -analyzeduration 20000000 -probesize 20000000 -i /mnt/movies/life.like.2019.1080p.bluray.x264-getit.mkv -filter_complex [0:0]hwupload[0];[0]scale_vaapi=w=1278:h=538:format=nv12[1];[1]hwupload[2] -filter_complex [0:1] aresample=async=1:ocl='stereo':rematrix_maxval=0.000000dB:osr=48000[3] -map [2] -metadata:s:0 language=eng -codec:0 h264_vaapi -b:0 1898k -maxrate:0 2531k -bufsize:0 5062k -r:0 23.975999999999999 -force_key_frames:0 expr:gte(t,0+n_forced*5) -map [3] -metadata:s:1 language=eng -codec:1 aac -b:1 148k -f dash -seg_duration 5 -init_seg_name init-stream$RepresentationID$.m4s -media_seg_name chunk-stream$RepresentationID$-$Number%05d$.m4s -window_size 5 -delete_removed false -skip_to_segment 1 -time_delta 0.0625 -manifest_name http://127.0.0.1:32400/video/:/transcode/session/zuthbshnwnudgfcfl21encku/28430603-d1fd-4502-8c71-46b3caefeca5/manifest?X-Plex-Http-Pipeline=infinite -avoid_negative_ts disabled -map_metadata -1 -map_chapters -1 dash -start_at_zero -copyts -vsync cfr -y -init_hw_device vaapi=vaapi: -hwaccel_device vaapi -filter_hw_device vaapi -nostats -loglevel quiet -loglevel_plex error -progressurl http://127.0.0.1:32400/video/:/transcode/session/zuthbshnwnudgfcfl21encku/28430603-d1fd-4502-8c71-46b3caefeca5/progress
****
cedarrapidsboy commented 3 years ago

I may have to play more with VAAPI. My first reaction is... I cannot test it since I do not have an Intel GPU at my disposal. But, VAAPI is an open standard, so perhaps it would even work with my nVidia GPU?

Regardless, here is an untested version of the file. You may want to give it a try and report back. Hopefully, the worst that will happen is the HW accel will fail and it will revert to SW. Logs are appreciated.

https://github.com/cedarrapidsboy/pms-postprocessing/blob/qsv_encoding/transcode_internal.sh

rprihoda commented 3 years ago

I'll test it soon and report back, thanks.

rprihoda commented 3 years ago

Update: Tested and failed back to software encoding.

20200908-130738 [U2KLMrLy] INFO: transcode_internal.sh : Starting postprocessing for /mnt/tv/.grab/aac0aecc884a881eea2b7d992df15e185eb8b205-3340ab8fb90c4e22f7b40e1f2f86245a6f122315/KXAN News at Noon (2007) - 2020-09-08 12 00 00 - KXAN News at Noon.ts.
INFO - FFMPEG_EXTERNAL_LIBS: /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Codecs/96550d8-3281-linux-x86_64/
mpeg2video 1920 1080
29.97
INFO -  De-interlace filter: ""
INFO -            Framerate: 29.97
INFO -   Calculated bitrate: 4474
INFO - Encoding buffer (3x): 13422
INFO -           Dimensions: 1920 x 1080
20200908-130827 [U2KLMrLy] INFO: transcode_internal.sh : Video and audio transcoding starting.
INFO -     Max bitrate (2x): 8948
20200908-130827 [U2KLMrLy] DEBUG: transcode_internal.sh : /mnt/tv/.grab/aac0aecc884a881eea2b7d992df15e185eb8b205-3340ab8fb90c4e22f7b40e1f2f86245a6f122315/KXAN News at Noon (2007) - 2020-09-08 12 00 00 - KXAN News at Noon.ts;mpeg2video;1920;1080;29.97;;4474;8948;13422.
20200908-130827 [U2KLMrLy] WARNING: 1 : transcode_internal.sh : Fail-over to libx264.
20200908-132649 [U2KLMrLy] INFO: transcode_internal.sh : Transcoding complete. /tmp/transcode.CyjZdZVt.mkv created.
20200908-132649 [U2KLMrLy] INFO: transcode_internal.sh : Remuxing /tmp/working.sqXv19Ew.mkv into /tmp/transcode.59pV9F78.mp4.
20200908-132707 [U2KLMrLy] INFO: transcode_internal.sh : Moved /tmp/transcode.59pV9F78.mp4 to /mnt/tv/.grab/aac0aecc884a881eea2b7d992df15e185eb8b205-3340ab8fb90c4e22f7b40e1f2f86245a6f122315/KXAN News at Noon (2007) - 2020-09-08 12 00 00 - KXAN News at Noon.mp4.
20200908-132708 [U2KLMrLy] INFO: transcode_internal.sh : Original file format not wanted. Removed /mnt/tv/.grab/aac0aecc884a881eea2b7d992df15e185eb8b205-3340ab8fb90c4e22f7b40e1f2f86245a6f122315/KXAN News at Noon (2007) - 2020-09-08 12 00 00 - KXAN News at Noon.ts.
20200908-132708 [U2KLMrLy] INFO: transcode_internal.sh : Postprocessing completed. Removed /tmp/transcode.59pV9F78.mp4.
cedarrapidsboy commented 3 years ago

Thank you for confirming that. I'm interested in it, but won't be able to do a lot of work on it unless I can setup an easy test bed. A couple things I don't know... can I setup my nvidia system to use vaapi? If so, can Plex's vaapi encoder then be tested with an nvidia chipset? I'll post back here if I make any progress.

cedarrapidsboy commented 3 years ago

No progress. I did try moving my Plex Pass system over to a NAS that has the minimum QSV-compatible CPU (J1900), but could not get Plex to do any hardware transcoding, period. The video device was available to it (/dev/dri/card0|renderD128), it simply wouldn't do it. So, afraid with no way to locally test QSV or VAAPI, I'll be unable to work this request.

rprihoda commented 3 years ago

No problem. I understand. If I get any free time I'll try and do some work on it.

fdarveau commented 2 years ago

Sorry to revive this issue. I stumbled upon it looking for something else and couldn't help my curiosity since I needed QSV support a while ago. I'm posting this in case other people end up here looking for a solution or you have some more time to look into "officially" supporting QSV.

After some research at that time, I was able to make QSV work by using this command :

/usr/lib/plexmediaserver/Plex\ Transcoder -y -hide_banner \
-init_hw_device vaapi=intel:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device intel -filter_hw_device intel -i "${WORKINGFILE}" \
-vf 'format=vaapi,hwupload' -c:v h264_vaapi -b:v ${BITRATE}k -maxrate:v ${BITMAX}k -profile:v high \
-bf:v 3 -bufsize:v ${BUFFER}k -preset:v hq -forced-idr:v 1 ${DEINT} \
${AUDIOPARMS} \
"${TEMPFILENAME}" > >(tee -a "${LOGFILE}") 2> >(tee -a "${ERRFILE}" >&2)

It might be needed to add an environment variable for the path to the render driver (-init_hw_device vaapi=intel:/dev/dri/renderD128) in case other people have a different configuration. I hard coded it in my version since that was my setup.

rprihoda commented 2 years ago

I'm still interested. I'll take your code for a test run soon. Thanks.