From 373b12bacd762018e29064e614a94ca01229d068 Mon Sep 17 00:00:00 2001 From: shagi Date: Tue, 3 Nov 2020 19:47:38 +0100 Subject: [PATCH] Move image generating code to utils --- negromate/songs/loader.py | 26 +++----------------------- negromate/songs/utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/negromate/songs/loader.py b/negromate/songs/loader.py index fccf4a8..b7c1ca8 100644 --- a/negromate/songs/loader.py +++ b/negromate/songs/loader.py @@ -1,17 +1,14 @@ import json -import subprocess import asstosrt import srt import webvtt -from .utils import needs_change +from .utils import needs_change, generate_cover, generate_thumbnail from . import logger class Song: - THUMBNAIL_GEOMETRY = '200x200' - def __init__(self, path, root): self.name = path.name self.metadata = None @@ -88,29 +85,12 @@ class Song: cover = self.path / "cover.jpg" if needs_change(cover, (self.video,)): self.cover = cover - command = [ - 'ffmpeg', - '-loglevel', 'quiet', - '-i', str(self.video.absolute()), - '-vcodec', 'mjpeg', - '-vframes', '1', - '-an', - '-f', 'rawvideo', - '-ss', '2', - '-y', - str(self.cover.absolute()), - ] - subprocess.check_call(command) + generate_cover(self.video, self.cover) thumbnail = self.path / "thumb.jpg" if needs_change(thumbnail, (self.cover,)): self.thumbnail = thumbnail - subprocess.check_call([ - 'convert', - str(self.cover.absolute()), - '-resize', self.THUMBNAIL_GEOMETRY, - str(self.thumbnail.absolute()), - ]) + generate_thumbnail(self.cover, self.thumbnail) @property def publish(self): diff --git a/negromate/songs/utils.py b/negromate/songs/utils.py index 5491ed0..2bcd76e 100644 --- a/negromate/songs/utils.py +++ b/negromate/songs/utils.py @@ -1,3 +1,6 @@ +import subprocess + + def needs_change(destination, dependencies): last_dependency_change = 0 for dependency in dependencies: @@ -12,3 +15,29 @@ def needs_change(destination, dependencies): return True return destination.lstat().st_mtime < last_dependency_change + + +def generate_cover(video, cover, second=2): + command = [ + 'ffmpeg', + '-loglevel', 'quiet', + '-i', str(video.absolute()), + '-vcodec', 'mjpeg', + '-vframes', '1', + '-an', + '-f', 'rawvideo', + '-ss', str(second), + '-y', + str(cover.absolute()), + ] + subprocess.check_call(command) + + +def generate_thumbnail(cover, thumbnail, geometry="200x200"): + command = [ + 'convert', + str(cover.absolute()), + '-resize', geometry, + str(thumbnail.absolute()), + ] + subprocess.check_call(command)