It is currently April 13th, 2021, 10:31 am

Adding leading zeros to video position of WebNowPlaying

Help with creating, editing & fixing problems with skins
CrispyChips
Posts: 5
Joined: March 28th, 2021, 8:04 pm

Adding leading zeros to video position of WebNowPlaying

Post by CrispyChips »

EDIT: Thanks to balala for helping me!
\/ Fixed code \/

Code: Select all

[Rainmeter]
Update=100

[Background]
Meter=IMAGE
W=500
H=100
SolidColor=0,0,0,1

;; -- Measures -- ;;

[MeasurePosition]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Position
UpdateDivider=5
RegExpSubstitute=1
; Add leading zeros to the returned position (and fix Spotify returning ':NaN:NaN' when no song is loaded).
Substitute="^(\d{1}):(\d{2})$":"00:0\1:\2","^(\d{2}):(\d{2})$":"00:\1:\2","^(\d{1}):(\d{2}):(\d{2})$":"0\1:\2:\3",":NaN:NaN":"--:--:--"

[MeasureState]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=State
; If the returned state is unloaded or finished, display '--:--:--'.
IfCondition=(MeasureState = 0) || (MeasureState = 3)
IfTrueAction=[!SetOption MeterPosition Text "--:--:--"]
; If the returned state is playing or paused, display the video position.
IfCondition2=(MeasureState = 1) || (MeasureState = 2)
IfTrueAction2=[!SetOption MeterPosition Text [MeasurePosition]]
IfConditionMode=1

;; -- Meters -- ;;

[MeterPosition]
Meter=String
MeasureName=MeasurePosition
Text=%1
X=500
FontColor=255,255,255
FontFace=VCR OSD MONO
FontSize=75
StringAlign=Right
AntiAlias=1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------



When using WebNowPlaying's Position measure, it will output the time of the current video position as HH:MM:SS, for example:

Code: Select all

    0:35
    9:59
   12:19
   59:59
 1:26:04
I would like to add leading zeros to the position so that instead it reads as:

Code: Select all

00:00:35
00:09:59
00:12:19
00:59:59
01:26:04
I've tried using RegExpSubstitutions, but I don't know how to account for the variable number of characters the Position measure will return.
If the time is 0:35 I would need to add 00:0 to the front, if the time is 1:26:04 I would need to add just 0 for example. Does anyone know how I can make this work?

WebNowPlaying Plugin: https://github.com/tjhrulz/WebNowPlaying

Code: Select all

[Rainmeter]
Update=100

[Background]
Meter=IMAGE
W=500
H=100
SolidColor=0,0,0,1

[MeasurePosition]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Position
UpdateDivider=5
Substitute="0:00":"--:--:--"

[MeterPosition]
Meter=String
Text=[MeasurePosition]
X=500
FontColor=255,255,255
FontFace=Trebuchet MS
FontSize=75
StringAlign=Right
AntiAlias=1
DynamicVariables=1
Last edited by CrispyChips on March 30th, 2021, 8:53 pm, edited 5 times in total.
User avatar
ikarus1969
Posts: 426
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Adding leading zeros to video position of WebNowPlaying

Post by ikarus1969 »

edit: just forghet my previous solution. It works much easier without lua
  • removed Substitute from [MeasurePosition]
  • added the new measure [MeasurePosition_Substituted] and take this for the meter [MeterPosition]

Code: Select all

[MeasurePosition]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Position
UpdateDivider=5
DynamicVariables=1

[MeasurePosition_Substituted]
Measure=STRING
String=00000000[MeasurePosition]
RegexpSubstitute=1
Substitute="(\d{3,})(\d\D.+)$":"00:00:0\2",".+(\d\d:\d\d:\d\d)$":"\1","00:00:00":"--:--:--"
DynamicVariables=1

CrispyChips
Posts: 5
Joined: March 28th, 2021, 8:04 pm

Re: Adding leading zeros to video position of WebNowPlaying

Post by CrispyChips »

This almost works, but when at a position in a video between 10:00 - 59:59 (when there's four numbers being used) and past 10:00:00 (when there's six numbers being used) the fourth and sixth digit respectively are still substituted with zero when they shouldn't be. For example, numbers like:

Code: Select all

   11:45
   37:25
19:21:46
45:23:51
become:

Code: Select all

   01:45
   07:25
09:21:46
05:23:51

Code: Select all

[Rainmeter]
Update=100

[Background]
Meter=IMAGE
W=500
H=100
SolidColor=0,0,0,1

[MeasurePosition]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Position
UpdateDivider=5

[MeasurePosition_Substituted]
Measure=String
String=00000000[MeasurePosition]
RegexpSubstitute=1
Substitute="(\d{3,})(\d\D.+)$":"00:00:0\2",".+(\d\d:\d\d:\d\d)$":"\1","00:00:00":"--:--:--"
DynamicVariables=1

[MeterPosition]
Meter=String
Text=[MeasurePosition_Substituted]
X=500
FontColor=255,255,255
FontFace=Trebuchet MS
FontSize=75
StringAlign=Right
AntiAlias=1
DynamicVariables=1
User avatar
balala
Rainmeter Sage
Posts: 12545
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Adding leading zeros to video position of WebNowPlaying

Post by balala »

CrispyChips wrote: March 29th, 2021, 5:56 pm This almost works, but when at a position in a video between 10:00 - 59:59 (when there's four numbers being used) and past 10:00:00 (when there's six numbers being used) the fourth and sixth digit respectively are still substituted with zero when they shouldn't be. For example, numbers like:
I wouldn't complicate things to much. Try to add the following two options to the [MeasurePosition] measure (if you want to try this, make sure you've removed the previously added options / measures):

Code: Select all

[MeasurePosition]
...
RegExpSubstitute=1
Substitute="^(\d{1}):(\d{2})$":"00:0\1:\2","^(\d{2}):(\d{2})$":"00:\1:\2","^(\d{1}):(\d{2}):(\d{2})$":"0\1:\2:\3"
Now you can use this measure directly into the [MeterPosition], as has been done. But here again is a mention which worth to be taken into account: recommend not to use a section variable (Text=[MeasurePosition]) into the meter:

Code: Select all

[MeterPosition]
Meter=String
MeasureName=MeasurePosition
X=500
FontColor=255,255,255
FontFace=Trebuchet MS
FontSize=75
StringAlign=Right
AntiAlias=1
Text=%1
Note that along with replacing the Text option and adding the MeasureName, I also removed the DynamicVariables=1 option, which in this case not needed anymore.
CrispyChips
Posts: 5
Joined: March 28th, 2021, 8:04 pm

Re: Adding leading zeros to video position of WebNowPlaying

Post by CrispyChips »

Thanks for your help, it works now!

I also wanted to see if I could get the position to display --:--:-- when a video has stopped or isn't currently loaded. Originally I did this by replacing 0:00 with --:--:--, as that is what WebNowPlaying's position measure returns when nothing is playing. However, I would still like 00:00:00 to display during the first second of the video and position returns the video duration when a video finishes playing, not 0:00.

I figure I can use the state measure to determine when a video is unloaded or finished, but I don't know how to replace the text in MeasurePosition using that.
User avatar
balala
Rainmeter Sage
Posts: 12545
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Adding leading zeros to video position of WebNowPlaying

Post by balala »

CrispyChips wrote: March 30th, 2021, 1:58 am I also wanted to see if I could get the position to display --:--:-- when a video has stopped or isn't currently loaded. Originally I did this by replacing 0:00 with --:--:--, as that is what WebNowPlaying's position measure returns when nothing is playing. However, I would still like 00:00:00 to display during the first second of the video and position returns the video duration when a video finishes playing, not 0:00.

I figure I can use the state measure to determine when a video is unloaded or finished, but I don't know how to replace the text in MeasurePosition using that.
Just add an additional ,"00:00:00":"--:--:--" substitution, to the end of the Substitute option of the [MeasurePosition] measure. With this the option becomes: Substitute="^(\d{1}):(\d{2})$":"00:0\1:\2","^(\d{2}):(\d{2})$":"00:\1:\2","^(\d{1}):(\d{2}):(\d{2})$":"0\1:\2:\3","00:00:00":"--:--:--".
Test this please and if there is something which doesn't work please let me know. The suggested solution (using the State PlayerType option), might work, however so far I'm not sure you have to use it. So first please test the above Substitute extension, then we'll see.
CrispyChips
Posts: 5
Joined: March 28th, 2021, 8:04 pm

Re: Adding leading zeros to video position of WebNowPlaying

Post by CrispyChips »

I would like the position to only display --:--:-- when a video isn't loaded or just finished which is determined by the state measure. Replacing 00:00:00 will only work for when the video isn't loaded.

I've been trying to use the state measure to determine when to use --:--:-- and when to use the position measure, but now the position won't update properly while a video is playing.

BTW, the state measure return values for reference:

Code: Select all

0 = Unloaded
1 = Playing
2 = Paused
3 = Finished

Code: Select all

[Rainmeter]
Update=100

[Background]
Meter=IMAGE
W=500
H=100
SolidColor=0,0,0,1

;; -- Measures -- ;;

[MeasurePosition]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Position
UpdateDivider=5
RegExpSubstitute=1
; Add leading zeros to the returned position.
Substitute="^(\d{1}):(\d{2})$":"00:0\1:\2","^(\d{2}):(\d{2})$":"00:\1:\2","^(\d{1}):(\d{2}):(\d{2})$":"0\1:\2:\3"

[MeasureState]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=State
; If the returned state is unloaded or finished, display '--:--:--'.
IfCondition=(MeasureState = 0) || (MeasureState = 3)
IfTrueAction=[!SetOption MeterPosition Text "--:--:--"]
; If the returned state is playing or paused, display the video position.
IfCondition2=(MeasureState = 1) || (MeasureState = 2)
IfTrueAction2=[!SetOption MeterPosition Text [MeasurePosition]]

;; -- Meters -- ;;

[MeterPosition]
Meter=String
MeasureName=MeasurePosition
Text=%1
X=500
FontColor=255,255,255
FontFace=VCR OSD MONO
FontSize=75
StringAlign=Right
AntiAlias=1
User avatar
balala
Rainmeter Sage
Posts: 12545
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Adding leading zeros to video position of WebNowPlaying

Post by balala »

CrispyChips wrote: March 30th, 2021, 6:13 pm I've been trying to use the state measure to determine when to use --:--:-- and when to use the position measure, but now the position won't update properly while a video is playing.
Try to add an IfConditionMode=1 option to the [MeasureState] measure.
CrispyChips
Posts: 5
Joined: March 28th, 2021, 8:04 pm

Re: Adding leading zeros to video position of WebNowPlaying

Post by CrispyChips »

Oh wow, that was all I was missing huh?

I'm not new to coding, but I am new to rainmeter's syntax. Thanks for all the help!
User avatar
balala
Rainmeter Sage
Posts: 12545
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Adding leading zeros to video position of WebNowPlaying

Post by balala »

CrispyChips wrote: March 30th, 2021, 6:33 pm Oh wow, that was all I was missing huh?

I'm not new to coding, but I am new to rainmeter's syntax. Thanks for all the help!
You're welcome.
Yes, Rainmeter is weird for many got used with real programming languages. Even if myself also had a little of programming background, that was not too extended. So working a lot in Rainmeter (what I definitely do), I started to see different things, not extremely obvious for Rainmeter beginners.