Skip to content

Commit

Permalink
Merge pull request #5 from valentina-s/adjust_m3u8_target_duration
Browse files Browse the repository at this point in the history
Adjust m3u8 target duration
  • Loading branch information
valentina-s authored Nov 16, 2022
2 parents 44916a9 + e8a46e2 commit cda6871
Showing 1 changed file with 8 additions and 5 deletions.
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)

0 comments on commit cda6871

Please sign in to comment.