This commit is contained in:
2021-05-20 00:31:31 +02:00
parent 30bb08712a
commit 90ea2174db
13 changed files with 10991 additions and 73 deletions

View File

@ -7,7 +7,6 @@ import multiprocessing
import signal
import re
import traceback
import subprocess
from datetime import datetime, timedelta
from sqlalchemy import exc, func
from sqlalchemy.sql.expression import bindparam
@ -45,61 +44,6 @@ def remove_chatcolors(message):
i += 1
return output
silence_start_re = re.compile(' silence_start: (?P<start>[0-9]+(\.?[0-9]*))$')
silence_end_re = re.compile(' silence_end: (?P<end>[0-9]+(\.?[0-9]*)) ')
total_duration_re = re.compile('size=[^ ]+ time=(?P<hours>[0-9]{2}):(?P<minutes>[0-9]{2}):(?P<seconds>[0-9\.]{5}) bitrate=')
def get_chunk_times(path):
proc = subprocess.Popen([
'ffmpeg',
'-i', path,
'-af', 'silencedetect=n=-80dB:d=0.1',
'-f', 'null',
'-'],
stderr=subprocess.PIPE,
stdout=subprocess.DEVNULL
)
output = proc.communicate()[1].decode('utf-8')
lines = output.splitlines()
# Chunks start when silence ends, and chunks end when silence starts.
chunk_starts = []
chunk_ends = []
for line in lines:
silence_start_match = silence_start_re.search(line)
silence_end_match = silence_end_re.search(line)
total_duration_match = total_duration_re.search(line)
if silence_start_match:
start = float(silence_start_match.group('start'))
if start == 0.:
# Ignore initial silence.
continue
chunk_ends.append(start)
if len(chunk_starts) == 0:
# Started with non-silence.
chunk_starts.append(0.)
elif silence_end_match:
chunk_starts.append(float(silence_end_match.group('end')))
elif total_duration_match:
hours = int(total_duration_match.group('hours'))
minutes = int(total_duration_match.group('minutes'))
seconds = float(total_duration_match.group('seconds'))
end_time = hours * 3600 + minutes * 60 + seconds
if len(chunk_starts) == 0:
# No silence found.
chunk_starts.append(0)
if len(chunk_starts) > len(chunk_ends):
if abs(chunk_starts[-1] - end_time) < 0.1:
# Last chunk starts at very the end? nah.
del chunk_starts[-1]
else:
# Finished with non-silence.
chunk_ends.append(end_time)
return list(zip(chunk_starts, chunk_ends))
def parse_demo(path):
jmodified = False
@ -243,13 +187,6 @@ def parse_demo(path):
db.session.rollback()
print('deadlock 5')
# calculate player voice chunks/timing if possible and not exists
if obj['voicetime'] > 0 and 'voice_chunks' not in obj:
voicefile = DEMO_PATH_PREFIX + path + '/voice/' + guid + '.opus'
if os.path.isfile(voicefile):
obj['voice_chunks'] = get_chunk_times(voicefile)
jmodified = True
# insert new player session
player_session = models.PlayerSession(
player_guid=guid,
@ -258,8 +195,7 @@ def parse_demo(path):
chats=obj['chats'],
deaths=obj['deaths'],
kills=obj['kills'],
voicetime=obj['voicetime'],
voice_chunks=obj['voice_chunks'] if 'voice_chunks' in obj else None
voicetime=obj['voicetime']
)
db.session.add(player_session)
db.session.commit()
@ -285,12 +221,18 @@ def parse_demo(path):
elif obj['winner'] == 3:
session.ct_wins += 1
# remove redundant info
eventdata = obj.copy()
del eventdata['tick']
del eventdata['event']
event = models.Event(
player_guid=guid,
session_id=session.id,
time=starttime + timedelta(seconds=obj['tick'] / tickinterval),
tick=obj['tick'],
time=starttime + timedelta(seconds=obj['tick'] * tickinterval),
event=obj['event'],
data=obj
data=eventdata
)
db.session.add(event)
db.session.commit()
@ -318,7 +260,8 @@ def parse_demo(path):
chat = models.Chat(
player_guid=obj['steamid'],
session_id=session.id,
time=starttime + timedelta(seconds=obj['tick'] / tickinterval),
tick=obj['tick'],
time=starttime + timedelta(seconds=obj['tick'] * tickinterval),
name=nick,
chat=msg
)
@ -361,7 +304,7 @@ if __name__ == '__main__':
with open('done.txt', 'r') as fp:
done = set(fp.read().splitlines())
if True:
if False:
done = set()
app = create_app()
with app.app_context():