Skip to content

Commit

Permalink
record: Log precise timings of the first live segments
Browse files Browse the repository at this point in the history
This change replaces repeating "Downloaded segment N" with a single line per file.
  • Loading branch information
TheDrHax committed Jun 23, 2023
1 parent 216d4c2 commit a2501d8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions twitch_utils/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import sys
import math
import itertools
from datetime import datetime
from typing import Dict, Union, Any

try:
Expand Down Expand Up @@ -61,13 +62,15 @@ def __init__(self, url: str,
threads: int = 1,
api: Union[TwitchAPI, None] = None,
start: int = 0,
end: Union[int, None] = None):
end: Union[int, None] = None,
live: bool = False):
self.url = url
self.quality = quality
self.threads = threads
self.api = api
self.start = start
self.end = end
self.live = live

def copy(self):
return Stream(self.url, self.quality, self.threads,
Expand Down Expand Up @@ -113,6 +116,7 @@ def download(self, dest: str) -> int:
sl_proc = Popen(sl_cmd, **sl_kwargs)

expected, downloaded = [-1] * 2
first_segment = True

while True:
line = sl_proc.stderr.readline()
Expand All @@ -133,7 +137,18 @@ def download(self, dest: str) -> int:
expected = queued['segment']
elif complete:
segment = complete['segment']
print(f'Downloaded segment {segment} out of {expected}')

if self.live and first_segment:
# Log precise timings to leave some traces for manual
# checks of recording's consistency
# For example: If the following calculation
# (ts2 - ts1) - (inpoint2 - inpoint1)
# is > 0, we can assume that Twitch has lost some segments
# and the stream has become shorter by this amount.
ts = datetime.now().timestamp()
inpoint = Clip(dest).inpoint
print(f'Clip {dest} started at {ts} with offset {inpoint}')
first_segment = False

if downloaded == -1 or downloaded + 1 == segment:
downloaded = segment
Expand Down Expand Up @@ -205,7 +220,8 @@ def record(channel_name: str, vod_id: str, vod_url: Union[str, None] = None,
stream = Stream(f'https://twitch.tv/{channel_name}',
api=api,
quality=quality,
threads=threads)
threads=threads,
live=True)

if not vod_url:
vod_url = f'https://twitch.tv/videos/{vod_id}'
Expand Down

0 comments on commit a2501d8

Please sign in to comment.