#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <INet.au3>
;Test-URL: http://www.youtube.com/watch?v=nGWrkq2Ales

If (Not FileExists(@ScriptDir & "\Videos\")) Then DirCreate(@ScriptDir & "\Videos\")

;Gui Erstellen
$hWin1 = GUICreate("iTube v1.0", 319, 93, 192, 124)
$hLbl1 = GUICtrlCreateLabel("YouTube URL hier Eingeben :", 8, 8, 147, 17)
$hInp1 = GUICtrlCreateInput("", 160, 8, 153, 21, BitOR($ES_RIGHT, $ES_AUTOHSCROLL))
$hPrg1 = GUICtrlCreateProgress(8, 32, 305, 17)
$hBtn1 = GUICtrlCreateButton("Download", 8, 56, 75, 25, $WS_GROUP)
$hPrg2 = GUICtrlCreateProgress(88, 60, 225, 17)

GUISetState(@SW_SHOW) ;GUI Anzeigen

While 1
	$nMsg = GUIGetMsg() ;Controls Abfragen
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $hBtn1
			If GUICtrlRead($hInp1) = "" Then ContinueLoop
			_Download(GUICtrlRead($hInp1))

	EndSwitch
WEnd

Func _Download($sURL, $bHD = False)
	Local $sSource, $sTitle, $sID, $sDownloadLink, $sSWFArgs, $sSWFID, $iSize, $iCurrent, $iProzent
	$sSource = _INetGetSource($sURL)
	$sSource = StringStripCR($sSource)
	$sSource = StringReplace($sSource, @LF, '')

	$sTitle = StringRegExp($sSource, "'VIDEO_TITLE': '(.*?)'", 1, 1) ;Titel Auslesen
	If IsArray($sTitle) Then $sTitle = $sTitle[0] ;Anpassen
	GUICtrlSetData($hPrg2, 25)
	Sleep(500)

	$sID = StringRegExp($sSource, "'VIDEO_ID': '(.*?)'", 1, 1) ;ID Auslesen
	If IsArray($sID) Then $sID = $sID[0] ;Anpassen
	GUICtrlSetData($hPrg2, 50)
	Sleep(500)

	;SWF ARG - Bereich auslesen
	$sSWFArgs = StringRegExp($sSource, 'SWF_ARGS(.*?)}', 1, 1)
	If IsArray($sSWFArgs) Then $sSource = $sSWFArgs[0]
	GUICtrlSetData($hPrg2, 75)
	Sleep(500)

	;Code ID aus dem SWF ARG Bereich auslesen
	$sSWFID = StringRegExp($sSource, '"t": "(.*?)",', 1, 1)
	If IsArray($sSWFID) Then $sSWFID = $sSWFID[0]
	GUICtrlSetData($hPrg2, 100)
	Sleep(500)
	GUICtrlSetData($hPrg2, 0)

	$sDownloadLink = 'http://www.youtube.com/get_video?fmt=18&video_id=' & $sID & '&t=' & $sSWFID
	$iSize = InetGetSize($sDownloadLink)

	InetGet($sDownloadLink, @ScriptDir & "\Videos\" & StringRegExpReplace($sTitle, "((?<!.)([a-zA-Z]\:\\)(?!\s)(([\w-+.]*" & _
			"((?<!\s)(\s{1})(?!\\)|(?<!\\)(\\{1})(?!\s))*)*)(\\?)(?!.))", "") & ".mp4", 1, 1)

	While @InetGetActive = 1
		$iCurrent = @InetGetBytesRead
		$iProzent = 100 * $iCurrent / $iSize
		$iProzent = Round($iProzent, 0)

		GUICtrlSetData($hPrg1, $iProzent)
		Sleep(200)
	WEnd
	MsgBox(64, "iTube v1.0", $sTitle & " erfolgreich Heruntergeladen")
	Exit
EndFunc   ;==>_Download