Script: Convert from AVC to HEVC via NVENC

Come up with a useful post-processing script? Share it here!
Post Reply
illrigger
Newbie
Newbie
Posts: 5
Joined: May 18th, 2016, 11:17 am

Script: Convert from AVC to HEVC via NVENC

Post by illrigger »

It took me a while to get the major kinks out of this, so I thought I would post it and save someone else some time. It's based on a script I pulled from a post by BrooBee, so credit to him for the framework that makes it work so well in SAB.

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!
I also have a standalone version that will parse a tree and convert any MKVs it finds. Be careful as it won't care if it's already HEVC or not. Also note that Sonarr and Radarr have a habit of re-detecting the HEVCs you manually shrunk as DVD or SDTV quality, so you may have to manually make some adjustments to keep them from downloading a new "higher quality" version.

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")
If anyone has any questions or suggestions on how to make this better, feel free to post below.
User avatar
OneCD
Hero Member
Hero Member
Posts: 557
Joined: March 4th, 2017, 3:47 pm

Re: Script: Convert from AVC to HEVC via NVENC

Post by OneCD »

Ah, haven't see a batch file for years - kinda takes me back to the bad-old DOS days. Thanks for posting. ;D

BTW: that's an interesting way to make it "sleep" for 5 seconds. ;)
Stuff I like: Apache bash cron DD-WRT Debian DNSMasq Entware FireFox GitHub ImageMagick Kate KDE LibreELEC Netrunner NFS NVIDIA OpenVPN Orvibo-S20 pfSense Python Raspberry-Pi RAID SABnzbd Transmission Usenet VirtualBox Watcher3 XFCE
illrigger
Newbie
Newbie
Posts: 5
Joined: May 18th, 2016, 11:17 am

Re: Script: Convert from AVC to HEVC via NVENC

Post by illrigger »

Heh, I'm an old Windows sysadmin. Batch stuff is my first stop for scripting if I can, since I know it's going to work without any additional stuff installed. :P
sparky002
Newbie
Newbie
Posts: 1
Joined: March 13th, 2018, 10:44 pm

Re: Script: Convert from AVC to HEVC via NVENC

Post by sparky002 »

This is awesome! Good work. One issue that may or may not be your fault or something that can be changed. How can I run this script on multiple downloaded files from sabnzbd at once?
illrigger
Newbie
Newbie
Posts: 5
Joined: May 18th, 2016, 11:17 am

Re: Script: Convert from AVC to HEVC via NVENC

Post by illrigger »

sparky002 wrote: March 13th, 2018, 10:47 pm This is awesome! Good work. One issue that may or may not be your fault or something that can be changed. How can I run this script on multiple downloaded files from sabnzbd at once?
Glad you like it!

You mean multiple files at the same time, or a bunch of files that are sitting there in the download folder?

If the former, I wouldn't even try. I have tried to do it via running multiple copies of ffmpeg at the same time, and the GPU bottlenecks it to the point where each thread added considerable time to the process vs a single thread doing all of them in sequence - from 10-15% with two threads up to 50% longer with 3 or 4 threads. The NVENC engine just doesn't do a great job with multithreading.

If the latter, it should be doing it already. It looks at all of the files in your temp folder, and and does a "for each" pass on each one until there are no more files there. If you want to do a backlog of files, the second small script at the bottom of my post will do a recursive scan of folders under the one you run it at, find every MKV under it, and convert them, leaving the converted files in the folder you ran it from. With SabNZBd, putting the script in your download routine should make sure that it finishes converting everything there before it processes the next file, so you should be ending up with an empty temp folder every time a file completes processing.

I am using Radarr and Sonarr as my download agents, so if you are using something different things might not work quite the same for you. If you haven't tried them, I highly recommend you do. They're head and shoulders above what Sickbeard/Sickrage do (and there isn't constant turmoil among the dev team), and equal to CouchPotato with more options and faster grab times. Radarr is still in alpha, but it's completely stable at this point as far as I can tell after several months of using it.
Post Reply