Trouble synchronizing start of LED routine to the start of VLC video playback using Python libVLC and Raspberry Pi

I am trying to synchronise LEDs flashing with a video being played using a Raspberry Pi 3B+ and the Python wrappers for libVLC. For example, I want the LED to flash red at exactly 2000ms of video playback and green at 5500ms etc.

I can get the timings in-between the LED flashes accurate enough in Python (to within a few ms) but I cannot seem to synchronise the start of the video playback with the start of the timing for the LEDs. I have already tried a few things

1. Initialise the video player, load the media, sleep in the script a few seconds to let the player start and load the media. Call play() and immediately start the LED timings.

The playback is always delayed after play is called for 100+ms it seems causing the LEDs to trigger earlier than wanted

2. Query for State.Playing using player.get_state() in a while True loop and starting the LEDs immediately after detection

The state change seems to be delayed by 100+ms despite the get_state() being called in a busy loop, this causes the LED routine to start late

3. Adding a few seconds of black screen to the start of the video, calling player.get_time() after a second or so and sleeping for the duration until the ‘start’ of the real part of the video

get_time() doesn’t seem to give accurate timings, it appears to only update itself every 300ms or so giving it a large window where it is off the actual video progress.

Are there any alternatives I should be considering here? Any way to get more accurate timing for when to start the LED routine? I preferably want the delay to be less than 30ms so that the difference is mostly imperceptible to the eye..

For reference here is some code I have been trying so far:

Attempt 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>player = vlc.MediaPlayer()
player.toggle_fullscreen()
media = vlc.Media('/home/pi/Downloads/test.mp4')
player.set_media(media)
sleep(5)
player.play()
startLedRoutine()
</code>
<code>player = vlc.MediaPlayer() player.toggle_fullscreen() media = vlc.Media('/home/pi/Downloads/test.mp4') player.set_media(media) sleep(5) player.play() startLedRoutine() </code>
player = vlc.MediaPlayer()
player.toggle_fullscreen()
media = vlc.Media('/home/pi/Downloads/test.mp4')
player.set_media(media)
sleep(5)
player.play()
startLedRoutine()

Attempt 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>player = vlc.MediaPlayer()
player.toggle_fullscreen()
media = vlc.Media('/home/pi/Downloads/test.mp4')
player.set_media(media)
sleep(5)
player.play()
while True:
if player.get_state() == vlc.State.Playing:
break
startLedRoutine()
</code>
<code>player = vlc.MediaPlayer() player.toggle_fullscreen() media = vlc.Media('/home/pi/Downloads/test.mp4') player.set_media(media) sleep(5) player.play() while True: if player.get_state() == vlc.State.Playing: break startLedRoutine() </code>
player = vlc.MediaPlayer()
player.toggle_fullscreen()
media = vlc.Media('/home/pi/Downloads/test.mp4')
player.set_media(media)
sleep(5)
player.play()

while True:
    if player.get_state() == vlc.State.Playing:
        break

startLedRoutine()

Attempt 3:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>player = vlc.MediaPlayer()
player.toggle_fullscreen()
media = vlc.Media('/home/pi/Downloads/test.mp4')
player.set_media(media)
sleep(5)
player.play()
sleep(5) # 10 seconds of black screen has been added to the start of the video
sleep(10 - player.get_time() * 1000) # sleep for the remainder of the time before 10s is reached
startLedRoutine()
</code>
<code>player = vlc.MediaPlayer() player.toggle_fullscreen() media = vlc.Media('/home/pi/Downloads/test.mp4') player.set_media(media) sleep(5) player.play() sleep(5) # 10 seconds of black screen has been added to the start of the video sleep(10 - player.get_time() * 1000) # sleep for the remainder of the time before 10s is reached startLedRoutine() </code>
player = vlc.MediaPlayer()
player.toggle_fullscreen()
media = vlc.Media('/home/pi/Downloads/test.mp4')
player.set_media(media)
sleep(5)
player.play()
sleep(5) # 10 seconds of black screen has been added to the start of the video
sleep(10 - player.get_time() * 1000) # sleep for the remainder of the time before 10s is reached
startLedRoutine()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật