This repository was archived by the owner on May 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Voice Announcement System (website part) #2
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a2fb309
Voice Announcement System
monster860 52b7fb2
Experimental branch for ffmpeg
monster860 458eac2
makes the thing work
monster860 29d2ac9
Uses topic instead of json files
monster860 8300d13
oops forgot to change it back
monster860 39da94e
remove printf debugging
monster860 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,6 @@ venv | |
|
|
||
| key_env.bat | ||
|
|
||
| .vscode | ||
| .vscode | ||
|
|
||
| voice_announce_tmp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .routes import blueprint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| from flask import Blueprint, Response, request, render_template | ||
| from werkzeug.utils import secure_filename | ||
| from io import open | ||
| from os import path, remove | ||
| from yogsite.extensions import flask_csrf_ext | ||
| from yogsite.config import cfg | ||
| from yogsite.util import topic_query | ||
| from subprocess import run,DEVNULL | ||
|
|
||
| blueprint = Blueprint("voice_announce", __name__) | ||
|
|
||
| type_extensions = { | ||
| "audio/aac": ".aac", | ||
| "audio/mpeg": ".mp3", | ||
| "audio/ogg": ".ogg", | ||
| "audio/opus": ".opus", | ||
| "audio/wav": ".wav", | ||
| "audio/webm": ".weba" | ||
| } | ||
|
|
||
| @blueprint.route("/voice_announce/<string:serversqlname>/<string:id>") | ||
| def page_voice_announce(serversqlname, id): | ||
| for s in cfg.get("servers").values(): | ||
| if s["sqlname"] == serversqlname: | ||
| server = s | ||
| res = topic_query(server, "", args = { | ||
| "voice_announce_query": id, | ||
| "key": server["comms_key"] | ||
| }) | ||
| if (not res) or (not int(res["exists"])): | ||
| return Response("Invalid voice announcement URL", status=404) | ||
|
|
||
| return render_template("voice_announce/voice_announce.html", enable_robot_voice = int(res["is_ai"])) | ||
|
|
||
| @blueprint.route("/voice_announce/<string:serversqlname>/<string:id>/upload", methods=["POST"]) | ||
| @flask_csrf_ext.exempt | ||
| def voice_announce_upload(serversqlname, id): | ||
| for s in cfg.get("servers").values(): | ||
| if s["sqlname"] == serversqlname: | ||
| server = s | ||
| id = secure_filename(id) | ||
| dir = cfg.get('voice_announce.directory') | ||
| res = topic_query(server, "", args = { | ||
| "voice_announce_query": id, | ||
| "key": server["comms_key"] | ||
| }) | ||
| if (not res) or (not int(res["exists"])): | ||
| return Response("Invalid voice announcement URL", status=404) | ||
|
|
||
| if request.content_length > 120000: | ||
| return Response("File too large", status=400) | ||
| ct = request.content_type | ||
| semicolon_loc = ct.find(";") | ||
| if semicolon_loc != -1: | ||
| ct = ct[:semicolon_loc] | ||
| if type_extensions[ct] == None: | ||
| return Response("Invalid file type", status=400) | ||
| filename_base = id | ||
| filename = filename_base + "_base" + type_extensions[ct] | ||
| ogg_filename = filename_base + "_converted.ogg" | ||
| with open(path.join(dir,filename), "wb") as file: | ||
| file.write(request.get_data()) | ||
|
|
||
| result = run(["ffmpeg", "-i", path.join(dir,filename), "-c:a", "libvorbis", "-y", path.join(dir,ogg_filename)], stdin=DEVNULL,stdout=DEVNULL,stderr=DEVNULL) | ||
| if result.returncode != 0: | ||
| remove(path.join(dir, filename)) | ||
| return Response("Conversion failed", status=500) | ||
| probe_result = run(["ffprobe", "-i", path.join(dir, ogg_filename), "-show_entries", "format=duration", "-v", "quiet", "-of", "csv=p=0"], capture_output=True) | ||
| if probe_result.returncode != 0: | ||
| remove(path.join(dir, filename)) | ||
| remove(path.join(dir, ogg_filename)) | ||
| return Response("ffprobe failed: " + probe_result.stderr.decode("utf-8"), status=500) | ||
| duration = float(probe_result.stdout) | ||
| if duration > 35: | ||
| remove(path.join(dir, filename)) | ||
| remove(path.join(dir, ogg_filename)) | ||
| return Response("Duration too long!", status=400) | ||
|
|
||
| topic_query(server, "", args = { | ||
| "voice_announce": id, | ||
| "ogg_file": ogg_filename, | ||
| "uploaded_file": filename, | ||
| "ip": request.remote_addr, | ||
| "duration": duration, | ||
| "key": server["comms_key"] | ||
| }) | ||
|
|
||
| return Response(None, status=204) | ||
|
|
||
| @blueprint.route("/voice_announce/<string:serversqlname>/<string:id>/cancel", methods=["GET","POST"]) | ||
| @flask_csrf_ext.exempt | ||
| def voice_announce_cancel(serversqlname, id): | ||
| for s in cfg.get("servers").values(): | ||
| if s["sqlname"] == serversqlname: | ||
| server = s | ||
|
|
||
| topic_query(server, "", args = { | ||
| "voice_announce_cancel": id, | ||
| "key": server["comms_key"] | ||
| }) | ||
|
|
||
| return Response(None, status=204) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome