Blame thumb.py

Ivan Mahonin ae689c
Ivan Mahonin ae689c
import os
Ivan Mahonin ae689c
import subprocess
Ivan Mahonin ae689c
Ivan Mahonin ae689c
import config
Ivan Mahonin ae689c
Ivan Mahonin ae689c
Ivan Mahonin ae689c
env = dict(os.environ)
Ivan Mahonin ae689c
env['LANG']='C'
Ivan Mahonin ae689c
Ivan Mahonin ae689c
Ivan Mahonin ae689c
def printerr(r):
Ivan Mahonin ae689c
    print('process returned with error:', r.returncode)
Ivan Mahonin ae689c
    print('command:', r.args)
Ivan Mahonin ae689c
    print('output:', r.stdout)
Ivan Mahonin ae689c
    print('error:', r.stderr)
Ivan Mahonin ae689c
    return
Ivan Mahonin ae689c
Ivan Mahonin ae689c
Ivan Mahonin ae689c
def ffmpeg(src, dst):
Ivan Mahonin ae689c
    r = subprocess.run(
Ivan Mahonin ae689c
        [ config.ffprobe, '-v', 'error',  '-show_entries', 'format=duration:stream=codec_type', '-of', 'default=noprint_wrappers=1', src ],
Ivan Mahonin ae689c
        capture_output = True, text = True, env = env )
Ivan Mahonin ae689c
    if r.returncode: return #printerr(r)
Ivan Mahonin ae689c
    video = False; audio = False; duration = 0
Ivan Mahonin ae689c
    for l in r.stdout.split():
Ivan Mahonin ae689c
        f = l.split('=', 1)
Ivan Mahonin ae689c
        if len(f) != 2: continue
Ivan Mahonin ae689c
        if l == 'codec_type=video': video = True
Ivan Mahonin ae689c
        if l == 'codec_type=audio': audio = True
Ivan Mahonin ae689c
        if l.startswith('duration='):
Ivan Mahonin ae689c
            try: duration = float(l[9:])
Ivan Mahonin ae689c
            except ValueError: pass
Ivan Mahonin ae689c
    if not duration: return
Ivan Mahonin ae689c
    if video:
Ivan Mahonin ae689c
        filters = "select='gte(t,%f)',thumbnail,scale=%d:%d:force_original_aspect_ratio=decrease:force_divisible_by=2" % (duration/4, config.thumbw, config.thumbh)
Ivan Mahonin ae689c
        r = subprocess.run(
Ivan Mahonin ae689c
            [ config.ffmpeg, '-i', src, '-vf', filters, '-frames:v', '1', '-y', dst ],
Ivan Mahonin ae689c
            capture_output = True, text = True, env = env )
Ivan Mahonin ae689c
        #if r.returncode: printerr(r)
Ivan Mahonin ae689c
        if not r.returncode and os.path.isfile(dst): return dst
Ivan Mahonin ae689c
    if audio:
Ivan Mahonin ae689c
        filters = "showwavespic=s=%dx%d:scale=sqrt:colors=%06x" % (config.thumbw, config.thumbh, config.thumbcolor)
Ivan Mahonin ae689c
        r = subprocess.run(
Ivan Mahonin ae689c
            [ config.ffmpeg, '-i', src, '-filter_complex', filters, '-frames:v', '1', '-y', dst ],
Ivan Mahonin ae689c
            capture_output = True, text = True, env = env )
Ivan Mahonin ae689c
        #if r.returncode: printerr(r)
Ivan Mahonin ae689c
        if not r.returncode and os.path.isfile(dst): return dst
Ivan Mahonin ae689c
Ivan Mahonin ae689c
Ivan Mahonin ae689c
def magick(src, dst):
Ivan Mahonin ae689c
    r = subprocess.run(
Ivan Mahonin ae689c
        [ config.magick, src, '-resize',  '%dx%d>' % (config.thumbw, config.thumbh), dst ],
Ivan Mahonin ae689c
        capture_output = True, text = True, env = env )
Ivan Mahonin ae689c
    if r.returncode: return #printerr(r)
Ivan Mahonin ae689c
    if not r.returncode and os.path.isfile(dst): return dst
Ivan Mahonin ae689c
Ivan Mahonin ae689c
Ivan Mahonin ae689c
def make(src, dst):
Ivan Mahonin ae689c
    return ffmpeg(src, dst) or magick(src, dst)
Ivan Mahonin ae689c