# A2DSource library: Universal AviSynth Source Functions
# Written by MrC (November 2008)
# Updated by MrC (March 2009)
# Following AviSynth plugins are required in the same A2DSource folder:
#  FFMpegSource.dll		more info @ http://forum.doom9.org/showthread.php?t=127037
#  GetSystemEnv.dll		more info @ http://avisynth.org/stickboy

LoadPlugin("FFMpegSource.dll")
LoadPlugin("GetSystemEnv.dll")


Function A2DVideoSource(string VideoSource, string "CacheFolder", bool "debug")
{
CacheFolder = default(CacheFolder, GetSystemEnv("temp"))
debug = default(debug, false)
Try {
SourceStr = "A2DVideoSource: AVISource"
Video = AVISource(VideoSource, audio = false)
}
Catch(Err_Msg) {
	Try {
	SourceStr = "A2DVideoSource: DirectShowSource"
	Video = DirectShowSource(VideoSource, convertfps = true, audio = false)
	}
	Catch(Err_Msg) {
		SourceStr = "A2DVideoSource: FFmpegSource"
		Video = FFmpegSource(VideoSource, vtrack = -1, atrack = -2, vcachefile = CacheFolder + "\A2DFFV_" + String(Rand()) + ".cache", seekmode = 0)
	}
}
Video = (debug == true) ? Subtitle(Video.Info(), SourceStr, font = "courier new", size = 16, align = 1) : Video
return(Video)
}


Function A2DAudioSource(string AudioSource, string "CacheFolder")
{
CacheFolder = default(CacheFolder, GetSystemEnv("temp"))
Try {
Audio = DirectShowSource(AudioSource, video = false)
}
Catch(Err_Msg) {
	Try {
	Audio = FFAudioSource(AudioSource, atrack = -1, acachefile = CacheFolder + "\A2DFFA1_" + String(Rand()) + ".cache", acachefile2 = CacheFolder + "\A2DFFA2_" + String(Rand()) + ".cache")
	}
	Catch(Err_Msg) {
		Audio = FFmpegSource(AudioSource, vtrack = -2, atrack = -1, acachefile = CacheFolder + "\A2DFFA_" + String(Rand()) + ".cache", seekmode = 0)
	}
}
return(Audio)
}

