Skip to content

Instantly share code, notes, and snippets.

@dvdbng
Created October 20, 2018 22:18
Show Gist options
  • Save dvdbng/7e93785380700a3e26a062b2d6113936 to your computer and use it in GitHub Desktop.
Save dvdbng/7e93785380700a3e26a062b2d6113936 to your computer and use it in GitHub Desktop.
VLC: Set a different playback speed for dialog and not-dialog scenes (using subtitles)
#!/usr/bin/env python3
# This changes the playback speed to a different speed when there is and when there isn't subtitles.
# You need to activate the Lua HTTP interface in VLC and
# configure the password here, then run the script with a .srt as first argument.
# Ended up not being very nice because VLC janks for a few milliseconds when the playback speed changes
import requests
import pysrt
import sys
import time
import re
PASSWORD = '1234'
SUB_RATE = 0.75
NOSUB_RATE = 1.25
sub = pysrt.open(sys.argv[1])
def get(url):
return requests.get(url, auth=('', PASSWORD)).text
play_time = 0
play_time_at = time.time()
def set_speed(rate):
global play_time, play_time_at
resp = get('http://127.0.0.1:8080/requests/status.xml?command=rate&val=%s' % rate)
play_time_at = time.time()
position = float(re.search('<position>([^<]+)</position>', resp).group(1))
length = float(re.search('<length>([^<]+)</length>', resp).group(1))
play_time = position * length
current_rate = 1
set_speed(NOSUB_RATE)
while True:
current_time = play_time + (time.time() - play_time_at) * current_rate
have_sub = len(sub.at(int(current_time * 1000))) > 0
new_rate = SUB_RATE if have_sub else NOSUB_RATE
if current_rate != new_rate:
current_rate = new_rate
set_speed(new_rate)
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment