Move image generating code to utils
This commit is contained in:
		
							parent
							
								
									22f89720f6
								
							
						
					
					
						commit
						373b12bacd
					
				| 
						 | 
				
			
			@ -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):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue