Move image generating code to utils

This commit is contained in:
Ales (Shagi) Zabala Alava 2020-11-03 19:47:38 +01:00
parent 22f89720f6
commit 373b12bacd
2 changed files with 32 additions and 23 deletions

View File

@ -1,17 +1,14 @@
import json import json
import subprocess
import asstosrt import asstosrt
import srt import srt
import webvtt import webvtt
from .utils import needs_change from .utils import needs_change, generate_cover, generate_thumbnail
from . import logger from . import logger
class Song: class Song:
THUMBNAIL_GEOMETRY = '200x200'
def __init__(self, path, root): def __init__(self, path, root):
self.name = path.name self.name = path.name
self.metadata = None self.metadata = None
@ -88,29 +85,12 @@ class Song:
cover = self.path / "cover.jpg" cover = self.path / "cover.jpg"
if needs_change(cover, (self.video,)): if needs_change(cover, (self.video,)):
self.cover = cover self.cover = cover
command = [ generate_cover(self.video, self.cover)
'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)
thumbnail = self.path / "thumb.jpg" thumbnail = self.path / "thumb.jpg"
if needs_change(thumbnail, (self.cover,)): if needs_change(thumbnail, (self.cover,)):
self.thumbnail = thumbnail self.thumbnail = thumbnail
subprocess.check_call([ generate_thumbnail(self.cover, self.thumbnail)
'convert',
str(self.cover.absolute()),
'-resize', self.THUMBNAIL_GEOMETRY,
str(self.thumbnail.absolute()),
])
@property @property
def publish(self): def publish(self):

View File

@ -1,3 +1,6 @@
import subprocess
def needs_change(destination, dependencies): def needs_change(destination, dependencies):
last_dependency_change = 0 last_dependency_change = 0
for dependency in dependencies: for dependency in dependencies:
@ -12,3 +15,29 @@ def needs_change(destination, dependencies):
return True return True
return destination.lstat().st_mtime < last_dependency_change 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)