themoeway / local-audio-yomichan

Anki add-on to run a local audio server for Yomichan.
MIT License
91 stars 11 forks source link

Feature request: support for Ogg/Opus and Ogg/Vorbis files #10

Closed tsweet64 closed 1 year ago

tsweet64 commented 1 year ago

EDIT: Fixed the problem (I didn't select the JSON option in yomichan). Will look into a more generalized way of implementing this and try to get a PR out soon

Hello, thanks for this amazing project!

Low priority suggestion, but it would be nice to be able to convert all the audio files to Opus in order to save space (I would normally manually go through the Anki media and do this occasionally, but having Opus files straight from the source would be nice).

I have attempted to make this addon support it (see attached diff), and everything appears to work from the addon end, with apparently normal output such as [{'name': 'JPod101', 'url': 'http://localhost:5050/jpod/とうほう - 東方.ogg'}, {'name': 'JPod101 Alt', 'url': 'http://localhost:5050/jpod_alternate/とうほう - 東方.ogg'}, {'name': 'NHK16 トーホー [0]', 'url': 'http://localhost:5050/nhk16/audio/20171011142454.aac'}]. The provided urls can be accessed and played normally in the web browser. However, Yomichan reports "No audio found"

I'm probably missing something super obvious... or perhaps it's a yomichan problem

diff --git a/plugin/server.py b/plugin/server.py
index 08f8017..5ffcb60 100644
--- a/plugin/server.py
+++ b/plugin/server.py
@@ -47,6 +47,9 @@ class LocalAudioHandler(http.server.SimpleHTTPRequestHandler):
         elif audio_file.endswith(".aac"):
             self.send_response(200)
             self.send_header("Content-type", "audio/aac")
+        elif audio_file.endswith((".ogg", ".opus")):
+            self.send_response(200)
+            self.send_header("Content-type", "audio/ogg")
         else:
             self.send_response(400)
             return
@@ -66,6 +69,9 @@ class LocalAudioHandler(http.server.SimpleHTTPRequestHandler):
         elif file_path.endswith(".aac"):
             self.send_response(200)
             self.send_header("Content-type", "audio/aac")
+        elif file_path.endswith((".ogg", ".opus")):
+            self.send_response(200)
+            self.send_header("Content-type", "audio/ogg")
         else:
             self.send_response(400)
             return
diff --git a/plugin/source/forvo.py b/plugin/source/forvo.py
index d016704..ceccb45 100644
--- a/plugin/source/forvo.py
+++ b/plugin/source/forvo.py
@@ -20,8 +20,8 @@ class ForvoAudioSource(AudioSource):
                 path = os.path.join(root, name)
                 relative_path = os.path.relpath(path, start)

-                if not name.endswith(".mp3"):
-                    print(f"(ForvoAudioSource) skipping non-mp3 file: {relative_path}")
+                if not name.endswith((".mp3", ".ogg", ".opus", ".aac")):
+                    print(f"(ForvoAudioSource) skipping non-audio file: {relative_path}")
                     continue

                 speaker = os.path.basename(root)
diff --git a/plugin/source/jpod.py b/plugin/source/jpod.py
index 360145c..0f13514 100644
--- a/plugin/source/jpod.py
+++ b/plugin/source/jpod.py
@@ -23,14 +23,13 @@ class JPodAudioSource(AudioSource):
             for name in files:
                 path = os.path.join(root, name)
                 relative_path = os.path.relpath(path, start)
-
-                if not name.endswith(".mp3"):
+                if not name.endswith((".mp3", ".ogg", ".opus", ".aac")):
                     print(
-                        f"({self.__class__.__name__}) skipping non-mp3 file: {relative_path}"
+                        f"({self.__class__.__name__}) skipping non-audio file: {relative_path}"
                     )
                     continue

-                parts = name.removesuffix(".mp3").split(" - ")
+                parts = os.path.splitext(name)[0].split(" - ")
                 if len(parts) != 2:
                     print(
                         f"({self.__class__.__name__}) skipping file with ' - ' sep: {relative_path}"