Youtube Watcher v.3 - a simple Youtube Turn-off Lights script

Post your working scripts, libraries and tools for AHK v1.1 and older
gtaking
Posts: 5
Joined: 12 May 2016, 00:26
Contact:

Youtube Watcher v.3 - a simple Youtube Turn-off Lights script

12 Sep 2017, 17:25

i dont know whether it is the same for all the countries but Youtube changed its design altogether just after summer. i remember some of the pages were like this but now all of them are!

i was using simple version of this script (v.1) for the old site but now video frame changes size like crazy

tough job as it is, i managed to make my ahk generated window act like youtube video div manually xD maybe a brute-force-like video frame boundary pixel search would be a global solution but here it is...


usage

- open your youtube video in firefox, chrome, opera or vivaldi
- turn on youtube theater mode
- adjust size and screen placement of the video window
- open the script

Image

makes whole screen black while we only see the youtube video frame
only works for firefox, chrome, opera & vivaldi
when there is no menu and bookmarks toolbar in any browser
opera has no sidebar
vivaldi has left panel
when on different application (working / playing) you can use video timeskip keys (arrow keys) on firefox
they will be sent to browser
double click on black areas to close

works on multi monitors

works on win10

to start the script with a key combination anytime, you can write an always-working-script that resides in your windows Startup folder


limitations

fullscreen black background is optional, can be disabled from system tray menu

remote controls (arrow keys 5 sec video skip) only works on firefox (LoL firefox ftw)
ControlSend doesnt work on any chrome-like browser ahk_class Chrome_WidgetWin_1 even though we send to right control Chrome_RenderWidgetHostHWND1 (idk why)
( therefore if you do not want these 2 features like ever, you may just delete related lines from the code )

window you want script to work on should be active (on top of everything)
or it works (or returns error) for the active youtube window
script is designed only for standard browser window
any action which will change the browser window border sizes (maximized or full screen affects title bar size...) will cause script to not work properly
about fullscreen

script isnt compatible with any kind of fullscreen unless you manually expand window to whole screen in theater mode.

even if we understand the browser is filling whole screen and even if we can get current video start coordinate with brute force search (while youtube doesnt change that top color) to get some of the init values; it seems we need some new vars and if nodes because of the system non-visible area rule change that came with snapping edges on maximized / fullscreen. apparently we need to double the whole work and code lines to have this feature. therefore fullscreen support cant be added :)

20" and below monitor users may not even need the script since fullscreen video mode may cover their needs. they may never need black area on their screen.

i dont know about anyone but 21"+ monitors provide so large picture that from the day i bought i cant watch any video on fullscreen unless im meters away. i cant use fullscreen, i need blackness on my desktop when im writing, therefore the script is shaped around that fact.


numbers

since i dont think everyone would prefer using a shortcut for the job, numbers ive found can be used in any kind of language you will work on to generate more cinematic-mode-like view (inside browser page, with button on browser screen...) but i guess there are much easier ways to get page element coordinates on different platforms xD

differences on client window sizes or toolbar placements on other browsers directly effect this kind of tool so i only made it for my usage.

you may edit the code to get no background, no input listening etc... you may edit numbers to adjust it to your browsers but you need a solid plan and tools that will make your job much easier. still takes much time, i dont encourage you to do it.

right now its working perfect, hopefully youtube doesnt change those video size rules again :)

cheers


[Edit: Updated to v.3, now supports different browsers]

Code: Select all

; youtube watcher v.3
; gtaking

; 2017 III


; Designed for YouTube Theater mode
	; video window should be active
; Works on desktop

; Echoes timeskip keys to video window
	; can be disabled from tray menu
; Fills background with black screen
	; can be closed from tray menu
; Double click to dismiss


; for youtube videos on Firefox, Chrome, Opera & Vivaldi
; 	  for new youtube design update

; 	  values when there is no extra toolbars (menu bar, bookmarks toolbar etc...)
;		  video frame coordinate				yvid : 1st non-black pixel
;		  browser window left column			  wI :  6-8
;		  browser window right column			 wII : 23-25
;		  browser window bot row (no scrollbar)	wIII :  6-8


#NoTrayIcon
#SingleInstance, Force
#KeyHistory 0
#WinActivateForce
ListLines Off
SetBatchLines, -1
SetWinDelay, -1
SetKeyDelay, -1, -1
SendMode Input
SetTitleMatchMode, 2


arrowkeysactive := true, arrowmenuname1 := "Disable remote control", arrowmenuname2 := "Enable remote control"


Menu, Tray, Icon, accessibilitycpl.dll, 13
Menu, Tray, Tip, Youtube Watcher
Menu, Tray, NoStandard
Menu, Tray, Add, %arrowmenuname1%, Deactivate
Menu, Tray, Add, Remove Background, RemovingBlackBack
Menu, Tray, Add, Quit, Exit
Menu, Tray, Default, Quit
Menu, Tray, Icon


Gui +OwnDialogs

bid := WinExist("YouTube")
if (!bid) {
	MsgBox, 0x40000, Youtube Watcher, No active YouTube window found., 1.5
	ExitApp
	Return
}
bid := "ahk_id " bid
WinGet, bname, ProcessName

; extra toolbars inside browser, which are narrowing page area
extrahbot := 0

; if you have different toolbar layout than script default, modify related values
; scrollbars, title bars given by the system, which are directly affecting window start / end coordinates
if (bname == "firefox.exe")
	wI := 6, wII := 23, wIII := wI, yv := 141
else if (bname == "chrome.exe" )
	wI := 8, wII := 25, wIII := wI, yv := 141
else if (bname == "opera.exe")
	wI := 8, wII := 23, wIII := wI, yv := 137
else if (bname == "vivaldi.exe")
	wI := 34, wII := 17, wIII := 0, yv := 126, extrahbot := 20
; else if (bname == "Safari.exe")
; Safari doesn't support new site at this moment
else {
	MsgBox, 0x40000, Youtube Watcher, Supported browsers:`tFirefox`, Chrome`, Opera & Vivaldi., 5
	ExitApp
	Return
}


WinGetPos, x, y, w, h, %bid%

if (h < yv + wII + wIII) {
	MsgBox, 0x40000, Youtube Watcher, YouTube window is minimized or too small, 1.5
	ExitApp
	Return
}


if (w <= 881 + wI + wIII) {

	yvid := yv + 24
	if (w <= 656 + wI + wIII) {
		wvid := 426
		hvid := 240
	}
	else if (w <= 856 + wI + wIII) {
		wvid := 640
		hvid := 360
	}
	else {
		wvid := 853
		hvid := 480
	}
	ww := w - wI - wII - wvid
	if (ww > 0) {
		if (mod( ww, 2)) {
			ww  := (ww - 1) / 2
			wI  += ww + 1
		}
		else {
			ww  := ww / 2
			wI  += ww
		}
		wII += ww
	}

	yIII := y + yvid + hvid
	hIII := h - yvid - hvid
}
else {

	yvid := yv
	wvid := w - wI - wII
	if (h < 750 + wIII) {
		hvid := 480
		yIII := y + yvid + hvid
		hIII := h - yvid - hvid
	}
	else {
		hv := 8		; video render frame
					; top & bottom black spaces
		hvid := round( wvid / 16 * 9) + hv
		yIII := y + yvid + hvid

		hh   := y + h - wIII - 129 - extrahbot
		if (yIII > hh) {
			yIII := hh
			hvid := yIII - y - yvid
		}
		hIII := h - yvid - hvid
	}
}

if (hIII < wIII + extrahbot) {
	yIII := y + h - wIII - extrahbot
	hIII := wIII + extrahbot
	hvid := yIII - y - yvid
}


SysGet, VirtualScreenWidth, 78
SysGet, VirtualScreenHeight, 79

Gui, New, +HwndBrowserBlackBack +Owner +Disabled -Caption -SysMenu -Theme +ToolWindow
Gui, %BrowserBlackBack%: Color, black
Gui, %BrowserBlackBack%: Show, x0 y0 w%VirtualScreenWidth% h%VirtualScreenHeight% NoActivate

WinSet, Bottom, , ahk_id %BrowserBlackBack%
WinActivate, ahk_class Shell_TrayWnd

Gui, New, +HwndBrowserBlackI +Owner +Disabled -Caption -SysMenu -Theme +ToolWindow +AlwaysOnTop
Gui, %BrowserBlackI%: Color, black
Gui, %BrowserBlackI%: Show, x%x% y%y% w%w% h%yvid% NoActivate

xII := x + w - wII
yII := y + yvid
Gui, New, +HwndBrowserBlackII +Owner +Disabled -Caption -SysMenu -Theme +ToolWindow +AlwaysOnTop
Gui, %BrowserBlackII%: Color, black
Gui, %BrowserBlackII%: Show, x%xII% y%yII% w%wII% h%hvid% NoActivate

Gui, New, +HwndBrowserBlackIV +Owner +Disabled -Caption -SysMenu -Theme +ToolWindow +AlwaysOnTop
Gui, %BrowserBlackIV%: Color, black
Gui, %BrowserBlackIV%: Show, x%x% y%yII% w%wI% h%hvid% NoActivate


Gui, New, +HwndBrowserBlackIII +Owner +Disabled -Caption -SysMenu -Theme +ToolWindow +AlwaysOnTop
Gui, %BrowserBlackIII%: Color, black
Gui, %BrowserBlackIII%: Show, x%x% y%yIII% w%w% h%hIII% NoActivate

Return


LButton::
RButton::
	MouseGetPos, , , id
	if (id == BrowserBlackI or id == BrowserBlackII or id == BrowserBlackIII or id == BrowserBlackIV or id == BrowserBlackBack) {
		melapsed := A_TickCount - mlastpressed
		if (melapsed < 300 && mlastid == id && A_ThisHotkey == mlastkey)
			GoSub, Exit
	}
	else
		if (A_ThisHotkey == "LButton")
			Click down
		else
			Click down right
	mlastid := id
	mlastkey := A_ThisHotkey
	mlastpressed := A_TickCount
Return


~LButton Up::
~RButton Up::
Return


~Left::
~Right::
	if (arrowkeysactive) and not WinActive(bid) {
		StringTrimLeft, arrowkey, A_ThisHotkey, 1
		ControlSend, , {%arrowkey%}, %bid%
	}
Return


Deactivate:
	arrowkeysactive := !arrowkeysactive
	if (arrowkeysactive)
		Menu, Tray, Rename, %arrowmenuname2%, %arrowmenuname1%
	else
		Menu, Tray, Rename, %arrowmenuname1%, %arrowmenuname2%
	WinActivate, ahk_class Shell_TrayWnd
	WinActivate, %bid%
Return


AddingBlackBack:
	Menu, Tray, Delete, Add background
	Menu, Tray, Insert, Quit, Remove background, RemovingBlackBack
	Gui, %BrowserBlackBack%: Show
	WinActivate, ahk_class Shell_TrayWnd
	WinActivate, %bid%
Return


RemovingBlackBack:
	Menu, Tray, Delete, Remove background
	Menu, Tray, Insert, Quit, Add background, AddingBlackBack
	Gui, %BrowserBlackBack%: Hide
	WinActivate, %bid%
Return


Exit:
GuiClose:
ExitApp
Return

Last edited by gtaking on 16 Sep 2017, 11:22, edited 1 time in total.
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: Youtube Watcher v.2 - a simple Youtube Turn-off Lights script

12 Sep 2017, 19:21

kinda cool, however youtube has a dark theme built in. Also there's loads of plugins that do the same thing
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Youtube Watcher v.2 - a simple Youtube Turn-off Lights script

12 Sep 2017, 23:04

As RUNIE mentions, there are other solutions that might be more reliable for YouTube specifically.

I could see utility in this particular script if it wasn't limited to Firefox only, and especially so if it wasn't limited to YouTube only. I'm not sure how many users don't just full screen their videos and prefer to blacken the rest of the screen, but I imagine if you could get this generalized to work on other sites, it would be able to stand out.

My idea having read about this is if you could take the black border that fills the screen, and then adjust the width of the top, left, right, and bottom borders by dragging on the inside rectangle and aligning it to the video yourself. Of course, a way to automate that would be ideal, but I don't know if there is a good way to do that besides bruteforcing all the possible player sizes and positions across the popular video websites - and somehow being mindful of a browser's UI - and being able to keep up to date when UIs are changed on websites and browsers alike. Being able to save settings though would be a great compromise for that - let uses make a profile with their own specified widths and heights to each of the borders.
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Youtube Watcher v.2 - a simple Youtube Turn-off Lights script

14 Sep 2017, 16:36

@gtaking , thank you works fine

can I modify :
- with hotkey toggle : close or add black background window
- didn't see how to modify video-height
- can't see tray

I watched youtube video in AHK-GUI ActiveX , but no more works
( ' Flash embedded videos are no more supported ' )
or watch youtube video in fullscreen mode

another example , start video from different sites and click Button 'CHECK-XY' and then click 'left top / right bottom' to get black background around the video .

Code: Select all

MODIFIED = 20170914
;CREATED = 20121030
NAME1    = BLACK_%modified%

;=========== BLACK BACKGROUND ==================================
/*
--------- watch video with black background

          How to use=
          -----------
          start URL-VIDEO and then :
                     -1 click button CheckXY
                     -2 XY leftclick left  top
                     -3 XY leftclick right bottom

          rightclick in GUI :  move the hole
          
          Default           :  default-size
          
          Last              :  last saved size

          Escape            :  toggle GUI-minimize/maximize
          
          QUIT              :  use button QUIT to close script
*/
;===============================================================

#Warn
#singleinstance,force
setworkingdir, %a_scriptdir%
autotrim,off
Settitlematchmode, 2
DetectHiddenText,on
DetectHiddenWindows, on
CoordMode,Pixel,Screen
CoordMode,Mouse,Screen

t:=""
Gui,4:default
Gui,4: -DPIScale


RSSINI=%A_scriptdir%\BLACKINI.txt

;------------ check TrayHeight ----------------
xx :=100
WinGetPos, TTX, TTY, TTW, TrayHeight, ahk_class Shell_TrayWnd,,,
minus1:=(A_screenheight*0.001)/xx
HA    :=(A_screenheight-TrayHeight-minus1)
WA    :=(A_screenwidth)

GW    :=(WA*99.5)/xx      ;- width    Gui Width
GH    :=(HA*99  )/xx      ;- height   Gui Height

SW1  :=(WA*100)/xx    ;width show
SW2  :=(WA*100)/xx    ;width region
SH1  :=(HA*100 )/xx   ;height

Gui,4:-border +AlwaysOnTop
Gui,4:Color,Black   

Menu, Tray, Add, OPEN, GuiShow
Menu, Tray, Default,OPEN
Menu, Tray, Add, Close,CLOSE
Menu, Tray, Add, About, About
Menu, Tray, Add
Menu, Tray, Add, Exit, Exit
Menu, Tray, NoStandard

i=0
y   :=(ha*1.01)/xx
x   :=(wa*1.66 )/xx
LY1 :=(HA*96  )/xx
w   :=(wa*5 )/xx
h   :=(ha*2.5)/xx
sect1=Check-XY,Default,LAST,QUIT,Youtube,Archive,NL_Archive,UK_Pathe,PT_Oldies,NL_Lubach,US_Oldies,RU_Masha
sect=%sect1%
stringsplit,k,sect,`,
           total:=k0
Gui,4:add,text,section x%x% y%ly1% w0 h0,
 Loop,%total%
    {
    i++
    ck:=k%a_index%
    Gui,4:Add,Progress, x+15   y%ly1% w%w% h%h%  Disabled BackgroundBlack
    if (i>4)
      Gui,4:Add,Text,     xp   yp  wp  hp   cGray    BackgroundTrans Center 0x200 gStart2 ,%ck%
    else
      Gui,4:Add,Text,     xp   yp  wp  hp   cYellow  BackgroundTrans Center 0x200 gStart2 ,%ck%
    }

Gui,4:show,x0 y0 h%SH1% w%SW1%,%NAME1%
gosub,default
return
;------------------------------------------------------
exit:
exitapp

Start2:
gui,2:submit,nohide
r:= a_guicontrol
;msgbox, 262208,TEST ,%r%
goto,%r%
return


;--------------------------------------------------------
default:
WinSet, Region,, %NAME1%
X1  :=(WA*.1  )/xx
Y1  :=(HA*.1  )/xx

B   :=(WA*99.7 )/xx
H   :=(HA*94   )/xx

X2  :=(X1+B)            ;-- 2. x-position
Y2  :=(Y1+H)            ;-- 2. y-position
WinSet,Region, 0-0 %SW2%-0 %SW2%-%SH1% 0-%SH1% 0-0     %X1%-%Y1%   %X2%-%Y1%  %X2%-%Y2%   %X1%-%Y2% %X1%-%Y1%,%NAME1%
return


;--------- read last position and size ------------------
LAST:
  IniRead, X1  , %rssini% , Position1 , X1
  IniRead, X2  , %rssini% , Position1 , X2
  IniRead, Y1  , %rssini% , Position1 , Y1
  IniRead, Y2  , %rssini% , Position1 , Y2
       HL:=(Y2-Y1)
       BL:=(X2-X1)

       WinSet, Region,, %NAME1%
       WinSet,Region, 0-0 %SW2%-0 %SW2%-%SH1% 0-%SH1% 0-0     %X1%-%Y1%   %X2%-%Y1%  %X2%-%Y2%   %X1%-%Y2% %X1%-%Y1%,%NAME1%
return
;---------------------------------------------------------
quit:
exitapp
;-------------

;----------- define XY1/2 --------------------------------
CHECK-XY:
V=0
settimer,aas,500
return
;-----------------
AAS:
settimer,aas,20
MouseGetPos,Xa,Ya
ToolTip X=%Xa%   Y=%Ya%

GetKeyState,state, LButton
if state=D
   {
   V++
   M:=mod(V,2)
   if M=1
       {
       MouseGetPos,X1,Y1
       KeyWait, LButton, U
       ToolTip Position1 is set X=%X1%   Y=%Y1%
       sleep,800
       return
       }

    else
       {
       settimer,aas,off
       MouseGetPos,X2,Y2
       KeyWait, LButton, U
       tooltip
       WinSet, Region,, %NAME1%
       WinSet,Region, 0-0 %SW2%-0 %SW2%-%SH1% 0-%SH1% 0-0     %X1%-%Y1%   %X2%-%Y1%  %X2%-%Y2%   %X1%-%Y2% %X1%-%Y1%,%NAME1%
       IniWrite, %X1% , %rssini% ,Position1 , X1
       IniWrite, %X2% , %rssini% ,Position1 , X2
       IniWrite, %Y1% , %rssini% ,Position1 , Y1
       IniWrite, %Y2% , %rssini% ,Position1 , Y2
       HL:=(Y2-Y1)
       BL:=(X2-X1)
       return
       }
   }
return
;---------------------------------------


4GuiContextMenu:              ;-- when rightclick in GUI can move the hole
   {
   Gui,4:submit,nohide
   HLP = %a_GuiControl%
   if HLP=
      {
      MouseGetPos,x1,y1
      X2  :=(X1+BL)       ;2. x-position
      Y2  :=(Y1+HL)       ;2. y-position
      WinSet, Region,, %NAME1%
      WinSet,Region, 0-0 %SW2%-0 %SW2%-%SH1% 0-%SH1% 0-0     %X1%-%Y1%   %X2%-%Y1%  %X2%-%Y2%   %X1%-%Y2% %X1%-%Y1% ,%NAME1%
      return
      }
   return
   }
return
;------------------------------------


;------------- menu tray ------------
About:
ee=
  (
  start URL
  -1 click button CheckXY
  -2 XY leftclick left  above
  -3 XY leftclick right down
  )
msgbox, 262208,INFO,%modified%`n%ee%
Return
;---------
GuiShow:
reload
Return

;-------------------------------------

close:
4Guiclose:
esc::
t:=!t
if t
   Gui,4:show,minimize
else
   Gui,4:show,maximize
Return
;------------------

NL_archive:
;run,https://www.youtube.com/channel/UC3Xw6AMOJIiUJ0cNHB7CdIg
run,https://www.youtube.com/user/BeeldenGeluid/videos
gosub,default
return

uk_Pathe:
;run,https://www.youtube.com/channel/UCGp4u0WHLsK8OAxnvwiTyhA
run,https://www.youtube.com/user/britishpathe/videos
gosub,default
return

PT_Oldies:
run,https://www.youtube.com/channel/UCLcXSPiATFcJU0ddNywFmFw/videos
gosub,default
return

NL_Lubach:
run,https://www.youtube.com/channel/UCdH_8mNJ9vzpHwMNwlz88Zw/videos
gosub,default
return

US_Oldies:
run,https://www.youtube.com/channel/UCDG3-niIfxCzj7BWd2hgQLQ/videos
gosub,default
return

RU_Masha:
run,https://www.youtube.com/user/MashaBearEN/videos
gosub,default
return


Youtube:
;run,https:www.youtube.com
run,https://www.youtube.com/results?search_query=jim+reeves&search_type=
;run,https://www.youtube.com/watch?v=nECoA-uVGfw
gosub,default
return


Archive:
;run,https://archive.org/details/MissTelevision1950Contest
;run,https://archive.org/details/JimReeves_201702
;run,https://archive.org/details/MyJapan1945
run,https://archive.org/details/Comedy_Films
gosub,default
return
;===============================================================
gtaking
Posts: 5
Joined: 12 May 2016, 00:26
Contact:

Re: Youtube Watcher v.3 - a simple Youtube Turn-off Lights script

16 Sep 2017, 11:28

@RUNIE
ty.

its just that my 30 y.o. eyes cant handle any nonsense text / visual of any form anymore. pure black ftw. this script born from personal preferences (as the script file is portable across different machines, works after drive format...)


@Exaskryz
thank you for nice feedback. it seems with these constraints there isnt many options one can choose to do.

after crazy css3 update i got the feeling that in the future browsers will transform into a new kind of graphic ui platform in between simple os window and high end gpu graphic interface. and if thats whats gonna happen, we wont be getting any window control out of that.

in this case next step would be to find a simple rectangle edge finder algorithm from the sea of image processing algorithms. simplifying a known solution shouldnt be that hard but im not the guy ill pass. many sites use same solid bg color for edges near vid frame, should be relatively easy tho.

a gui.ahk asking toolbar preferences and saving into .ini idea was cool i admit it. but 3 files or 2 files + visible button or browser config file reader :D would be too complicated for my intent to keep things simplest. especially when forum users who can use that file arrangement are the same guys who can change initial values at the beginning of the code easily, no?

if i listen you guys it will transform into a real million-download release. this forum is good.

as for full browser support, ty for reminding me im the one who has to do it right now xD its been added


@garry
you can modify in any way you want bro, in any way it fit your needs

didnt add those modifications because they werent too common:

- a hotkey for AddingBlackBack: didnt really want to confuse people with a crazy key combo i found
- its automatic, related to browser-height in youtube theater mode (if ive understood your question right)
- thats a thing in win10 :) send-gui-to-back worked but taskbar missed it in the first time. you have to wait and send your gui to back again for workaround (duplicate send2back line and sleep between them). or we could activate tray. oh wait... =.=^ ok, thats been added in v.3 also.
gtaking
Posts: 5
Joined: 12 May 2016, 00:26
Contact:

Re: Youtube Watcher v.3 - a simple Youtube Turn-off Lights script

16 Sep 2017, 11:32

[v.3 is ready]

fixes (just when i thought this simple piece of code was perfect xD)

- columns were overflowing due to very small window
- when video stops getting larger top black space on render area was vanishing but our video frame didnt
- on script start and on tray menu usage black background was getting focus, hiding everything


added features (as always, adjusted to my toolbar preferences xD)

- Chrome Opera Vivaldi
- Safari still doesnt support new youtube xD

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Google [Bot] and 123 guests