the custom fileserver is back

This commit is contained in:
deflax 2024-01-18 00:36:20 +02:00
parent bd2ef69a7d
commit 3eaba322b2
3 changed files with 23 additions and 12 deletions

View file

@ -12,7 +12,6 @@ bot_token = os.environ.get('DISCORDBOT_TOKEN', 'token')
live_channel_id = os.environ.get('DISCORDBOT_LIVE_CHANNEL_ID', 0) live_channel_id = os.environ.get('DISCORDBOT_LIVE_CHANNEL_ID', 0)
live_channel_update = os.environ.get('DISCORDBOT_LIVE_CHANNEL_UPDATE', 5) live_channel_update = os.environ.get('DISCORDBOT_LIVE_CHANNEL_UPDATE', 5)
scheduler_hostname = os.environ.get('SCHEDULER_API_HOSTNAME', 'tv.example.com') scheduler_hostname = os.environ.get('SCHEDULER_API_HOSTNAME', 'tv.example.com')
vod_hostname = os.environ.get('VOD_HOSTNAME', 'vod.example.com')
# Discord API Intents # Discord API Intents
intents = discord.Intents.all() intents = discord.Intents.all()
@ -20,7 +19,7 @@ intents.members = True
intents.guilds = True intents.guilds = True
intents.messages = True intents.messages = True
intents.reactions = True intents.reactions = True
#intents.presences = True intents.presences = True
intents.message_content = True intents.message_content = True
# Discord client # Discord client
@ -135,10 +134,12 @@ async def query_database():
scheduler.remove_job('announce_live_channel') scheduler.remove_job('announce_live_channel')
if rechead != {}: if rechead != {}:
vod_filename = rechead['file'] video_filename = rechead['video']
thumb_filename = rechead['thumb']
rechead = {} rechead = {}
vod_url = f'https://{vod_hostname}/storage/{vod_filename}' vod_url = f'https://{scheduler_hostname}/video/{video_filename}'
offline_msg = f'Live stream is now offline. [Download VoD recording]({vod_url})' thumb_url = f'https://{scheduler_hostname}/thumb/{thumb_filename}'
offline_msg = f'Live stream is now offline. [Download VoD recording]({vod_url}) {thumb_url}'
else: else:
offline_msg = f'Live stream is now offline.' offline_msg = f'Live stream is now offline.'
logger_discord.info(offline_msg) logger_discord.info(offline_msg)

View file

@ -6,6 +6,7 @@ import json
import requests import requests
from datetime import datetime from datetime import datetime
from flask import Flask, render_template, jsonify, request from flask import Flask, render_template, jsonify, request
from flask.helpers import send_file
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
from core_client import Client from core_client import Client
from ffmpeg import FFmpeg, Progress from ffmpeg import FFmpeg, Progress
@ -163,18 +164,20 @@ def exec_stream(stream_id, stream_name, stream_prio, stream_hls_url):
# Execute recorder # Execute recorder
def exec_recorder(stream_id, stream_hls_url): def exec_recorder(stream_id, stream_hls_url):
global rechead global rechead
current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f") current_datetime = datetime.now().strftime("%Y%m%d_%H%M%S-%f")
video_file = current_datetime + ".mp4" video_file = current_datetime + ".mp4"
thumb_file = current_datetime + ".png" thumb_file = current_datetime + ".png"
if rechead != {}: if rechead != {}:
logger_job.error('Recorder is already started. Refusing to start another job.') logger_job.error('Recorder is already started. Refusing to start another job.')
else: else:
logger_job.warning(f'Recording {output_file} started.') logger_job.warning(f'Recording {video_file} started.')
rechead = { 'id': stream_id, rechead = { 'id': stream_id,
'video': video_file, 'video': video_file,
'thumb': thumb_file } 'thumb': thumb_file }
video_output = f'{rec_path}/live/{video_file}' video_output = f'{rec_path}/live/{video_file}'
thumb_output = f'{rec_path}/live/{thumb_file}' thumb_output = f'{rec_path}/live/{thumb_file}'
# Record a mp4 file
ffmpeg = ( ffmpeg = (
FFmpeg() FFmpeg()
.option("y") .option("y")
@ -182,23 +185,23 @@ def exec_recorder(stream_id, stream_hls_url):
.output(video_output, .output(video_output,
{"codec:v": "copy", "codec:a": "copy", "bsf:a": "aac_adtstoasc"}, {"codec:v": "copy", "codec:a": "copy", "bsf:a": "aac_adtstoasc"},
)) ))
@ffmpeg.on("progress") @ffmpeg.on("progress")
def on_progress(progress: Progress): def on_progress(progress: Progress):
print(progress) print(progress)
ffmpeg.execute() ffmpeg.execute()
ffmpeg = ( # Generate thumbnail image from the recorded mp4 file
ffmpeg_thumb = (
FFmpeg() FFmpeg()
.input(video_output) .input(video_output)
.output(thumb_output, .output(thumb_output,
{"vf": "thumbnail", "frames:v": "1"}) {"vf": "thumbnail", "frames:v": "1"})
) )
ffmpeg.execute() ffmpeg_thumb.execute()
logger_job.warning(f'Recording {video_file} finished.') logger_job.warning(f'Recording {video_file} finished.')
os.rename(f'{video_output}', f'{rec_path}/vod/{video_file}') os.rename(f'{video_output}', f'{rec_path}/vod/{video_file}')
os.rename(f'{thumb_output}', f'{rec_path}/thumb/{thumb_file}}') os.rename(f'{thumb_output}', f'{rec_path}/thumb/{thumb_file}')
rechead = {} rechead = {}
@ -285,5 +288,13 @@ def database_route():
global database global database
return jsonify(database) return jsonify(database)
@app.route("/video/<file_name>", methods=['GET'])
def video_route(file_name):
return send_file(f"./{rec_path}/video/{file_name}",mimetype='video/mp4')
@app.route("/thumb/<file_name>", methods=['GET'])
def thumb_route(file_name):
return send_file(f"./{rec_path}/thumb/{file_name}",mimetype='image/png')
def create_app(): def create_app():
return app return app

View file

@ -9,4 +9,3 @@ SCHEDULER_API_HOSTNAME=tv.example.com
DISCORDBOT_TOKEN=changeme DISCORDBOT_TOKEN=changeme
DISCORDBOT_LIVE_CHANNEL_ID=0 DISCORDBOT_LIVE_CHANNEL_ID=0
DISCORDBOT_LIVE_CHANNEL_UPDATE=5 DISCORDBOT_LIVE_CHANNEL_UPDATE=5
VOD_HOSTNAME=vod.example.com