; Video to MKV Convert
; by Lehmden 2013
; Version 0.1.3

 #include "language.au3"


If FileExists(@ScriptDir & "\Video2mkv_convert.ini") Then
	$indir = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "folder", "input", @MyDocumentsDir)
	$outdir = IniRead(@ScriptDir & "\Video2mkv_convert.ini", "folder", "output", @MyDocumentsDir)
	Else
	$indir = @MyDocumentsDir
	$outdir = @MyDocumentsDir
Endif

; Get initial working dir
$wd = @WorkingDir & "\"

; Get input Dir

$InputFolder = FileSelectFolder($InputFolderText, "",6, $indir)
If @error Then 
	 MsgBox(0, $errmsg, $NoSrcDir)
	Exit
EndIf

; Get output Dir

$OutputFolder = FileSelectFolder($OuputFolderText, "", 7, $outdir)
If @error Then 
	 MsgBox(0, $errmsg, $NoDestDir)
	Exit
EndIf

IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "folder", "input", $InputFolder)
IniWrite(@ScriptDir & "\Video2mkv_convert.ini", "folder", "output", $OutputFolder)

;Main Program
$ConversionText = $ConversionText1 & $InputFolder & $ConversionText2

$y = MsgBox (4, $StartConversion, $ConversionText)

if $y = 6 Then
	; Change to Input Folder
	FileChangeDir ( $InputFolder )

	; Show Filenames in Working Directory.
	$search = FileFindFirstFile("*.*")  

	; Test if there is any file
	If $search = -1 Then
		MsgBox(0, $errmsg, $NoFiles)
		Exit
	EndIf

		While 1
		$file = FileFindNextFile($search) 
			If @error Then ExitLoop
				
				$InFile = $InputFolder & "\" & $file
				$len = StringLen($file) 
				$pos = StringInStr($file, ".", 2, -1)
				$FileNew = StringLeft($file, $pos) & "mkv"
				$FileTag = StringLeft($file, $pos) & "xml"
				$OutFile = $OutputFolder & "\" & $FileNew
				$ext = StringLower ( StringTrimLeft($file, $pos) )
				if $ext = "wmv" then
					$options = ' -i "' & $InFile & '" -acodec copy -vcodec copy "' & $OutputFolder & "\" & "temp.mkv" & '"'
					$ffm = $wd & "ffmpeg.exe"
					ShellExecuteWait ($ffm, $options)
					If FileExists($InputFolder & "\" & $FileTag) Then
					$option = ' -o "' & $OutFile & '" --global-tags ' & '"' & $InputFolder & "\" & $FileTag & '"' & ' "'& $OutputFolder & "\" & "temp.mkv" & '"'
					$mmg = $wd & "mkvmerge.exe"
					ShellExecuteWait ($mmg, $option)
					FileDelete ( $OutputFolder & "\" & "temp.mkv" )
					Else
					$option = ' -o "' & $OutFile & '"' & ' "'& $OutputFolder & "\" & "temp.mkv" & '"'
					$mmg = $wd & "mkvmerge.exe"
					ShellExecuteWait ($mmg, $option)	
					FileDelete ( $OutputFolder & "\" & "temp.mkv" )
					EndIf
				Elseif $ext = "avi" OR $ext = "ts" OR $ext = "mpg" OR $ext = "mpeg" OR $ext = "mp4" OR $ext = "mov" OR $ext = "mkv"  OR $ext = "flv" then
					If FileExists($InputFolder & "\" & $FileTag) Then
					$option = ' -o "' & $OutFile & '" --global-tags ' & '"' & $InputFolder & "\" & $FileTag & '"' & ' "' & $InFile & '"'
					$mmg = $wd & "mkvmerge.exe"
					ShellExecuteWait ($mmg, $option)
					Else
					$option = ' -o "' & $OutFile & '"' & ' "'& $InFile & '"'
					$mmg = $wd & "mkvmerge.exe"
					ShellExecuteWait ($mmg, $option)	
					EndIf
				EndIf
			
		WEnd

		; Close Search Handle
		FileClose($search)
	
EndIf

if $y = 7 Then
	MsgBox (0, $Conversion, $isabort)
EndIf
Exit