diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 10a3b76..6732070 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -100,6 +100,14 @@ http { expires -1d; } + # The vods location + location ~ ^/stream/(.+)/vods/(.+\.mp4)$ { + alias /stream_data/$1/vods/$2; + + # The vods should not be cacheable + expires -1d; + } + location ~ ^/\?token=.*$ { proxy_pass http://frontend:5173; proxy_http_version 1.1; diff --git a/web_server/blueprints/streams.py b/web_server/blueprints/streams.py index 207522c..c252df2 100644 --- a/web_server/blueprints/streams.py +++ b/web_server/blueprints/streams.py @@ -237,6 +237,8 @@ def end_stream(): stream_length, 0)) + vod_id = db.get_last_insert_id() + # Set user as not streaming db.execute("""UPDATE users SET is_live = 0 @@ -245,6 +247,6 @@ def end_stream(): # Get username username = user_info["username"] - combine_ts_stream.delay(path_manager.get_stream_path(username), path_manager.get_vods_path(username)) + combine_ts_stream.delay(path_manager.get_stream_path(username), path_manager.get_vods_path(username), vod_id) return "Stream ended", 200 \ No newline at end of file diff --git a/web_server/celery_tasks/__init__.py b/web_server/celery_tasks/__init__.py index 1e6ae2c..12ad953 100644 --- a/web_server/celery_tasks/__init__.py +++ b/web_server/celery_tasks/__init__.py @@ -28,13 +28,12 @@ def update_thumbnail(stream_file, thumbnail_file, sleep_time) -> None: sleep(sleep_time) @shared_task -def combine_ts_stream(stream_path, vods_path): +def combine_ts_stream(stream_path, vods_path, vod_file_name): """ Combines all ts files into a single vod, and removes the ts files """ ts_files = [f for f in listdir(stream_path) if f.endswith(".ts")] ts_files.sort() - print(ts_files) # Create temp file listing all ts files with open(f"{stream_path}/list.txt", "w") as f: @@ -42,7 +41,6 @@ def combine_ts_stream(stream_path, vods_path): 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") + ".mp4" vod_command = [ "ffmpeg", @@ -54,7 +52,7 @@ def combine_ts_stream(stream_path, vods_path): f"{stream_path}/list.txt", "-c", "copy", - f"{vods_path}/{file_name}" + f"{vods_path}/{vod_file_name}.mp4" ] subprocess.run(vod_command) diff --git a/web_server/database/database.py b/web_server/database/database.py index 3248d69..0f31435 100644 --- a/web_server/database/database.py +++ b/web_server/database/database.py @@ -54,6 +54,10 @@ class Database: print(f"Database error: {e}") raise + def get_last_insert_id(self) -> int: + """Get the ID of the last inserted row.""" + return self.cursor.lastrowid if self.cursor else None + def convert_to_list_dict(self, result): """Convert query result to a list of dictionaries.""" if not result: