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 56379a1
Show file tree
Hide file tree
Showing 6 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

Check warning on line 33 in src/sigal/audio.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/audio.py#L33

Added line #L33 was not covered by tests

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

Check warning on line 36 in src/sigal/audio.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/audio.py#L35-L36

Added lines #L35 - L36 were not covered by tests

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

Check warning on line 39 in src/sigal/audio.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/audio.py#L38-L39

Added lines #L38 - L39 were not covered by tests

return status.value

Check warning on line 41 in src/sigal/audio.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/audio.py#L41

Added line #L41 was not covered by tests
Binary file added src/sigal/audio_file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)

Check warning on line 357 in src/sigal/gallery.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/gallery.py#L356-L357

Added lines #L356 - L357 were not covered by tests

@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(

Check warning on line 364 in src/sigal/gallery.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/gallery.py#L362-L364

Added lines #L362 - L364 were not covered by tests
"Reading date from image metadata : %s", self.src_filename
)
return datetime.fromisoformat(self.meta["date"][0])
except Exception:
self.logger.debug(

Check warning on line 369 in src/sigal/gallery.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/gallery.py#L367-L369

Added lines #L367 - L369 were not covered by tests
"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()

Check warning on line 373 in src/sigal/gallery.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/gallery.py#L373

Added line #L373 was not covered by tests


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)

Check warning on line 435 in src/sigal/gallery.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/gallery.py#L435

Added line #L435 was not covered by tests

# 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

Check warning on line 998 in src/sigal/gallery.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/gallery.py#L998

Added line #L998 was not covered by tests

# 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

Check warning on line 126 in src/sigal/settings.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/settings.py#L126

Added line #L126 was not covered by tests
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()

Check warning on line 152 in src/sigal/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/utils.py#L152

Added line #L152 was not covered by tests


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}")

Check warning on line 159 in src/sigal/utils.py

View check run for this annotation

Codecov / codecov/patch

src/sigal/utils.py#L159

Added line #L159 was not covered by tests
return mime_type


def init_plugins(settings):
Expand Down

0 comments on commit 56379a1

Please sign in to comment.