#!/usr/bin/env python 
import subprocess
import tempfile
	
#sourcedir = input("enter directory of files to process (do not indluce '\' at end): ")
sourcedir = "H:\FilmFix Film Transfer\VHS-C tapes\SD"
#diroutput = input("enter output location (do not indluce '\' at end): ")
diroutput = "H:\FilmFix Film Transfer\VHS-C tapes\HD"
#sourceExt = input("avi or mov?: ")
sourceExt = "mov"

def write_AVS_file(sourcedir,x,sourceExt):
	#
	# 	create a temporary file and write some data to it
	#
	f = open('temp_AVS.avs', 'w')
	f.write('SetFilterMTMode("QTGMC", 2)\n')
	f.write('FFmpegSource2("' + sourcedir + '\\' + x + '.' + sourceExt + '", atrack=1)\n')
	f.write('ConvertToYV12()\n')
	#
	#   bottom frame first
	f.write('AssumeBFF()\n')
	#
	#   top frame first
	#AssumeTFF()
	#
	# 29.97fps
	f.write('QTGMC(Preset="Slower", FPSDivisor=2, Edithreads=3)\n')
	#
	# 59.94fps
	#QTGMC(Preset="Slower", Edithreads=3)
	#
	#  VHS tape NTSC
	f.write('Crop(10,0,-6,-10)\n')
	f.write('Spline64Resize(1448,1080)\n')
	f.write('LimitedSharpenFaster(ss_x=1.9, ss_y=1.9, Smode=1, strength=60, radius=4, Lmode=1, wide=false, overshoot=1, soft=-1, edgemode=0, special=false, exborder=1)\n')
	f.write('AddBorders(236, 0, 236, 0, color_black)\n')
	f.write('Prefetch(threads=10)\n')

def getFileNames():
	global filenamefirst 
	filenamefirst = int(input("enter NUMBER of starting file name to process: "))
	global filenamelast 
	filenamelast = int(input("enter NUMBER of last file to process: "))

def main():
	import os
	import os.path
	getFileNames()
	for x in range(filenamefirst, filenamelast+1):	
		write_AVS_file(sourcedir,str(x),sourceExt)
		if os.path.exists(diroutput + "\\" + str(x) + ".mp4"):
			os.remove(diroutput + "\\" + str(x) + ".mp4")
		from subprocess import check_output
		check_output("ffmpeg -i \"temp_AVS.avs\" -c:v libx264 -b:v 50M -c:a aac \"" + diroutput + "\\" + str(x) + ".mp4\"", shell=True)	
		print("^================================= finished file " + str(x) + "." + sourceExt)
	print("end of program")
		
main()


