FEAT: Streams now save to mp4 after a stream is stopped, instead of discarded

This commit is contained in:
2025-02-11 20:57:27 +00:00
parent 07652eed0d
commit 899764eaf8
7 changed files with 142 additions and 36 deletions

View File

@@ -1,6 +1,9 @@
from celery import Celery, shared_task, Task
from utils.stream_utils import generate_thumbnail, get_streamer_live_status
from time import sleep
from os import listdir, remove
from datetime import datetime
import subprocess
def celery_init_app(app) -> Celery:
class FlaskTask(Task):
@@ -19,11 +22,43 @@ def update_thumbnail(user_id, sleep_time=180) -> None:
"""
Updates the thumbnail of a stream periodically
"""
ffmpeg_wait_time = 5
generate_thumbnail(user_id)
sleep(sleep_time)
# check if user is streaming
while get_streamer_live_status(user_id)['isLive']:
sleep(ffmpeg_wait_time)
generate_thumbnail(user_id)
sleep(sleep_time - ffmpeg_wait_time)
return
@shared_task
def combine_ts_stream(username):
"""
Combines all ts files into a single vod, and removes the ts files
"""
path = f"stream_data/hls/{username}/"
ts_files = [f for f in listdir(path) if f.endswith(".ts")]
ts_files.sort()
# Create temp file listing all ts files
with open(f"{path}list.txt", "w") as f:
for ts_file in ts_files:
f.write(f"file '{ts_file}'\n")
# Concatenate all ts files into a single vod
file_name = datetime.now().strftime("%d-%m-%Y-%H-%M-%S")
vod_path = f"stream_data/hls/{username}/{file_name}.mp4"
vod_command = [
"ffmpeg",
"-f",
"concat",
"-safe",
"0",
"-i",
f"{path}list.txt",
"-c",
"copy",
vod_path
]
subprocess.run(vod_command)
# Remove ts files
for ts_file in ts_files:
remove(f"{path}{ts_file}")
return vod_path