This is a script that converts AVC/x.264 mkv files to HEVC/x.265 using the latest version of NVENC that is included with the nVidia GTX 1050 and higher (it *might* work with the GTX 9xx cards, as they only had limited HEVC encode support - YMMV) and REQUIRES one to be present in the system. It does NOT work with GT series cards like the GT 1030, as nVidia does not include the NVENC engine in those GPUs.
It automatically converts all of the video tracks inside the mkv container while copying all audio and subtitle tracks in an untouched state. It also cleans up the residual files in your download, while keeping .srt, .idx, and .sub files intact.
I designed it to work alongside Sonarr and Radarr, but will likely work with other download apps. It relies on all files unpacking into the base directory like most downloads do, and will fail but leave the files untouched if that is not the case (as will Sonarr and Radarr). You can manually convert such failures using the code below, copy them into the base directory, and Sonarr and Radarr will either see them automatically or you can manually point at them if they don't.
This process has a very strong impact on disk space used for your files, as HVEC files are 33% to 66% smaller than AVC. There is a loss in video quality when encoding with NVENC, but it's not really noticeable to me on my 70" 1080p TV, your preference will vary. It's a trade off - if disk space is more important to you than pristine quality on your rips, this will save you a LOT of space.
Speed, in my media server (Core i3 6100 with a GTX 1050, with my work directory on standard 7200RPM NAS disks) it will encode a Blu-Ray rip at around 5x speed. It's faster on some things, slower on others, but 5x seems to be a good average. If you do your work on SSD, it is considerably faster, 7.5x to 12x depending on the file. If you have a RAMDISK, it will probably be even faster, but that would need to be a very large disk, large enough to hold both files at the same time.
Keep in mind that not every playback device can directly play HEVC, so be sure to check. Also note that if you are using Plex, any videos played on the web interface will be transcoded no matter what your hardware supports since that player does not support HEVC hardware acceleration. This may have a large impact on the performance of your media server, especially for 4K files. For Windows users who have issues with the Windows 10 Plex client (which is most of them), I highly recommend using Kodi with the Plex plugin. I even do this on Android TV, because it supports direct audio streaming better than the native Plex client. Remember if you do go with Plex on Kodi to check the box in Plex settings there to enable HEVC support.
And as per all scripts, use at your own risk, and ALWAYS test on something before you put it into full production.
Code: Select all
@ECHO OFF
ECHO.
ECHO Begin converting MKV video files from h.264 to h.265 via nVidia hardware accelleration (NVENC) while leaving audio and subtitles untouched.
ECHO.
REM Script by illrigger, based on work by BrooBee
REM This script REQUIRES an nVidia GeForce GTX 1050 or higher card be present in the system. It will NOT work with earlier generation cards or the GT1030 (which lacks NVENC).
REM It is intended to be used with SABNZBD+ as a post-process script in combination with Sonarr and Radarr on a Windows system.
REM Change these variables to match your system
SET FFMPEG=C:\ffmpeg\bin\ffmpeg.exe
SET OldAVI=avi
SET OldMKV=mkv
SET OldMP4=mp4
SET NewFormat=mkv
REM You shouldn't need to edit anything below here. Only if you add another format (Container) OldASF, OldWMV etc.
ECHO Looking in "%~1" for %OldAVI% or %OldMKV% files. Using %FFMPEG% to convert %OldAVI% or %OldMKV% to %NewFormat%...
ECHO.
ECHO Deleting unnecessary Files...
ECHO.
del /Q /F "%~1\*.*sample*","%~1\*.srr","%~1\*.jpg","%~1\*.url","%~1\*.pdf"
ECHO.
ECHO These are the files to convert:
ECHO.
for %%i IN ("%~1\*.%OldAVI%","%~1\*.%OldMKV%","%~1\*.%OldMP4%") DO (ECHO "%%i")
ECHO.
ECHO.
ECHO Starting Conversion (If there are any files)
ECHO.
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~START CONVERTING~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
md "%~1"\convert
for %%i IN ("%~1\*.%OldAVI%","%~1\*.%OldMKV%","%~1\*.%OldMP4%") DO (%FFMPEG% -y -hwaccel dxva2 -i "%%i" -map 0 -c:v hevc_nvenc -c:a copy -c:s copy "%~1\convert\%%~ni.%NewFormat%")
REM Pause for 5 seconds once processing is complete to let things settle a bit
PING 127.0.0.1 -n 1 -w 5000 >NUL
REM This part checks to see if the old format files were converted to new format
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END CONVERTING~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO.
ECHO Checking for new format files in %~1
ECHO.
IF EXIST "%~1\convert\*.%NewFormat%" (
ECHO.
ECHO Found %NewFormat% in folder!
ECHO.
ECHO Deleting old files then
ECHO.
ECHO Pause for 5 seconds before trying to delete files
ECHO.
PING 127.0.0.1 -n 1 -w 5000 >NUL
ECHO Copying subtitle files to conversion folder and cleaning up root folder
ECHO.
copy "%~1\*.srt","%~1\*.idx","%~1\*.sub" "%~1\convert\"
del /Q /F "%~1\*.*"
ECHO Moving h.256 mkv file to root folder and cleaning up conversion folder
copy "%~1\convert\*.*" "%~1"
del /Q /F "%~1\convert\*.*"
rd "%~1"\convert
)
ECHO.
ECHO Done!
Code: Select all
for /R %%a in ("*.mkv") do (ffmpeg -y -hwaccel dxva2 -i "%%a" -map 0 -c:v hevc_nvenc -c:a copy -c:s copy "%%~na-2.mkv")