Beispiel #1
0
 def imageEnhance(size, *args):
   outputOpt = args[-1]
   name = outputOpt['file'] if 'file' in outputOpt else None
   if not ('op' in outputOpt and outputOpt['op'] == 'output'):
     outputOpt = {}
   bench = outputOpt.get('diagnose', {}).get('bench', False)
   trace = outputOpt.get('trace', False) or bench
   process, nodes = genProcess(stepFile + list(args))
   return begin(imNode, nodes, trace, bench).bindFunc(process)(size, name=name)
Beispiel #2
0
 def imageEnhance(size, *args):
     outputOpt = args[-1]
     if not ('op' in outputOpt and outputOpt['op'] == 'output'):
         outputOpt = {}
     name = outputOpt['file'] if 'file' in outputOpt else None
     trace = outputOpt['trace'] if 'trace' in outputOpt else True
     process, nodes = genProcess(stepFile + list(args))
     return begin(imNode, nodes,
                  True if trace else -1).bindFunc(process)(size, name=name)
Beispiel #3
0
 def lock(duration):
     from gevent.event import Event
     flag = Event()
     node = begin(Node({}, 1, duration, 0))
     node.reset().trace(0)
     while duration > 0 and not context.stopFlag.is_set():
         duration -= 1
         flag.wait(1)
         flag.clear()
         node.trace()
     return duration
Beispiel #4
0
def prepare(video, steps):
  optEncode = steps[-1]
  encodec = optEncode['codec'] if 'codec' in optEncode else config.defaultEncodec  # pylint: disable=E1101
  optDecode = steps[0]
  decodec = optDecode['codec'] if 'codec' in optDecode else config.defaultDecodec  # pylint: disable=E1101
  optRange = steps[1]
  start = int(optRange['start']) if 'start' in optRange else 0
  outDir = config.outDir  # pylint: disable=E1101
  procSteps = stepVideo + list(steps[2:-1])
  process, nodes = genProcess(procSteps)
  root = begin(Node({'op': 'video', 'encodec': encodec}, 1, 2, 0), nodes, False)
  context.root = root
  width, height, frameRate, totalFrames = getVideoInfo(video)
  slomos = [*filter((lambda opt: opt['op'] == 'slomo'), procSteps)]
  if 'frameRate' in optEncode:
    frameRate = optEncode['frameRate']
  else:
    for opt in slomos:
      frameRate *= opt['sf']
  if 'width' in optDecode:
    width = optDecode['width']
  if 'height' in optDecode:
    height = optDecode['height']
  outWidth, outHeight = (width, height)
  for opt in filter((lambda opt: opt['op'] == 'SR' or opt['op'] == 'resize'), procSteps):
    if opt['op'] == 'SR':
      outWidth *= opt['scale']
      outHeight *= opt['scale']
    else: # resize
      outWidth = round(outWidth * opt['scaleW']) if 'scaleW' in opt else opt['width']
      outHeight = round(outHeight * opt['scaleH']) if 'scaleH' in opt else opt['height']
  if start < 0:
    start = 0
  if start and len(slomos): # should generate intermediate frames between start-1 and start
    start -= 1
    for opt in slomos:
      opt['opt'].firstTime = 0
  stop = None
  if 'stop' in optRange:
    stop = int(optRange['stop'])
    if stop <= start:
      stop = None
  root.total = (stop if stop else totalFrames) - start
  if not stop:
    stop = 0xffffffff
  root.multipleLoad(width * height * 3)
  initialETA(root)
  root.reset().trace(0)
  videoName = config.getPath()
  outputPath = outDir + '/' + videoName
  commandIn = [
    ffmpegPath,
    '-i', video,
    '-an',
    '-sn',
    '-f', 'rawvideo',
    '-s', '{}x{}'.format(width, height),
    '-pix_fmt', pix_fmt]
  if len(decodec):
    commandIn.extend(decodec.split(' '))
  commandIn.append('-')
  commandOut = [
    ffmpegPath,
    '-y',
    '-f', 'rawvideo',
    '-pix_fmt', pix_fmt,
    '-s', '{}x{}'.format(outWidth, outHeight),
    '-r', str(frameRate),
    '-i', '-',
    '-i', video,
    '-map', '0:v',
    '-map', '1?',
    '-map', '-1:v',
    '-c:1', 'copy',
    '-c:v:0']
  if start > 0:
    commandOut = commandOut[:12] + commandOut[22:]
  if len(encodec):
    commandOut.extend(encodec.split(' '))
  commandOut.append(outputPath)
  return commandIn, commandOut, outputPath, width, height, start, stop, root, process
Beispiel #5
0
def prepare(video, by, steps):
    optEncode = steps[-1]
    encodec = optEncode.get('codec', config.defaultEncodec)  # pylint: disable=E1101
    optDecode = steps[0]
    decodec = optDecode.get('codec', config.defaultDecodec)  # pylint: disable=E1101
    optRange = steps[1]
    start = int(optRange.get('start', 0))
    outDir = config.outDir  # pylint: disable=E1101
    procSteps = stepVideo + list(steps[2:-1])
    diagnose = optEncode.get('diagnose', {})
    bench = diagnose.get('bench', False)
    clear = diagnose.get('clear', False)
    process, nodes = genProcess(procSteps)
    traceDetail = config.progressDetail or bench  # pylint: disable=E1101
    root = begin(Node({'op': 'video'}, 1, 2, 0), nodes, traceDetail, bench,
                 clear)
    context.root = root
    slomos = [step for step in procSteps if step['op'] == 'slomo']
    refs, ahead = 0, 0
    if start < 0:
        start = 0
    for i in range(
            len(procSteps) - 1, -1, -1
    ):  # gather some reference frames before start point for video models
        step = procSteps[i]
        if step['op'] == 'slomo':
            step['opt'].outStart = -refs % step['sf'] if refs else 1
            step['opt'].outEnd = -(-ahead % step['sf'])
            refs = max(ceil(refs / step['sf']), lookback[step['op']])
            ahead = max(ceil(ahead / step['sf']), lookahead[step['op']])
        elif step['op'] in padOp:
            step['opt'].start = 0
            step['opt'].end = 0
            refs += lookback[step['op']]
            ahead += lookahead[step['op']]
    if start < refs:  # no enough reference frames
        arefs = start
        for step in procSteps:
            if arefs >= refs:
                break
            if step['op'] == 'slomo':
                refs = refs * step['sf'] - step['opt'].outStart
                step['opt'].outStart = 0
                arefs = arefs * step['sf']
            elif step['op'] in padOp:
                step['opt'].start = min(refs - arefs, lookback[step['op']])
                refs -= step['opt'].start
        start = 0
    else:
        start -= refs
    stop = int(optRange.get('stop', -1))
    if stop <= start:
        stop = -1
    root.total = -1 if stop < 0 else stop - start
    outputPath = fixExt(
        splitext(optEncode.get('file', '') or outDir + '/' + config.getPath()))
    dataPath = suffix(outputPath, '-a')
    commandIn = [
        ffmpegPath, '-hide_banner', '-f', 'lavfi', '-i', video, '-vn', '-c',
        'copy', '-y', dataPath, '-map', '0:v', '-f', 'rawvideo', '-pix_fmt',
        pix_fmt
    ]
    if by != 'cmd':
        commandIn = clipList(commandIn, 2, 4)
    if len(decodec):
        commandIn.extend(decodec.split(' '))
    commandIn.append('-')
    metadata = [
        '-metadata', 'service_provider="MoePhoto {}"'.format(config.version)
    ]  # pylint: disable=E1101
    commandVideo = [
        ffmpegPath, '-hide_banner', '-y', '-f', 'rawvideo', '-pix_fmt',
        pix_fmt, '-s', '', '-r', '', '-thread_queue_size', '64', '-i', '-',
        '-i', dataPath, '-map', '0:v', '-map', '1?', '-map', '-1:v', '-c:1',
        'copy', *metadata, '-c:v:0'
    ] + encodec.split(' ') + ['']
    commandOut = None
    if by:
        commandVideo[-1] = suffix(outputPath, '-v')
        commandOut = [
            ffmpegPath, '-hide_banner', '-y', '-i', commandVideo[-1], '-i',
            dataPath, '-map', '0:v', '-map', '1?', '-c:0', 'copy', '-c:1',
            'copy', *metadata, outputPath
        ]
    else:
        commandVideo[16] = video
    frameRate = optEncode.get('frameRate', 0)
    width = optDecode.get('width', 0)
    height = optDecode.get('height', 0)
    sizes = [step for step in procSteps if step['op'] in resizeOp]
    return outputPath, process, start, stop, ahead, root, commandIn, commandVideo, commandOut, slomos, sizes, width, height, frameRate
Beispiel #6
0
def prepare(video, by, steps):
    optEncode = steps[-1]
    encodec = optEncode.get('codec', config.defaultEncodec)  # pylint: disable=E1101
    optDecode = steps[0]
    decodec = optDecode.get('codec', config.defaultDecodec)  # pylint: disable=E1101
    optRange = steps[1]
    start = int(optRange.get('start', 0))
    outDir = config.outDir  # pylint: disable=E1101
    procSteps = stepVideo + list(steps[2:-1])
    diagnose = optEncode.get('diagnose', {})
    bench = diagnose.get('bench', False)
    clear = diagnose.get('clear', False)
    process, nodes = genProcess(procSteps)
    traceDetail = config.progressDetail or bench  # pylint: disable=E1101
    root = begin(Node({'op': 'video'}, 1, 2, 0), nodes, traceDetail, bench,
                 clear)
    context.root = root
    slomos = [*filter((lambda opt: opt['op'] == 'slomo'), procSteps)]
    if start < 0:
        start = 0
    if start and len(
            slomos
    ):  # should generate intermediate frames between start-1 and start
        start -= 1
        for opt in slomos:
            opt['opt'].firstTime = 0
    stop = int(optRange.get('stop', -1))
    if stop <= start:
        stop = -1
    root.total = -1 if stop < 0 else stop - start
    outputPath = fixExt(
        splitext(optEncode.get('file', '') or outDir + '/' + config.getPath()))
    dataPath = suffix(outputPath, '-a')
    commandIn = [
        ffmpegPath, '-hide_banner', '-f', 'lavfi', '-i', video, '-vn', '-c',
        'copy', '-y', dataPath, '-map', '0:v', '-f', 'rawvideo', '-pix_fmt',
        pix_fmt
    ]
    if by != 'cmd':
        commandIn = clipList(commandIn, 2, 4)
    if len(decodec):
        commandIn.extend(decodec.split(' '))
    commandIn.append('-')
    metadata = [
        '-metadata', 'service_provider="MoePhoto {}"'.format(config.version)
    ]  # pylint: disable=E1101
    commandVideo = [
        ffmpegPath, '-hide_banner', '-y', '-f', 'rawvideo', '-pix_fmt',
        pix_fmt, '-s', '', '-r', '', '-i', '-', '-i', dataPath, '-map', '0:v',
        '-map', '1?', '-map', '-1:v', '-c:1', 'copy', *metadata, '-c:v:0'
    ] + encodec.split(' ') + ['']
    commandOut = None
    if by:
        commandVideo[-1] = suffix(outputPath, '-v')
        commandOut = [
            ffmpegPath, '-hide_banner', '-y', '-i', commandVideo[-1], '-i',
            dataPath, '-map', '0:v', '-map', '1?', '-c:0', 'copy', '-c:1',
            'copy', *metadata, outputPath
        ]
    else:
        commandVideo[14] = video
    frameRate = optEncode.get('frameRate', 0)
    width = optDecode.get('width', 0)
    height = optDecode.get('height', 0)
    sizes = filter((lambda opt: opt['op'] == 'SR' or opt['op'] == 'resize'),
                   procSteps)
    return outputPath, process, start, stop, root, commandIn, commandVideo, commandOut, slomos, sizes, width, height, frameRate
Beispiel #7
0
 def imageEnhance(size, *args, name=None, trace=True):
     process, nodes = genProcess(stepFile + list(args))
     return begin(imNode, nodes,
                  True if trace else -1).bindFunc(process)(size, name=name)