Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jessielw/f5d5dd30ca21d94ade19c7455c9b6afe to your computer and use it in GitHub Desktop.
Save jessielw/f5d5dd30ca21d94ade19c7455c9b6afe to your computer and use it in GitHub Desktop.
TV Series FFMPEG + AviSynth encoding batch script
@echo off
setlocal EnableDelayedExpansion
REM YOU MUST HAVE AviSynthPlus_3.7.0_20210111.exe and
REM AviSynthPlus_3.7.0_20210111_vcredist.exe insatlled
REM You can get both of those from https://github.com/AviSynth/AviSynthPlus/releases/
REM
REM You will also need https://github.com/FFMS/ffms2/releases ffms2 indexer installed
REM with the plugins in the the proper directories
REM
REM You also must have FFMPEG installed, either define the path or add it
REM to your enviormental variables in windows
REM ############## Directory to save finished jobs in #################
REM No quotes when setting variables for directories
REM automatic output directory is %cd%\Completed_Jobs
set output_directory=%cd%\Completed_Jobs
if not exist "%output_directory%" mkdir "%output_directory%"
REM automatic temp directory is %cd%\Temp_Files
set temp_directory=%cd%\Temp_Files
if not exist "%temp_directory%" mkdir "%temp_directory%"
REM ############## Directory to save finished jobs in #################
REM ############## Path to ffmsindex.exe #################
set ffmsindex="C:\Program Files (x86)\AviSynth+\plugins64+\ffmsindex.exe"
REM ############## Path to ffmsindex.exe #################
REM ############## AviSynth dll location #################
set AviSynth_dll="C:\Program Files (x86)\AviSynth+\plugins64+\ffms2.dll"
REM ############## AviSynth dll location #################
REM ############## FFMPEG location #################
set ffmpeg_dir="C:\FFMPEG\bin\ffmpeg.exe"
REM ############## FFMPEG location #################
for %%a in ("%cd%\*.mkv") do (
REM ########### Open File ###########
@echo ###################################################
@echo Opening file
@echo '%%~na'
@echo ###################################################
REM ########### Open File ###########
REM
REM
REM
REM
REM ########### Indexing Input with ffmsindex.exe ###########
!ffmsindex! -f "%%a" "!temp_directory!\%%~na.ffindex"
REM ########### Indexing Input with ffmsindex.exe ###########
REM
REM
REM
REM
REM ########### Write commands to avisynth script ###########
@echo LoadPlugin^(!AviSynth_dll!^) >> "!temp_directory!\%%~na.avs"
@echo FFVideoSource^("%%~dpa%%~nxa",cachefile="!temp_directory!\%%~na.ffindex"^) >> "!temp_directory!\%%~na.avs"
REM @echo Crop(0, 8, -0, -8)
REM FillBorders(left=2, top=0, right=0, bottom=0, mode=0)
REM BalanceBorders(0,0,8,0, thresh=128, blur=30) # (top, bottom, left, right)
REM ########### Write commands to avisynth script ###########
REM
REM
REM
REM
REM ############################ FFMPEG Commands ###############################
REM ############## Inputs to ffmpeg #################
REM Default avs video source (-map 0:v:x)
set ffmpeg_input1=-i "!temp_directory!\%%~na.avs"
REM Default source file input "%%a" (-map 1:x:x)
set ffmpeg_input2= -i "%%a"
set ffmpeg_input3=
set ffmpeg_input4=
set ffmpeg_input5=
set ffmpeg_input6=
set ffmpeg_input7=
set ffmpeg_inputs=!ffmpeg_input1!!ffmpeg_input2!!ffmpeg_input3!!ffmpeg_input4!!ffmpeg_input5!!ffmpeg_input6!!ffmpeg_input7!
REM set ffmpeg_inputX=%%~dp0%%~n0.(same name with different extension for input)
REM ############## Inputs to ffmpeg #################
REM
REM
REM
REM
REM ############## Video Commands #################
REM Video Commands MUST be the same
set ffmpeg_video_command=-map 0:v:0 -c:v:0 libx264 -preset veryslow -level 4.1 -b:v:0 5000k -profile high -psy-rd 1:0.15 -aq-mode 3 -fast-pskip false -mbtree false -fastfirstpass false -maxrate 7500k -bufsize 15000k -deblock -1:-1 -x264-params "no-dct-decimate=true:colorprim=bt709:colormatrix=bt709:transfer=bt709"
REM Set metadata
set ffmpeg_video_metadata=-metadata:s:v:0 title="Pretty Little Liars" -metadata:s:v:0 language=eng -disposition:v:1 default
REM ############## FFMPEG Pass 1 Command #################
REM Pass #1 will be video only
set ffmpeg_command_pass1=!ffmpeg_video_command! -an -sn -map_chapters -1 -pass 1 -passlogfile
REM Pass #2 is where you can add audio, subtitles and chapters
REM ############## Video Commands #################
REM
REM
REM
REM
REM ############## Audio and Subtitle Commands #################
REM audio -map 1:a:x subtitle -map 1:s:x (based off of source file)
set audio_1=-map 1:a:0 -c:a:0 copy -metadata:s:a:0 title="AC-3 5.1" -metadata:s:a:0 language=eng -disposition:a:0 default
set audio_2=
set audio_3=
set audio_4=
set subtitle_1= -map 1:s:1? -c:s:0 copy -metadata:s:s:0 title="English (SRT)" -metadata:s:s:0 language=eng -disposition:s:0 0
set subtitle_2=
set subtitle_3=
set subtitle_4=
REM ############## Audio and Subtitle Commands #################
REM
REM
REM
REM
REM ############## FFMPEG Pass 2 Command #################
set ffmpeg_command_pass2=!ffmpeg_video_command! !ffmpeg_video_metadata! -pass 2 -passlogfile
REM ############################ FFMPEG Commands ###############################
REM
REM ########### Starting Pass 1 ###########
@echo ###################################################
@echo Started video encoding pass 1:
@echo "%%~na"
@echo ###################################################
!ffmpeg_dir! -analyzeduration 100M -probesize 50M !ffmpeg_input1! !ffmpeg_command_pass1! "!temp_directory!\%%~na" -max_interleave_delta 90000000 -y -f matroska NUL -hide_banner -loglevel error -stats
REM ########### Starting Pass 1 ###########
REM
REM
REM ########### Starting Pass 2 ###########
@echo ###################################################
@echo Started video encoding pass 2:
@echo "%%~na"
@echo ###################################################
!ffmpeg_dir! -analyzeduration 100M -probesize 50M !ffmpeg_inputs! !ffmpeg_command_pass2! "!temp_directory!\%%~na" !audio_1!!audio_2!!audio_3!!audio_4!!subtitle_1!!subtitle_2!!subtitle_3!!subtitle_4! -max_interleave_delta 90000000 -f matroska "!output_directory!\%%~na.mkv" -hide_banner -loglevel error -stats
REM ########### Starting Pass 2 ###########
REM
REM
REM ########### Echo output in shell ###########
@echo ###################### Completed Encode! ######################
@echo Output is:
@echo "!output_directory!\%%~na.mkv"
@echo ###################### Completed Encode! ######################
REM ########### Echo output in shell ###########
@echo.
@echo.
)
REM ############## Deletes temp directory ##############
rmdir /s /q "%temp_directory%"
REM ############## Deletes temp directory ##############
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment