kh_repair.bat

Browser view only. Use the download link if you want to save the file.

Back to Toolbox
@echo off
echo. %~n0 ver 0.83
echo.

	:: pre-choose the font size. Cloud=18, cloud2=22, ninja=14
	::set nfmode=cloud2ver
	::set nfmode=cloudver
	set nfmode=ninjaver

set full=1
:: use %1 if they passed one in, otherwise pick it GUI style
if [%1] == [] goto error3
if exist %1 goto gotone

:filepick
	set dialog="about:<input type=file id=FILE><script>FILE.click();new ActiveXObject
	set dialog=%dialog%('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);
	set dialog=%dialog%close();resizeTo(0,0);</script>"

	for /f "tokens=* delims=" %%p in ('mshta.exe %dialog%') do set "file=%%p"
	rem echo Selected  file is : "%file%"
	rem timeout /t 2 >nul
	if not defined file goto error2
	goto preloop

:gotone
	set file=%~1
	goto preloop

:preloop
if exist ffprobe.exe call :showfiledata %file%
:: set all variables to defaults in case it's our 2nd run or later
set afix=
set scale=
set KFBoost=
set crop=
set NameFix=
set NewZoomName=
set sample=
set newext=
set m1type=
set makeMP3=
set makeMPG=
set infile=
set outfile=
set trim=
set splitat=
set split=

timeout /t 2 >nul

:looptop
cls
echo. %~n0 ver 0.6
echo.
:: first, figure out if any repairs or sample are enabled, so we can preview the output filename
	set is_repair=0
	if [%crop%] == [1] set is_repair=1
	if not [%scale%] == [] set is_repair=1
	if [%afix%] == [1] set is_repair=1
	if [%KFBoost%] == [1] set is_repair=1
	if [%NameFix%] == [1] set is_repair=1
	if [%trim%] == [1] set is_repair=1
	if [%split%] == [1] set is_repair=1
	set newext=
	if [%is_repair%] == [1] set newext=.repaired
	if [%sample%] == [1] set newext=%newext%.sample

title Displaying current settings
set disp=Source File : %file%  
if defined width set disp=Source File : %file%  %width%x%height%, %display_aspect_ratio%, %samplerate% 
echo. %disp% 
echo. Output File : %~n1%newext%.mp4
echo.

echo.
echo. Repairs you can make (these can be stacked)
set value=%crop%
set name=crop
set desc=Crop to 4x3
set help=C = Crops video to a 4x3 aspect ratio
call :showvalue

set value=%scale%
set name=scale
set desc=Resize to %scale%
set help=R = Resize the video to [%scale%] resolution
if [%scale%] == [] set help=R = Resize the video to some resolution
call :showvalue

set value=%afix%
set name=afix
set desc=Audiofix to 44.1k
set help=A = Audio to 44.1k sample rate, so YesLaw will sync it
call :showvalue

set value=%KFBoost%
set name=KFBoost
set desc=Key Frame Frequency Boost
set help=K = Key Frames occurance boost for better splits
call :showvalue

set value=%NameFix%
set name=NameFix
set desc=Name label in Zoom repair
set help=N = Name repair in Zoom - fix wrong name at bottom 
call :showvalue

set value=%sample%
set name=Sample
set desc=Sample of 15 seconds
set help=S = Sample - make a 15 second clip to check the video 
call :showvalue

set value=%trim%
set name=trim
set desc=Trim chunk from end of video
set disp1=T = Trim unwanted video from END of original
if defined trim set disp1=T = Trim unwanted video from END of original (@%chopat%)
set help=%disp1% 
call :showvalue

set value=%split%
set name=split
set desc=sPlit video somewhere in middle 
set disp1=P = sPlit video at some spot in the middle
if defined split set disp1=P = sPlit video at %splitat%  (MP4 only)
set help=%disp1% 
call :showvalue


echo.
echo. Additional files you can make:

set value=%makeMP3%
set name=makeMP3
set desc=3=Make MP3
set help=3 = Make an MP3 audio file from the ORIGINAL file 
call :showvalue

set value=%m1type%
set name=m1type
set desc=1=Make MPeg-1
set disp1=
if not [%m1type%] == [] set disp1= [%m1type%]
set disp2=ORIGINAL
if [%is_repair%] == [1] set disp2=REPAIRED
set help=1 = Make an MPeg-1 video%disp1% of the %disp2% file
if [%m1type%] == [] set help=1 = Make an MPeg-1 video of the %disp2% file
call :showvalue

echo.
title %~n0: Getting user choices
choice /c cqakrhdngs31tp /n /m "What now?   G=GO!!  Q=Quit "
	if errorlevel 14 goto togglesplit
	if errorlevel 13 goto toggletrim
	if errorlevel 12 goto togglempg
	if errorlevel 11 goto togglemp3
	if errorlevel 10 goto togglesample
	if errorlevel 9 goto BigLaunch
	if errorlevel 8 goto namerepair
	if errorlevel 7 goto showdetails
	if errorlevel 6 goto togglehelp
	if errorlevel 5 goto togglescale
	if errorlevel 4 goto togglekfb
	if errorlevel 3 goto toggleafix
	if errorlevel 2 goto end
	if errorlevel 1 goto togglecrop
	
	:toggletrim
		set trim=
		set chopat=
		set /p chopat=Enter new end time of video (h:m:s) : 
		if not defined chopat goto clearchop
		set trim=1
		goto looptop
		:clearchop
		set trim=
		goto looptop

	:togglesplit
		set split=
		set splitat=
		set /p splitat=Enter time to divide at (h:m:s) : 
		if not defined splitat goto clearsplit
		set split=1
		goto looptop
		:clearsplit
		set split=
		goto looptop

	:namerepair
		if exist arialbd.ttf goto gotttf
		if not exist c:\windows\fonts\arialbd.ttf goto nodetails2
		echo.
		echo. Copying Arial Bold font to current directory for FFmpeg to use...
		copy c:\windows\Fonts\arialbd.ttf >nul
		timeout /t 1 >nul
		if not exist arialbd.ttf goto nodetails2
		echo.
		:gotttf
		set NewZoomName=
		set /p NewZoomName=Enter new Witness name for Zoomcloud video : 
		:: echo. I got %NewZoomName%
		:: timeout /t 1 >nul
		if not defined NewZoomName goto clearnewname
		set NameFix=1
		goto looptop
		:clearnewname
		set NameFix=
		goto looptop
		
		:nodetails2
		echo.
		echo. You can't repair the name - ArialBD.ttf isn't here and
		echo. I couldn't find it in C:\Windows\Fonts.  
		echo. If you can find that file and copy it here, this function
		echo. should start working fine.  
		echo.
		echo. Press a key when ready to continue
		pause >nul
		goto looptop
		
	:showdetails
		echo.
		if not exist ffprobe.exe goto nodetails
		call :showfiledata %file%
		echo.
		echo.
		echo. = press a key when done =
		pause >nul
		goto looptop
		:nodetails
		echo.
		echo. You can't get file details - FFprobe.exe isn't here
		echo.
		timeout /t 5 >nul
		goto looptop
		
	:togglehelp
		set value=%full%
		call :togglebinary
		set full=%value%
		goto looptop
		
	:togglesample
		set value=%sample%
		call :togglebinary
		set sample=%value%
		goto looptop
	
	:toggleafix
		set value=%afix%
		call :togglebinary
		set afix=%value%
		goto looptop
	
	:togglekfb
		set value=%KFBoost%
		call :togglebinary
		set KFBoost=%value%
		goto looptop

	:togglecrop
		set value=%crop%
		call :togglebinary
		set crop=%value%
		goto looptop
		
	:togglemp3
		set value=%makeMP3%
		call :togglebinary
		set makeMP3=%value%
		goto looptop

	:togglescale
		if [%scale%] == [] goto scale1080p
		if [%scale%] == [1080p] goto scale720p
		if [%scale%] == [720p] goto scale480p
		if [%scale%] == [480p] goto scale360p
		if [%scale%] == [360p] goto scalenone
		:scale1080p 
		set scale=1080p
		goto looptop
		:scale720p 
		set scale=720p
		goto looptop
		:scale480p 
		set scale=480p
		goto looptop
		:scale360p 
		set scale=360p
		goto looptop
		:scalenone
		set scale=
		goto looptop
		
	:togglempg
		if [%m1type%] == [] goto setm12600k
		if [%m1type%] == [2600k] goto setm1sameq
		if [%m1type%] == [sameQ] goto nompeg1
		:setm1sameq 
		set m1type=sameQ
		goto looptop
		:setm12600k 
		set m1type=2600k
		goto looptop
		:nompeg1
		set m1type=
		goto looptop
		

:showvalue
	if [%name%] == [Sample] goto binary
	if [%name%] == [NameFix] goto binary
	if [%name%] == [KFBoost] goto binary
	if [%name%] == [crop] goto binary
	if [%name%] == [afix] goto binary
	if [%name%] == [scale] goto string
	if [%name%] == [makeMP3] goto binary
	if [%name%] == [m1type] goto string
	if [%name%] == [trim] goto string
	if [%name%] == [split] goto string
	goto error1
	
	:binary
	set disp=%desc%
	if [%full%] == [1]  set disp=%help%
	:: special case of Namefix to show the new name
	if [%name%] == [NameFix] if defined NewZoomName set disp=%disp% (%NewZoomName%)
	if [%value%] == []  echo.    [ ]   %disp%
	if [%value%] == [0] echo.    [ ]   %disp%
	if [%value%] == [1] echo.    [*]   %disp%
	exit /b
	:string
	set disp=%desc%
	if [%full%] == [1]  set disp=%help%
	if [%value%] == []  echo.    [ ]   %disp%
	if defined value    echo.    [*]   %disp%
	exit /b
	
:togglebinary
	if [%value%] == []  goto valueon
	if [%value%] == [0] goto valueon
	if [%value%] == [1] goto valueoff
	:valueon 
	set value=1
	exit /b 
	:valueoff
	set value=
	exit /b 
	
:showfiledata
	  ::setlocal
	  for /F "delims=" %%I in ('ffprobe.exe -v error -show_entries format^=duration -sexagesimal -of default^=noprint_wrappers^=1:nokey^=1 "%file%" 2^>^&1') do set "duration=%%I"
	  echo Duration is: %duration%

	  for /F "delims=" %%I in ('ffprobe.exe -v error -show_entries stream^=width -of default^=noprint_wrappers^=1:nokey^=1 "%file%" 2^>^&1') do set "width=%%I"
	  :: echo Width is: %width%

	  for /F "delims=" %%I in ('ffprobe.exe -v error -show_entries stream^=height -of default^=noprint_wrappers^=1:nokey^=1 "%file%" 2^>^&1') do set "height=%%I"
	  :: echo Height is: %height%
	  echo Video resolution is %width% x %height%
	  
	  for /F "delims=" %%I in ('ffprobe.exe -v error -show_entries stream^=display_aspect_ratio -of default^=noprint_wrappers^=1:nokey^=1 "%file%" 2^>^&1') do set "display_aspect_ratio=%%I"
	  echo Aspect is: %display_aspect_ratio%

	  for /F "delims=" %%I in ('ffprobe.exe -v error -select_streams a:0 -show_entries stream^=sample_rate -of default^=noprint_wrappers^=1:nokey^=1 "%file%" 2^>^&1') do set "samplerate=%%I"
	  echo Audio sample rate is: %samplerate%

	  for /F "delims=" %%I in ('ffprobe.exe -v error -select_streams v:0 -show_entries stream^=profile -of default^=noprint_wrappers^=1:nokey^=1 "%file%" 2^>^&1') do set "profile=%%I"
	  echo Profile is: %profile%
	::endlocal
	exit /b
	
	
:error1
	echo Bad category name "%name%" passed to gosub function
	echo.
	echo Press a key to continue
	pause >nul
	goto end
	
	
:error2
	echo.
	echo User cancelled without picking a file
	echo.
	echo Press a key to exit
	pause >nul
	goto end
	
	
:error3
	echo.
	echo Usage : %~n0 [file to repair]
	echo.
	echo Press a key to exit
	pause >nul
	goto end	
	
:: ====================================================
:Biglaunch
:: ====================================================

	:: ===== reference commands =====
	::	scaleparam=scale=-1:720
	::	aspect=-aspect 16:9
	::	params=-q:v 0 -f mp4 -vcodec libx264 -preset faster -pix_fmt yuv420p -b:a 128k 
	::	audiofix=-ar 44100
	::	kfb gets -x264-params scenecut=0 -g 80
	::	cropparam=,crop=iw/4*3:ih
	::  sample=-ss 0:0:20 -t 0:0:15
	:: ===== reference commands =====
	

	
	:: If you want to lock it into MP4 gen only with fixes, the following is how
	goto ALWAYS_MP4gen
	:: Since this works on non-MP4 files, don't error on no fixes selected
	if [%is_repair%] == [1] goto MP4gen
	if [%sample%] == [1] goto MP4gen
	:: at this point, we already need to skip the MP4, but do we have nothing at all selected?
	:: Jump to skipMP4 if either makeMP3 or m1type are set, or nofixesset if neither
	if [%makeMP3%] == [1] goto skipMP4
	if [%m1type%] == [] goto nofixesset
	goto skipMP4
	:ALWAYS_MP4gen
	
	
:MP4gen
	set KFpart=
	if [%KFBoost%] == [1] set KFpart=-x264-params scenecut=0 -g 80
	set AFpart=
	if [%afix%] == [1] set AFpart=-ar 44100 
	set croppart1=
	set croppart2=
	if [%crop%] == [1] set croppart1=,crop=iw/4*3:ih 
	if [%crop%] == [1] set croppart2=-aspect 4:3 
	set scalepart=
	if [%scale%] == [1080p] set scalepart=,scale=1920:1080 
	if [%scale%] == [720p] set scalepart=,scale=1280:720 
	if [%scale%] == [480p] set scalepart=,scale=720:480 
	if [%scale%] == [360p] set scalepart=,scale=640:360 
	set samplepart=
	if [%sample%] == [1] set samplepart=-ss 0:0:20 -t 0:0:15
	set trimpart=
	if [%trim%] == [1] set trimpart=-t %chopat%
	:: if it's split, we'll trim, then run ffmpeg again with the seek and name it part2 or out2 whatever
	if [%split%] == [1] set trimpart=-t %splitat%
	if [%split%] == [1] set secondpart=-ss %splitat%

	:: NameFix stuff
	set NFpart=
	if /i x%nfmode%x == xcloud2verx goto cloud2ver
	if /i x%nfmode%x == xcloudverx goto cloudver
	if /i x%nfmode%x == xninjaverx goto ninjaver
	goto cloud2ver
	goto cloudver
	goto ninjaver
	:: PosX = Pixels from left edge for start of string location
	:: PosY = Pixels from bottom for string
	:: Note, TTF file must be in current directory
	:: For cloud, PosX=0, PosY=08, fontsize=18.  
	:: For Ninja, PosX=69, PoxY=58, fontsize=14
	:: For cloud2, PosX=0, PosY=08, fontsize=22.  
	:ninjaver	
	set PosX=04
	set PosY=08
	set font=arialbd
	set fontsize=14
	set fontcolor=white
	goto aftername
	:cloudver
	set PosX=0
	set PosY=08
	set font=arialbd
	set fontsize=18
	set fontcolor=white
	goto aftername
	:cloud2ver
	set PosX=0
	set PosY=08
	set font=arialbd
	set fontsize=22
	set fontcolor=white
	goto aftername
	:aftername
	if [%NameFix%] == [1] set NFpart=,drawtext=text='\    %NewZoomName%   \ ':x=%PosX%:y=H-th-%PosY%:fontfile=%font%.ttf:fontsize=%fontsize%:fontcolor=%fontcolor%:box=1:boxborderw=8:boxcolor=black
	
	set params=-q:v 0 -f mp4 -vf "yadif %croppart1% %scalepart% %NFpart%" -vcodec libx264 %KFpart% -preset faster -pix_fmt yuv420p %croppart2% %AFpart% -b:a 128k 
	:: save in/out for possible MPeg1 use below
	set outfile=%~n1%newext%.mp4
	if [%split%] == [1] set outfile=%~n1_part1.mp4
	title Making MP4 of %~n1 with chosen features (%duration%)
	set launch=ffmpeg -y -i "%file%" %params% %samplepart% %trimpart% -aspect 16:9 "%outfile%"	
	if [%split%] == [1] title Making part1 of split
	%launch%
	if [%split%] == [0] goto skipMP4
	if [%split%] == [] goto skipMP4
	:: if we're here, it's a split cuz [0] and [] didn't tempt it into jumping
		set outfile=%~n1_part2.mp4
		set launch=ffmpeg -y -i "%file%" %params% %samplepart% %secondpart% "%outfile%"
		title Making part2 of split
		%launch%
	:skipMP4
	
	if [%m1type%] == [] goto skipmpeg1
	if [%m1type%] == [2600k] set params=-f mpeg -r 29.97 -f mpeg -r 29.97 -s 720:480 -write_tmcd 0 -qmin 16 -qmax 24 -pix_fmt yuv420p -b:v 2600k -maxrate 2600k -minrate 2600k -bufsize 1000k -sws_flags lanczos -g 15 -ab 128k  "%~n1.480p.2600k.mpg"
	if [%m1type%] == [sameQ] set params=-q:v 0 -maxrate 6500k -bufsize 1000k "%~n1.sameq.mpg"
	:: if something was repaired above, use it as the source
	set infile=%file%
	if defined outfile set infile=%outfile%
	title Making %m1type% MPEG-1 of %~n1%
	set launch2=ffmpeg -y -i "%infile%" %params%
	%launch2%
	:skipmpeg1
	
	if [%makeMP3%] == [] goto skipmp3
	set params=-f mp3 -vn -ar 44100 -ac 2 -ab 96k  "%~n1.mp3"
	set launch3=ffmpeg -y -i "%file%" %params%
	title Making MP3 of %~n1 
	%launch3%
	:skipmp3	

	goto end



	
	:nofixesset
	echo.
	echo You don't have anything selected to fix!
	echo.
	echo. == Press a key to continue ==
	pause >nul
	goto looptop
	
:end

title %~n0 done!