Auto-kill when ffmpeg doesn't output anything for 5m

This commit is contained in:
blank X 2021-04-11 09:38:22 +07:00
parent d37ed659cd
commit 4a7ad768c7
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 69 additions and 52 deletions

View File

@ -165,7 +165,18 @@ async def _video_worker():
command.extend(('-c', 'copy', video_filename))
return command
video_filename = os.path.join(tempdir, video_json['id'] + '.mkv')
proc = await asyncio.create_subprocess_exec(*(await construct_command()))
with open(video_json['id'] + '.mkv', 'ab') as log_file:
async def auto_kill(proc):
last_tell = log_file.tell()
while True:
await asyncio.sleep(300)
current_tell = log_file.tell()
if current_tell == last_tell:
break
last_tell = current_tell
proc.stdin.write(b'q')
await proc.stdin.drain()
proc = await asyncio.create_subprocess_exec(*(await construct_command()), stdin=asyncio.subprocess.PIPE, stdout=log_file, stderr=log_file)
text = 'New video'
if is_late:
text += ' (is late)'
@ -188,7 +199,13 @@ async def _video_worker():
await client.send_file(config['config']['storage_chat_id'], thumbnail_filename, caption=text, parse_mode=None)
os.remove(thumbnail_filename)
for _ in range(50):
auto_kill_task = asyncio.create_task(auto_kill(proc))
await proc.communicate()
auto_kill_task.cancel()
try:
await auto_kill_task
except asyncio.CancelledError:
pass
if not proc.returncode:
break
wait_time = 30
@ -218,7 +235,7 @@ async def _video_worker():
video_json = tmp
break
await asyncio.sleep(wait_time)
proc = await asyncio.create_subprocess_exec(*(await construct_command()))
proc = await asyncio.create_subprocess_exec(*(await construct_command()), stdin=asyncio.subprocess.PIPE, stdout=log_file, stderr=log_file)
except BaseException:
tempdir_obj.cleanup()
raise