Skip to content

Commit 057a10a

Browse files
committed
Add artwork image support (#1)
1 parent c22f109 commit 057a10a

5 files changed

Lines changed: 61 additions & 25 deletions
-359 KB
Binary file not shown.
472 KB
Loading
1.17 MB
Loading

readme.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
# OBS Spotify Mac
22
An OBS script to display Spotify track info on macOS
33

4-
![screenshot](Screen%20Shot%202021-06-21%20at%2002.02.25.png)
5-
64
# Text Format
75
```
86
{n} = Name
97
{a} = Artist
10-
```
8+
```
9+
# Artwork Image (Browser)
10+
```
11+
Width = 640
12+
Height = 640
13+
```
14+
15+
![screenshot](Screenshot%202022-11-24%20at%2014.41.39%402x.png)
16+
![screenshot](Screenshot%202022-11-24%20at%2014.41.59%402x.png)
17+
18+
# Change log
19+
- 1.0: Initial release
20+
- 1.1: Fixed something
21+
- 1.2: Add artwork image (#1)

spotify.lua

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,60 @@
55

66
obs = obslua
77

8-
source_name = ""
8+
source_name_text = ""
99
format_playing = ""
1010
format_paused = ""
1111
interval = 1
12+
source_name_browser = ""
1213

1314
function update_text(source)
1415
local result = spotify()
1516
local text = "[Spofity not running]"
17+
local artwork = ""
1618
if result ~= "" then
1719
result = split(result, ", ")
1820
local paused = (result[1] == "paused")
1921
local format = paused and format_paused or format_playing
2022
text = format:gsub("{n}", result[2]):gsub("{a}", result[3])
23+
artwork = result[4]
2124
end
2225

2326
local settings = obs.obs_data_create()
24-
obs.obs_data_set_string(settings, "text", text)
27+
if obs.obs_source_get_id(source) == 'browser_source' then
28+
obs.obs_data_set_string(settings, "url", artwork)
29+
-- obs.obs_data_set_string(settings, "width", 640)
30+
-- obs.obs_data_set_string(settings, "height", 640)
31+
else
32+
obs.obs_data_set_string(settings, "text", text)
33+
end
2534
obs.obs_source_update(source, settings)
2635
obs.obs_data_release(settings)
36+
2737
end
2838

2939
function timer()
30-
local source = obs.obs_get_source_by_name(source_name) --> obs_source_t
31-
if source ~= nil then
32-
local current_scene_source = obs.obs_frontend_get_current_scene() --> obs_source_t
33-
local current_scene = obs.obs_scene_from_source(current_scene_source) --> obs_scene_t
34-
local scene_item = obs.obs_scene_find_source_recursive(current_scene, source_name) --> obs_sceneitem_t
35-
36-
if (scene_item ~= nil) and obs.obs_sceneitem_visible(scene_item) then
37-
update_text(source)
40+
for i, source_name in ipairs({source_name_text, source_name_browser}) do
41+
local source = obs.obs_get_source_by_name(source_name) --> obs_source_t
42+
if source ~= nil then
43+
local current_scene_source = obs.obs_frontend_get_current_scene() --> obs_source_t
44+
local current_scene = obs.obs_scene_from_source(current_scene_source) --> obs_scene_t
45+
local scene_item = obs.obs_scene_find_source_recursive(current_scene, source_name) --> obs_sceneitem_t
46+
47+
if (scene_item ~= nil) and obs.obs_sceneitem_visible(scene_item) then
48+
update_text(source)
49+
end
50+
obs.obs_source_release(current_scene_source)
3851
end
39-
obs.obs_source_release(current_scene_source)
52+
obs.obs_source_release(source)
4053
end
41-
obs.obs_source_release(source)
4254
end
4355

4456
function restart_timer(settings)
45-
source_name = obs.obs_data_get_string(settings, "source_name")
57+
source_name_text = obs.obs_data_get_string(settings, "source_name_text")
4658
format_playing = obs.obs_data_get_string(settings, "format_playing")
4759
format_paused = obs.obs_data_get_string(settings, "format_paused")
4860
interval = obs.obs_data_get_int(settings, "interval")
61+
source_name_browser = obs.obs_data_get_string(settings, "source_name_browser")
4962

5063
obs.timer_remove(timer)
5164
obs.timer_add(timer, interval * 1000)
@@ -55,7 +68,13 @@ function spotify()
5568
local cmd = "osascript -e " .. [['
5669
if application "Spotify" is running then
5770
tell application "Spotify"
58-
return {the player state, the name of the current track, the artist of the current track}
71+
return {¬
72+
the player state, ¬
73+
the name of the current track, ¬
74+
the artist of the current track, ¬
75+
the artwork url of the current track, ¬
76+
(the player position) / (the (duration of the current track) / 1000) ¬
77+
}
5978
end tell
6079
else
6180
error "Spotify not running."
@@ -83,29 +102,35 @@ function script_description()
83102
<h2>Spotify Mac</h2>
84103
<h4>Display Now Playing Track Info</h4>
85104
<p>{n} = Name &nbsp;&nbsp;&nbsp; {a} = Artist</p>
86-
<p style="font-size: 11pt;"><i><a href="https://github.com/RayPS/obs-spotify-mac">version 1.1</a></i></p>
105+
<p style="font-size: 11pt;"><i><a href="https://github.com/RayPS/obs-spotify-mac">version 1.2</a></i></p>
87106
</center></body></HTML>]]
88107
end
89108

90109
function script_properties()
91110
local props = obs.obs_properties_create()
92111

93-
local p = obs.obs_properties_add_list(props, "source_name", "Text source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
112+
local list_text = obs.obs_properties_add_list(props, "source_name_text", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
113+
obs.obs_properties_add_int(props, "interval", "Update Interval (s)", 1, 60, 1)
114+
obs.obs_properties_add_text(props, "format_playing", "Playing Format", obs.OBS_TEXT_DEFAULT)
115+
obs.obs_properties_add_text(props, "format_paused", "Paused Format", obs.OBS_TEXT_DEFAULT)
116+
obs.obs_properties_add_text(props, "info_text_artwork", "Artwork Image (640×640):", obs.OBS_TEXT_INFO)
117+
local list_browser = obs.obs_properties_add_list(props, "source_name_browser", "Browser Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
118+
94119
local sources = obs.obs_enum_sources()
95120
if sources ~= nil then
96121
for _, source in ipairs(sources) do
97122
source_id = obs.obs_source_get_unversioned_id(source)
98123
if source_id == "text_gdiplus" or source_id == "text_ft2_source" then
99124
local name = obs.obs_source_get_name(source)
100-
obs.obs_property_list_add_string(p, name, name)
125+
obs.obs_property_list_add_string(list_text, name, name)
126+
elseif source_id == "browser_source" then
127+
local name = obs.obs_source_get_name(source)
128+
obs.obs_property_list_add_string(list_browser, name, name)
101129
end
102130
end
103131
end
104132
obs.source_list_release(sources)
105133

106-
obs.obs_properties_add_int(props, "interval", "Update Interval (s)", 1, 60, 1)
107-
obs.obs_properties_add_text(props, "format_playing", "Playing Format", obs.OBS_TEXT_DEFAULT)
108-
obs.obs_properties_add_text(props, "format_paused", "Paused Format", obs.OBS_TEXT_DEFAULT)
109134
return props
110135
end
111136

@@ -118,9 +143,9 @@ end
118143

119144
function script_update(settings)
120145
print("script_update")
121-
if source_name ~= nil then
146+
if source_name_text ~= nil then
122147
restart_timer(settings)
123148
else
124-
print("source_name not set")
149+
print("source_name_text not set")
125150
end
126151
end

0 commit comments

Comments
 (0)