Skip to content

Commit

Permalink
Adding support for audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed May 6, 2024
1 parent cf15fcc commit d370cf6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ alphabetical order):
- Keith Feldman
- Keith Johnson
- Kevin Murray
- Lucas Cimon
- Lucas Cimon (@Lucas-C)
- Lukas Vacek
- Luke Cyca (@lukecyca)
- Mate Lakat
Expand Down
41 changes: 41 additions & 0 deletions src/sigal/audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2013 - Christophe-Marie Duquesne
# Copyright (c) 2013-2023 - Simon Conseil
# Copyright (c) 2021 - Keith Feldman

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

from os.path import dirname, join

from . import utils
from .utils import is_valid_html5_audio

AUDIO_THUMB_FILE = join(dirname(__file__), "audio_file.png")


def process_audio(media):
"""Process an audio file: create thumbnail."""
settings = media.settings

with utils.raise_if_debug() as status:
utils.copy(media.src_path, media.dst_path, symlink=settings["orig_link"])

if settings["make_thumbs"]:
utils.copy(AUDIO_THUMB_FILE, media.thumb_path)

return status.value
32 changes: 32 additions & 0 deletions src/sigal/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@
copy,
get_mime,
get_mod_date,
is_valid_html5_audio,
is_valid_html5_video,
read_markdown,
should_reprocess_album,
url_from_path,
)
from .audio import process_audio
from .video import process_video
from .writer import AlbumListPageWriter, AlbumPageWriter

Expand Down Expand Up @@ -345,6 +347,32 @@ def date(self):
return self._get_file_date()


class Audio(Media):
"""Gather all informations on an audio file."""

type = "audio"

def __init__(self, filename, path, settings):
super().__init__(filename, path, settings)
self.mime = get_mime(self.src_ext)

@cached_property
def date(self):
"""The date from the Date metadata if available, or from the file date."""
if "date" in self.meta:
try:
self.logger.debug(
"Reading date from image metadata : %s", self.src_filename
)
return datetime.fromisoformat(self.meta["date"][0])
except Exception:
self.logger.debug(
"Reading date from image metadata failed : %s", self.src_filename
)
# If no date is found in the metadata, return the file date.
return self._get_file_date()


class Album:
"""Gather all informations on an album.
Expand Down Expand Up @@ -403,6 +431,8 @@ def __init__(self, path, settings, dirnames, filenames, gallery):
media = Image(f, self.path, settings)
elif ext.lower() in settings["video_extensions"]:
media = Video(f, self.path, settings)
elif ext.lower() in settings["audio_extensions"]:
media = Audio(f, self.path, settings)

# Allow modification of the media, including overriding the class
# type for the media.
Expand Down Expand Up @@ -964,6 +994,8 @@ def process_file(media):
processor = process_image
elif media.type == "video":
processor = process_video
elif media.type == "audio":
processor = process_audio

# Allow overriding of the processor
result = signals.process_file.send(media, processor=processor)
Expand Down
3 changes: 3 additions & 0 deletions src/sigal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"video_format": "webm",
"video_always_convert": False,
"video_size": (480, 360),
"audio_extensions": [".m4a", ".mp3", ".ogg", ".wav"],
"watermark": "",
"webm_options": ["-crf", "10", "-b:v", "1.6M", "-qmin", "4", "-qmax", "63"],
"webm_options_second_pass": None,
Expand Down Expand Up @@ -121,6 +122,8 @@ def get_thumb(settings, filename):

if ext.lower() in settings["video_extensions"]:
ext = ".jpg"
if ext.lower() in settings["audio_extensions"]:
ext = ".png" # extension of sigal.audio.AUDIO_THUMB_FILE
return join(
path,
settings["thumb_dir"],
Expand Down
11 changes: 10 additions & 1 deletion src/sigal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
logger = logging.getLogger(__name__)
MD = None
VIDEO_MIMES = {".mp4": "video/mp4", ".webm": "video/webm", ".ogv": "video/ogg"}
AUDIO_MIMES = {".m4a": "audio/m4a", ".mp3": "audio/mpeg", ".ogg": "audio/ogg", ".wav": "audio/x-wav"}


class Devnull:
Expand Down Expand Up @@ -146,9 +147,17 @@ def is_valid_html5_video(ext):
return ext in VIDEO_MIMES.keys()


def is_valid_html5_audio(ext):
"""Checks if ext is a supported HTML5 video."""
return ext in AUDIO_MIMES.keys()


def get_mime(ext):
"""Returns mime type for extension."""
return VIDEO_MIMES[ext]
mime_type = VIDEO_MIMES.get(ext) or AUDIO_MIMES.get(ext)
if not mime_type:
raise RuntimeError(f"Could not figure mime type, unknown file extension: {ext}")
return mime_type


def init_plugins(settings):
Expand Down

0 comments on commit d370cf6

Please sign in to comment.