def ui_prepare_watermark(entparams=[], appsetup={}): print( f"ui_addaudiotovideo:\n\tentparams={entparams}\n\tappsetup={appsetup}") imgfile = confirm_file(entparams[0], ftype='image', appsetup=appsetup, isnew=0) vidfile = confirm_file(entparams[1], ftype='video', appsetup=appsetup, isnew=0) outfile = confirm_file(entparams[3], ftype='video', fback='temp/' + str(time.time()) + vidfile.name, isnew=1, appsetup=appsetup) pixels = entparams[2].split(',') wpixel = pyback.forceint(pixels[0], 100) hpixel = 100 if len(pixels) < 2 else pyback.forceint(pixels[1], 100) cmdstr = f"ffmpeg -i {vidfile} -i {imgfile} -filter_complex \"overlay={wpixel}:{hpixel}\" {outfile} -loglevel error" print(f"cmdstr: {cmdstr}") try: os.system(cmdstr) except: return ['ERROR', 'The command could not be executed!']
def ui_addaudiotovideo(entparams=[], appsetup={}): print( f"ui_addaudiotovideo:\n\tentparams={entparams}\n\tappsetup={appsetup}") vidfile = confirm_file(entparams[0], ftype='video', appsetup=appsetup, isnew=0) audfile = confirm_file(entparams[1], ftype='audio', appsetup=appsetup, isnew=0) vistart = pyback.forceint(entparams[2], 0) austart = pyback.forceint(entparams[3], 0) alength = pyback.forceint(entparams[4], 0) outfile = confirm_file(entparams[5], ftype='video', fback=vidfile.name, isnew=1, appsetup=appsetup) if isinstance(vidfile, str) or not vidfile.exists() or isinstance( audfile, str) or not audfile.exists(): localmessage( mtype='error', title='Input files are missing', message= f"Input Audio ({audfile}) or Video ({vidfile}) is missing, kindly check path" ) return -1 if austart != 0: newaudfile = Path(audfile.parent) / (vidfile.stem + "__" + str(austart) + audfile.suffix) cmdstr = f"ffmpeg -i \"{audfile}\" -ss {austart} -to {austart+alength} -c copy -y \"{newaudfile}\" -loglevel error" print(f"executing command: {cmdstr}") try: os.system(cmdstr) except: return ['ERROR', 'The command could not be executed!'] audfile = newaudfile cmdstr = f"ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 \"{vidfile}\" -loglevel error" print(f"executing command: {cmdstr}") audcod = (subprocess.run( cmdstr, capture_output=True)).stdout.decode('unicode_escape') if audcod == '': cmdstr = f"ffmpeg -i \"{vidfile}\" -itsoffset {vistart} -t {vistart+alength} -i \"{audfile}\" -map 0:v:0 -map 1:a:0 -async 1 -y \"{outfile}\" -loglevel error" else: cmdstr = f"ffmpeg -i \"{vidfile}\" -itsoffset {vistart} -t {vistart+alength} -i \"{audfile}\" -filter_complex amix -map 0:v -map 0:a -map 1:a -async 1 -y \"{outfile}\" -loglevel error" print(f"executing command: {cmdstr}") try: os.system(cmdstr) except: return ['ERROR', 'The command could not be executed!'] if outfile.parent == 'temp': vidfile.rename( Path(vidfile.parent) / (vidfile.stem + int(time.time()) + vidfile.suffix)) outfile.rename(vidfile) return []
def ui_moverushstaging(entparams=[], appsetup={}): inpfile = Path(appsetup['project']['name']) / 'rushes' outfile = confirm_file(entparams[0], ftype='input', fback='video/' + str(time.time()) + "__blank__", isnew=1, appsetup=appsetup) frames = entparams[1].split(',') fframe = pyback.forceint(frames[0], 1) lframe = 999999 if len(frames) < 2 else pyback.forceint(frames[1], 999999) if lframe < fframe: lframe = 999999 pyback.png_overwrites(csframe=fframe, clframe=lframe, imgsrc=inpfile, imgdst=outfile, owrite=0, action=['append', 'copy']) return 1
def _image_resize(ifile='', ofile='', nsize=100): if ifile.suffix in appsetup['pictures']: image = Image.open(ifile) cwide, chigh = image.size if pyback.forceint(nsize, default=-1) != -1: nwide, nhigh = int(cwide * int(nsize) / 100), int(chigh * int(nsize) / 100) else: nwide, nhigh = pyback.getscreensize(nsize, cwide, chigh) nimage = image.resize((nwide, nhigh), Image.ANTIALIAS) nimage.save(ofile) elif ifile.suffix in appsetup['movies']: cmdstr = f"ffmpeg -i \"{ifile}\" -filter:v \"crop=960:1080:480:0\" \"{ofile}\"" os.system(cmdstr) return 0
def ui_image_manipulation_basic(entparams=[], appsetup={}): permissibles = ['contrast', 'color', 'brightness', 'sharpness', 'invert'] if entparams[2] not in permissibles: localmessage( mtype='error', title='Incorrect action type', message= f"The feature to be modified should be one of: {', '.join(permissibles)}" ) return -1 newVal = pyback.forceint(entparams[3], 1) if entparams[3] != 'range' else 'range' outfolder = 1 if newVal == 'range' else 0 inpfile, outfile = create_output_path(param0=entparams[0], param1=entparams[1], appsetup=appsetup, outfolder=outfolder) if inpfile.is_dir() and newVal == 'range': localmessage( mtype='error', title='Range is invalid', message= f"Setting range over already set of images will create too many media. Stopping" ) return -1 if inpfile.is_dir(): for ifile in inpfile.iterdir(): ofile = outfile / ifile.name enhance_image_basic(ifile=ifile, ofile=ofile, param=entparams[2], nval=newVal) elif newVal == 'range': for frid in range(1, 101): newcVal = (frid - 1) * 0.1 ofile = outfile / ("frame__" + "%06d" % (frid) + ".png") enhance_image_basic(ifile=inpfile, ofile=ofile, param=entparams[2], nval=newcVal) else: enhance_image_basic(ifile=inpfile, ofile=outfile, param=entparams[2], nval=newVal) return 1
def image_manual_bgremoval(entparams=[], appsetup={}): ipath = Path(appsetup['project']['name']) / 'rushes' opath = confirm_file(entparams[1], ftype='video', fback='', appsetup=appsetup, isnew=1) if opath.exists(): UREP = localmessage( mtype='ask', title='Path already exists', message= f"There is already a filepath with name {opath}. Overwrite its content?" ) if UREP == 'cancel': return -1 else: shutil.rmtree(opath) opath.mkdir() else: opath.mkdir() basefile = ipath / ("frame__" + "%06d" % (pyback.forceint(entparams[0], 2)) + ".png") trans = colorcode[ entparams[2]] if entparams[2] in colorcode else colorcode['black'] keeps = [255, 255, 255] alpha = 255 bimg = Image.open(basefile) bimg = bimg.convert("RGBA") bstate = bimg.getdata() def pixwise_removal(ifile=Path(), ofile=Path(), bstate=[], trans=[0, 0, 0], keeps=[255, 255, 255], alpha=255): ximg = Image.open(str(ifile)) ximg = ximg.convert("RGBA") xstate = ximg.getdata() pcount = ximg.size[0] * ximg.size[1] newData = [] for ix in range(0, pcount): bitem, xitem = bstate[ix], xstate[ix] if (xitem[0] == trans[0] and xitem[1] == trans[1] and xitem[2] == trans[1]): toappend = (bitem[0], bitem[1], bitem[2], alpha) else: toappend = (xitem[0], xitem[1], xitem[2], 0) newData.append(toappend) ximg.putdata(newData) ximg.save(str(ofile), "PNG") return 1 for file in ipath.iterdir(): if file.name[: 7] != 'frame__' or file.suffix != '.png' or not file.is_file( ): continue pixwise_removal(ifile=file, ofile=opath / file.name, bstate=bstate, trans=trans, keeps=keeps, alpha=alpha) return 1
def ui_p3dmodel_creation(entparams=[], appsetup={}): def get_filemapping(dirp=None, start=1, last=999999, tcount=1): fcount = len(list(dirp.glob('frame__??????.png'))) if fcount > last: fcount = last if start > 1: fcount = fcount - start + 1 if (dirp / 'frame__000000.png').exists(): fcount = fcount - 1 mapls = pyback.fixinitemlist(lfrom=fcount - 1, linto=tcount) retval = [x + start for x in mapls] return retval def filemapcopy(cmap=[], imgsrc=None, imgdst=None): if not imgdst.is_dir(): imgdst.mkdir(parents=True, exist_ok=True) for ix, frid in enumerate(cmap): print(f"copying file {frid} as {ix}") oldimg = imgsrc / ("frame__" + "%06d" % (frid) + ".png") newimg = imgdst / ("frame__" + "%06d" % (ix) + ".png") if newimg.exists(): newimg.unlink() shutil.copy(oldimg, newimg) print("PNG COPY COMPLETED") if '/' not in entparams[1]: entparams[1] = 'media/' + entparams[1] inpfile, outfile, curfps = create_output_path(param0=entparams[0], param1=entparams[1], appsetup=appsetup, outfolder=1, getfps=1) if isinstance(curfps, str): curfps = appsetup['project']['fps'] thinned = pyback.forceint(entparams[4], 0) print("create_output_path returned:", inpfile, outfile, curfps, isinstance(curfps, str)) if inpfile == None or outfile == None or inpfile == outfile: localmessage(mtype='error', title='Input & Output path error', message="Input or Output path are incorrect or same") return -1 csframe = pyback.forceint(entparams[2].split(",")[0], 1) clframe = pyback.forceint( entparams[2].split(",")[1], 999999) if len(entparams[2].split(",")) > 1 else 999999 if clframe < csframe: clframe = 999999 curfps = curfps if pyback.forceint( entparams[3], curfps) < 1 else pyback.forceint(entparams[3], curfps) if thinned == 0: pyback.png_overwrites(csframe=csframe, tdframe=csframe - 1, clframe=clframe, imgsrc=inpfile, imgdst=outfile, owrite=1, action=['copy']) else: copymap = get_filemapping(dirp=inpfile, start=csframe, last=clframe, tcount=thinned) filemapcopy(cmap=copymap, imgsrc=inpfile, imgdst=outfile) print(f"copymap: {copymap}") pyback.create_media_p3dmodel(ifile=outfile, owrite=1, appsetup=appsetup, fps=curfps) return 1
def ui_text_image_creation(entparams=[], appsetup={}): print( f"ui_text_image_creation:\n\tentparams={entparams}\n\tappsetup={appsetup}" ) is_single = 1 if entparams[4].lower() in yes_synos: imgfile = confirm_file(entparams[0], ftype='folder', appsetup=appsetup, isnew=1) imgfile.mkdir() is_single = 0 else: imgfile = confirm_file(entparams[0], ftype='image', appsetup=appsetup, isnew=1) print(f"FINAL FILE NAME: {imgfile}") if imgfile.exists(): localmessage( mtype='error', title='Such file already exist', message= f"New file ({imgfile}) exists, kindly delete it or rename this file" ) return -1 textstr = entparams[1] ffont = check_system_fonts(fontlike=entparams[2], fontsize=pyback.forceint(entparams[3], 16)) if imgfile == '' or textstr == '': localmessage( mtype='error', title='Missing usable parameters', message= f"New file ({imgfile}) or Text ({textstr}) could not be used") return -1 paramadd = parse_additionals(strtext=entparams[5]) print(f"paramadd: {paramadd}") imgsize = def_imgsize print("ffont", ffont) if is_single == 1: create_image_fortext(file=imgfile, imgsize=imgsize, text=textstr, font=ffont, paramadd=paramadd) else: maxsize = create_image_fortext(file=None, imgsize=imgsize, text=textstr, font=ffont, paramadd=paramadd) for ix in range(len(textstr) + 1): thistext = textstr[:ix] framefile = imgfile / ("frame__" + "%06d" % (ix) + ".png") create_image_fortext(file=framefile, imgsize=maxsize, text=thistext, font=ffont, paramadd=paramadd, nocrop=1) if not imgfile.exists(): localmessage( mtype='error', title='File could not be created', message= f"New file ({imgfile}) or Text ({textstr}) could not be used") return 1