Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SpotifyBackup
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robert Goldmann
SpotifyBackup
Commits
9b062917
Commit
9b062917
authored
1 year ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
SpotifyRecorder: improved final duration check
parent
4c77e89e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
SpotifyRecorder.py
+13
-8
13 additions, 8 deletions
SpotifyRecorder.py
with
13 additions
and
8 deletions
SpotifyRecorder.py
+
13
−
8
View file @
9b062917
...
...
@@ -103,8 +103,8 @@ class SpotifyRecorder:
recorder
=
SpotifyAudioRecorder
(
self
.
_audioDeviceName
,
filePath
)
with
recorder
.
record
():
self
.
__play_track
(
deviceId
,
track
[
'
track
'
][
'
uri
'
])
self
.
__wait_for_track_playing
()
self
.
__wait_for_track_end
(
track
)
timeWaitedForPlaying
=
self
.
__wait_for_track_playing
()
self
.
__wait_for_track_end
(
track
,
timeWaitedForPlaying
)
recordedTracks
.
append
(
track
[
'
track
'
][
'
name
'
])
except
Exception
as
e
:
...
...
@@ -120,7 +120,7 @@ class SpotifyRecorder:
fileName
=
f
'
{
index
}
-
{
artists
}
-
{
track
[
"
track
"
][
"
name
"
]
}
.wav
'
return
os
.
path
.
join
(
self
.
_destinationFolder
,
fileName
)
def
__wait_for_track_end
(
self
,
track
)
:
def
__wait_for_track_end
(
self
,
track
,
timeWaitedForPlaying
:
float
)
->
None
:
trackDurationInMs
=
track
[
'
track
'
][
'
duration_ms
'
]
trackDurationInSeconds
=
trackDurationInMs
//
1000
LOGGER
.
info
(
f
'
Track duration:
{
self
.
__convert_seconds_to_duration
(
trackDurationInSeconds
)
}
'
)
...
...
@@ -140,19 +140,20 @@ class SpotifyRecorder:
LOGGER
.
debug
(
f
'
Waiting for track to end (remaining:
'
f
'
{
self
.
__convert_seconds_to_duration
(
remainingTimeInSeconds
)
}
,
'
f
'
sleep:
{
self
.
__convert_seconds_to_duration
(
sleepTime
)
}
s)...
'
)
f
'
sleep:
{
self
.
__convert_seconds_to_duration
(
int
(
sleepTime
)
)
}
s)...
'
)
time
.
sleep
(
sleepTime
)
else
:
waitDuration
=
int
(
time
.
time
()
-
startTime
)
waitDuration
+=
timeWaitedForPlaying
if
waitDuration
<
trackDurationInSeconds
:
if
waitDuration
<
trackDurationInSeconds
-
self
.
_WAIT_TIME_TRACK_PLAYING_SHORT_IN_S
:
raise
RuntimeError
(
f
'
Track finished too early (waited:
{
waitDuration
}
s, expected:
{
trackDurationInSeconds
}
s)
'
)
if
waitDuration
>
trackDurationInSeconds
+
self
.
_WAIT_TIME_TRACK_PLAYING_SHORT_IN_S
*
3
:
raise
RuntimeError
(
f
'
Track finished too late (waited:
{
waitDuration
}
s, expected:
{
trackDurationInSeconds
}
)
'
)
LOGGER
.
debug
(
f
'
Track finished. Waited
{
waitDuration
}
s, expected
{
trackDurationInSeconds
}
s, OK
'
)
LOGGER
.
info
(
f
'
Track finished. Waited
{
waitDuration
}
s, expected
{
trackDurationInSeconds
}
s, OK
'
)
break
@staticmethod
...
...
@@ -173,16 +174,20 @@ class SpotifyRecorder:
def
__play_track
(
self
,
deviceId
:
str
,
trackUri
:
str
):
self
.
_spotify
.
start_playback
(
device_id
=
deviceId
,
uris
=
[
trackUri
])
def
__wait_for_track_playing
(
self
)
->
None
:
def
__wait_for_track_playing
(
self
)
->
float
:
LOGGER
.
debug
(
f
'
Wait for track to start playing...
'
)
startTime
=
time
.
time
()
duration
=
0
while
time
.
time
()
-
startTime
<
self
.
_MAX_WAIT_TIME_TRACK_STARTING_IN_S
:
if
self
.
_spotify
.
current_playback
()[
'
is_playing
'
]:
LOGGER
.
debug
(
f
'
Track started playing after
{
time
.
time
()
-
startTime
:
.
1
f
}
s
'
)
duration
=
time
.
time
()
-
startTime
LOGGER
.
debug
(
f
'
Track started playing after
{
duration
:
.
1
f
}
s
'
)
break
time
.
sleep
(
1
)
return
duration
if
__name__
==
'
__main__
'
:
with
open
(
'
config/settings-recorder.json
'
,
'
r
'
,
encoding
=
'
utf-8
'
)
as
f
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment