Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust m3u8 target duration #5

Merged
merged 8 commits into from
Nov 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/orca_hls_utils/DateRangeHLSStream.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class DateRangeHLSStream():
start_unix_time
end_unix_time
wav_dir
overwrite_output: allows ffmpeg to overwrite output, default is False
real_time = if False, get data as soon as possible, if true wait for polling interval before pulling
"""

def __init__(self, stream_base, polling_interval, start_unix_time, end_unix_time, wav_dir, real_time=False):
def __init__(self, stream_base, polling_interval, start_unix_time, end_unix_time, wav_dir, overwrite_output=False, real_time=False):
"""

"""
Expand All @@ -47,6 +48,7 @@ def __init__(self, stream_base, polling_interval, start_unix_time, end_unix_time
self.start_unix_time = start_unix_time
self.end_unix_time = end_unix_time
self.wav_dir = wav_dir
self.overwrite_output = overwrite_output
self.real_time = real_time
self.is_end_of_stream = False

Expand Down Expand Up @@ -101,10 +103,11 @@ def get_next_clip(self, current_clip_name = None):
(self.stream_base), (current_folder))
stream_obj = m3u8.load(stream_url)
num_total_segments = len(stream_obj.segments)
num_segments_in_wav_duration = math.ceil(self.polling_interval_in_seconds/stream_obj.target_duration)
target_duration = sum([item.duration for item in stream_obj.segments])/num_total_segments
num_segments_in_wav_duration = math.ceil(self.polling_interval_in_seconds/target_duration)

# calculate the start index by computing the current time - start of current folder
segment_start_index = math.ceil(datetime_utils.get_difference_between_times_in_seconds(self.current_clip_start_time, current_folder)/stream_obj.target_duration)
segment_start_index = math.ceil(datetime_utils.get_difference_between_times_in_seconds(self.current_clip_start_time, current_folder)/target_duration)
segment_end_index = segment_start_index + num_segments_in_wav_duration

if segment_end_index > num_total_segments:
Expand Down Expand Up @@ -144,7 +147,7 @@ def get_next_clip(self, current_clip_name = None):
# read the concatenated .ts and write to wav
stream = ffmpeg.input(os.path.join(tmp_path, Path(hls_file)))
stream = ffmpeg.output(stream, wav_file_path)
ffmpeg.run(stream, quiet=False)
ffmpeg.run(stream, overwrite_output=self.overwrite_output, quiet=False)

# clear the tmp_path
os.system(f'rm -rf {tmp_path}')
Expand All @@ -166,4 +169,4 @@ def get_next_clip(self, current_clip_name = None):

def is_stream_over(self):
# returns true or false based on whether the stream is over
return int(self.current_clip_start_time) >= int(self.end_unix_time)
return int(self.current_clip_start_time) >= int(self.end_unix_time)