From ae2aa44901b58bb964e1db198e01276d892464fc Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Thu, 2 Jul 2020 20:55:42 +0200 Subject: [PATCH] rename enc thread, add ts cleanup thread --- ffplayout/output/hls.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/ffplayout/output/hls.py b/ffplayout/output/hls.py index 4e8a272d..765397cf 100644 --- a/ffplayout/output/hls.py +++ b/ffplayout/output/hls.py @@ -1,3 +1,6 @@ +import os +import re +from glob import iglob from subprocess import PIPE, Popen from threading import Thread @@ -8,6 +11,25 @@ from ffplayout.utils import (_ff, _log, _playlist, _playout, stdin_args, terminate_processes) +def clean_ts(): + playlists = re.findall(r'[/\w.]+m3u8', _playout.hls_output) + + for playlist in playlists: + test_num = 0 + hls_path = os.path.dirname(playlist) + with open(playlist, 'r') as m3u8: + for line in m3u8: + if '.ts' in line: + test_num = int(re.findall(r'(\d+).ts', line)[0]) + break + + for ts_file in iglob(os.path.join(hls_path, '*.ts')): + ts_num = int(re.findall(r'(\d+).ts', ts_file)[0]) + + if test_num > ts_num: + os.remove(ts_file) + + def output(): """ this output is hls output, no preprocess is needed. @@ -44,11 +66,15 @@ def output(): _ff.encoder = Popen(cmd, stdin=PIPE, stderr=PIPE) - enc_thread = Thread(target=ffmpeg_stderr_reader, - args=(_ff.encoder.stderr, True)) - enc_thread.daemon = True - enc_thread.start() - enc_thread.join() + stderr_reader_thread = Thread(target=ffmpeg_stderr_reader, + args=(_ff.encoder.stderr, False)) + stderr_reader_thread.daemon = True + stderr_reader_thread.start() + stderr_reader_thread.join() + + ts_cleaning_thread = Thread(target=clean_ts) + ts_cleaning_thread.daemon = True + ts_cleaning_thread.start() except BrokenPipeError: messenger.error('Broken Pipe!')