diff options
-rw-r--r-- | cgi/img.py | 100 |
1 files changed, 46 insertions, 54 deletions
@@ -88,74 +88,62 @@ def processImage(post, data, t, originalname, spoiler=False): # Do we need to thumbnail it? if not used_filetype['image']: # make thumbnail + logging.debug("Generating thumbnail") file_thumb_width, file_thumb_height = getThumbDimensions( width, height, maxsize) - - if used_filetype['ffmpeg_thumb'] == '1': - # use ffmpeg to make thumbnail - logging.debug("Generating thumbnail") - - if used_filetype['mime'][:5] == 'video': - # Create preview for video AND spoiler it if necessary - try: - subprocess.check_output([ + + try: + if used_filetype['ffmpeg_thumb'] == '1': + # use ffmpeg to make thumbnail + if used_filetype['mime'][:5] == 'video': + # Create preview for video AND spoiler it if necessary + call_wrap([ Settings.FFMPEG_PATH, '-strict', '-2', '-ss', '0', '-i', file_path, '-v', 'quiet', '-an', '-vframes', '1', '-f', 'mjpeg', '-vf', 'scale=%d:%d' % ( file_thumb_width, file_thumb_height), '-threads', '1', file_thumb_path]) if spoiler: - subprocess.check_output([Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", + call_wrap([Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", "1", "-background", "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height), "-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ((file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler", "-quality", str(Settings.THUMB_QUALITY), file_thumb_path]) - except subprocess.CalledProcessError, e: - os.remove(file_path) - logging.error("Thumbnail creation failure: " + e.output) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' - elif used_filetype['mime'][:5] == 'audio': - # we do an exception and use png for audio waveform thumbnails since they - # 1. are smaller 2. allow for transparency - file_thumb_name = file_thumb_name[:-3] + "png" - file_thumb_path = file_thumb_path[:-3] + "png" - file_mobile_path = file_mobile_path[:-3] + "png" - file_cat_path = file_cat_path[:-3] + "png" - - if int(board['thumb_px']) > 149: - file_thumb_width = board['thumb_px'] - file_thumb_height = float(int(board['thumb_px'])/2) - else: - file_thumb_width = 150 - file_thumb_height = 75 - - try: - subprocess.check_output([Settings.FFMPEG_PATH, '-t', '300', '-i', file_path, - '-filter_complex', 'showwavespic=s=%dx%d:split_channels=1' % ( + elif used_filetype['mime'][:5] == 'audio': + # we do an exception and use png for audio waveform thumbnails since they + # 1. are smaller 2. allow for transparency + file_thumb_name = file_thumb_name[:-3] + "png" + file_thumb_path = file_thumb_path[:-3] + "png" + file_mobile_path = file_mobile_path[:-3] + "png" + file_cat_path = file_cat_path[:-3] + "png" + + if int(board['thumb_px']) > 149: + file_thumb_width = board['thumb_px'] + file_thumb_height = float(int(board['thumb_px'])/2) + else: + file_thumb_width = 150 + file_thumb_height = 75 + + call_wrap([Settings.FFMPEG_PATH, '-t', '300', '-i', file_path, + '-filter_complex', 'showwavespic=s=%dx%d:split_channels=1' % ( int(file_thumb_width), int(file_thumb_height)), - '-frames:v', '1', '-threads', '1', file_thumb_path]) - except subprocess.CalledProcessError, e: - os.remove(file_path) - logging.error("Thumbnail creation failure: " + e.output) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' - else: - # use imagemagick to make thumbnail - args = [Settings.CONVERT_PATH, file_path, "-limit", "thread", "1", "-background", - "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height)] - if spoiler: - args += ["-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ( - (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler"] - args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path] - - # generate thumbnails - logging.debug("Generating thumbnail") - try: - subprocess.check_output(args) - except subprocess.CalledProcessError, e: - os.remove(file_path) - logging.error("Thumbnail creation failure: " + repr(e.output)) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' + '-frames:v', '1', '-threads', '1', file_thumb_path]) + else: + # use imagemagick to make thumbnail + args = [Settings.CONVERT_PATH, file_path, "-limit", "thread", "1", "-background", + "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height)] + if spoiler: + args += ["-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ( + (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler"] + args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path] + + # generate thumbnails + call_wrap(args) + except subprocess.CalledProcessError, e: + os.remove(file_path) + logging.error("Thumbnail creation failure: " + e.output) + raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' # check if thumbnail was truly created try: @@ -380,6 +368,10 @@ def ffprobe_f(filename): return json.loads(out) +def call_wrap(args): + subprocess.check_output(args, stderr=subprocess.STDOUT) + + def getThumbDimensions(width, height, maxsize): """ Calculate dimensions to use for a thumbnail with maximum width/height of |