Advertisement
name22

GDI+ Chroma Keying with Videos

Jun 29th, 2011
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 6.35 KB | None | 0 0
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GDIP.au3>
  4. #include <WinAPI.au3>
  5.  
  6. ; - Author: name22 (www.autoit.de)
  7.  
  8. Opt("GUIOnEventMode", 1)
  9.  
  10. ;###-v-SETTINGS-v-###
  11.  
  12. $iGUIWidth = 720 ;Videobreite
  13. $iGUIHeight = 408 ;Videohöhe
  14. $iARGB_BG = 0x00000000 ;Hintergrundfarbe
  15. $nFPS = 30 ;Framerate (muss nicht der Framerate des Videos entsprechen!)
  16.  
  17. $sInputVideo = @ScriptDir & "\Test 2.avi" ;Pfad zur Videodatei
  18. $sOutputFolder = @ScriptDir & "\Frames" ;Ausgabeverzeichnis der einzelnen Frames
  19. $iVideoLength = 8000 ;Videolänge in Millisekunden
  20.  
  21. ;###-^-SETTINGS-^-###
  22.  
  23. $tSize = DllStructCreate($tagSIZE)
  24. $pSize = DllStructGetPtr($tSize)
  25. DllStructSetData($tSize, "X", $iGUIWidth)
  26. DllStructSetData($tSize, "Y", $iGUIHeight)
  27. $tSource = DllStructCreate($tagPOINT)
  28. $pSource = DllStructGetPtr($tSource)
  29. $tBlend = DllStructCreate($tagBLENDFUNCTION)
  30. $pBlend = DllStructGetPtr($tBlend)
  31. DllStructSetData($tBlend, "Alpha", 255)
  32. DllStructSetData($tBlend, "Format", 1)
  33. $tPoint = DllStructCreate($tagPOINT)
  34. $pPoint = DllStructGetPtr($tPoint)
  35. DllStructSetData($tPoint, "X", 0)
  36. DllStructSetData($tPoint, "Y", 0)
  37.  
  38. $iCurFrame = 1
  39.  
  40. $nSleepTime = 1000 / $nFPS
  41.  
  42. $nFPS_Display = 0
  43. $nFPS_Average = $nFPS
  44.  
  45. $vNTdll = DllOpen("ntdll.dll")
  46.  
  47. $tPrecSleep = DllStructCreate("int64 time;")
  48. $pPrecSleep = DllStructGetPtr($tPrecSleep)
  49.  
  50. $hMain = GUICreate("Chroma Keying Example by name22", $iGUIWidth, $iGUIHeight, Default, Default, $WS_POPUP, $WS_EX_LAYERED)
  51. GUISetState()
  52.  
  53. $hDC_Main = _WinAPI_GetDC($hMain)
  54. $hDC_Buffer = _WinAPI_CreateCompatibleDC($hDC_Main)
  55. $hBitmap_Buffer = _WinAPI_CreateCompatibleBitmap($hDC_Main, $iGUIWidth, $iGUIHeight)
  56. _WinAPI_SelectObject($hDC_Buffer, $hBitmap_Buffer)
  57.  
  58. _GDIPlus_Startup()
  59.  
  60. $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC_Buffer)
  61. _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
  62. _GDIPlus_GraphicsSetTextRenderingHint($hGraphics, 3)
  63.  
  64. $hBrush_Black = _GDIPlus_BrushCreateSolid(0xFF000000)
  65.  
  66. $hStringFormat = _GDIPlus_StringFormatCreate()
  67. $hFamily_FPS = _GDIPlus_FontFamilyCreate("Arial")
  68. $hFont_FPS = _GDIPlus_FontCreate($hFamily_FPS, 6)
  69.  
  70. $aMeasure = _GDIPlus_GraphicsMeasureString($hGraphics, "FPS: 000", $hFont_FPS, _GDIPlus_RectFCreate(), $hStringFormat)
  71. $tLayout_FPS = $aMeasure[0]
  72. $aMeasure = ""
  73. DllStructSetData($tLayout_FPS, "X", $iGUIWidth - DllStructGetData($tLayout_FPS, "Width") - 3)
  74. DllStructSetData($tLayout_FPS, "Y", $iGUIHeight - DllStructGetData($tLayout_FPS, "Height"))
  75. DllStructSetData($tLayout_FPS, "Width", DllStructGetData($tLayout_FPS, "Width") + 3)
  76.  
  77. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close", $hMain)
  78. HotKeySet("{ESC}", "_Close")
  79. OnAutoItExitRegister("_Shutdown")
  80.  
  81. If DirGetSize($sOutputFolder) = -1 Then DirCreate($sOutputFolder)
  82. RunWait(@ScriptDir & '\ffmpeg.exe -sameq -i "' & $sInputVideo & '" "' & $sOutputFolder & '\Frame %d.jpg"', @ScriptDir, @SW_HIDE, 0x2)
  83.  
  84. $oFSO = ObjCreate("Scripting.FileSystemObject")
  85. $iFileCount = $oFSO.GetFolder($sOutputFolder).Files.Count
  86.  
  87. Global $aFrames[$iFileCount + 1] = [$iFileCount]
  88.  
  89. $hIA_Keying = _GDIPlus_ImageAttributesCreate()
  90. _GDIPlus_ImageAttributesSetColorKeys($hIA_Keying, 1, True, 0xFF454545, 0xFFFFFFFF)
  91. For $i = 1 To $aFrames[0]
  92.     $aFrames[$i] = _GDIPlus_ImageLoadFromFile($sOutputFolder & "\Frame " & $i & ".jpg")
  93. ;~  $hGraphicsTmp = _GDIPlus_ImageGetGraphicsContext($aFrames[$i])
  94.     ;Hier könnte noch ein Wasserzeichen o.Ä. eingefügt werden.
  95. ;~  _GDIPlus_GraphicsDispose($hGraphicsTmp)
  96. Next
  97.  
  98. $nT_UpdateFPS = TimerInit()
  99. $nT_RunningTime = TimerInit()
  100. $nT_Sleep = TimerInit() + $nSleepTime
  101. While True
  102.     DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep)))
  103.     DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
  104.     $nFrameTime = TimerDiff($nT_Sleep)
  105.     $nT_Sleep = TimerInit()
  106.  
  107.     $nFPS_Cur = 1000 / $nFrameTime
  108.     If TimerDiff($nT_UpdateFPS) >= 1000 Then
  109.         $nFPS_Display = $nFPS_Cur
  110.         $nT_UpdateFPS = TimerInit()
  111.     EndIf
  112.  
  113.     _GDIPlus_GraphicsClear($hGraphics, $iARGB_BG)
  114.     $iCurFrame = Ceiling(TimerDiff($nT_RunningTime) / $iVideoLength * $aFrames[0])
  115.     If $iCurFrame > $aFrames[0] Then
  116.         $iCurFrame = 1
  117.         $nT_RunningTime = TimerInit()
  118.     EndIf
  119.     _GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $aFrames[$iCurFrame], 0, 0, $iGUIWidth, $iGUIHeight, 0, 0, $iGUIWidth, $iGUIHeight, $hIA_Keying)
  120.     _GDIPlus_GraphicsDrawStringEx($hGraphics, "FPS: " & Int($nFPS_Display), $hFont_FPS, $tLayout_FPS, $hStringFormat, $hBrush_Black)
  121.     _WinAPI_UpdateLayeredWindow($hMain, $hDC_Main, 0, $pSize, $hDC_Buffer, $pSource, 0, $pBlend, 2)
  122. WEnd
  123.  
  124. Func _Close()
  125.     Exit
  126. EndFunc   ;==>_Close
  127.  
  128. Func _Shutdown()
  129.     _WinAPI_ReleaseDC($hMain, $hDC_Main)
  130.     _WinAPI_DeleteDC($hDC_Buffer)
  131.     _WinAPI_DeleteObject($hBitmap_Buffer)
  132.  
  133.     _GDIPlus_GraphicsDispose($hGraphics)
  134.     For $i = 1 To $aFrames[0]
  135.         _GDIPlus_ImageDispose($aFrames[$i])
  136.     Next
  137.     _GDIPlus_BrushDispose($hBrush_Black)
  138.     _GDIPlus_StringFormatDispose($hStringFormat)
  139.     _GDIPlus_FontFamilyDispose($hFamily_FPS)
  140.     _GDIPlus_FontDispose($hFont_FPS)
  141.     _GDIPlus_ImageAttributesDispose($hIA_Keying)
  142.     _GDIPlus_Shutdown()
  143.  
  144.     DllClose($vNTdll)
  145. EndFunc   ;==>_Shutdown
  146.  
  147. ;Copyright name22 (@http://www.autoit.de/) Tuesday, 24th May 2011
  148.  
  149.  
  150. ; #FUNCTION#;===============================================================================
  151. ;
  152. ; Name...........: _HighPrecisionSleep()
  153. ; Description ...: Sleeps down to 0.1 microseconds
  154. ; Syntax.........: _HighPrecisionSleep( $iMicroSeconds, $hDll=False)
  155. ; Parameters ....:  $iMicroSeconds      - Amount of microseconds to sleep
  156. ;                  $hDll  - Can be supplied so the UDF doesn't have to re-open the dll all the time.
  157. ; Return values .: None
  158. ; Author ........: Andreas Karlsson (monoceres)
  159. ; Modified.......:
  160. ; Remarks .......: Even though this has high precision you need to take into consideration that it will take some time for autoit to call the function.
  161. ; Related .......:
  162. ; Link ..........;
  163. ; Example .......; No
  164. ;
  165. ;;==========================================================================================
  166. Func _HighPrecisionSleep($iMicroSeconds, $hDll = False)
  167.     Local $hStruct, $bLoaded
  168.     If Not $hDll Then
  169.         $hDll = DllOpen("ntdll.dll")
  170.         $bLoaded = True
  171.     EndIf
  172.     $hStruct = DllStructCreate("int64 time;")
  173.     DllStructSetData($hStruct, "time", -1 * ($iMicroSeconds * 10))
  174.     DllCall($hDll, "dword", "ZwDelayExecution", "int", 0, "ptr", DllStructGetPtr($hStruct))
  175.     If $bLoaded Then DllClose($hDll)
  176. EndFunc   ;==>_HighPrecisionSleep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement