0 Members and 1 Guest are viewing this topic.
''' Video converter - To project-specific format In: VIDEO FILE Out: A bunch of data files that the project can compile'''from PIL import Image,PngImagePlugin,ImageTk,ImageOpsfrom ffmpy import FFmpegimport sys,os,subprocessimport Tkinter as tk# ---------------------------------------------------------------------------class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.master.title("* Ohhhh yesss!") self.master.geometry('200x200') self.master.minsize(200,200) self.pack() self.img = ImageTk.BitmapImage(Image.new('1',(96,64),0),foreground='black') self.canvas = tk.Canvas(self.master,width=96,height=64) self.canvas.place(x=10,y=10,width=100,height=100) self.canvas.configure(bg='white',width=96,height=64,state=tk.NORMAL) self.imgobj = self.canvas.create_image(1,1,image=self.img,anchor=tk.NW,state=tk.NORMAL) def updateframe(self,bytearray): self.img = ImageTk.BitmapImage(Image.frombytes('1',(96,64),bytearray),foreground='black') self.canvas.itemconfig(self.imgobj,image=self.img) self.update_idletasks() self.update()print "Starting Program"root = tk.Tk()app = Application(root)app.update_idletasks()app.update()# ---------------------------------------------------------------------------np = os.path.normpathcwd = os.getcwd()af_called = 0buffers = 0max_frames = 23cur_frame = 0framebuf = []compressed_arrays = []total_len = 0#-----------------------------------------------------------------------------def readFile(file): a = [] f = open(file,'rb') b = f.read(1) while b!=b'': a.append(ord(b)) b = f.read(1) f.close() return a def writeFile(file,a): f = open(file,'wb+') f.write(bytearray(a)) f.close() def addframe(framedata=None): global max_frames,cur_frame,framebuf,af_called,compressed_arrays,buffers,total_len af_called += 1 if framedata: framedata = ''.join([chr(~ord(c)&0xFF) for c in framedata]) app.updateframe(framedata) framebuf.extend(framedata) cur_frame += 1 if cur_frame >= max_frames: framedata = None # If framedata was None either on init or set during running, flush buffer. if not framedata and framebuf: tfn = np(cwd+'/bin/tempimg') tfnc = np(cwd+'/bin/tempimgc') writeFile(tfn,framebuf) subprocess.call([np(cwd+'/tools/apack.exe'),tfn,tfnc]) outarray = readFile(tfnc) compressed_arrays.append([buffers,len(outarray),outarray]) total_len += len(outarray) buffers += 1 print "Buffer "+str(buffers)+" flushed at call number "+str(af_called) framebuf = [] cur_frame = 0 def dumptofile(fn,arrobj): filename = np(cwd+'/dat/'+fn) with open(filename,'w') as f: curchar = 0 s = '' for b in arrobj[2]: if curchar == 0: s = '.db ' s = s+'$%02X' % b if curchar == 16: f.write(s+"\n") s = '' curchar = 0 else: s = s+',' curchar += 1 if s: f.write(s[:len(s)-1]+'\n') def usage(): print '''Biscuits Video Converter Usage:\n python convert.py <input_video_file.ext>\n''' return#-----------------------------------------------------------------------------'''FFMPEG NOTES: -ss timestamp : Grab video starting at this timestamp -c copy : -c for codec. 'copy' keyword to ensure no reencoding -t timestamp : Duration of encoding -vf filtergraph : Alias for -filter:v (filter for video stream) using the crap in 'filtergraph'.'''input_commands = [ '-ss 00:00:04.0', None, ]output_commands = [ '-ss 00:00:04.0 -c copy -t 00:01:00.0', '-vf scale=-1.64',]print "Application starting..."try: infile = sys.argv[1] outfile1 = np(cwd+'/bin/temp1.mp4') outfile2 = np(cwd+'/bin/temp2.mp4') outfilei = np(cwd+'/bin/img/i%05d.png') if not os.path.isfile(infile): print "Error: "+str(infile)+" does not exist." sys.exit(1)except: print usage() sys.exit(1) print ":::: Pass 0" FFmpeg( inputs = {infile :'-y'}, outputs = {outfile1:'-c:v libx264 -crf 0 -preset ultrafast'},).run() print ":::: Pass 1" FFmpeg( inputs = {outfile1:'-y'}, outputs = {outfile2:'-vf scale=-1:64'},).run()print ":::: Pass 2"FFmpeg( inputs = {outfile2: '-y -ss 00:00:04.0'}, outputs = {outfile1:'-ss 00:00:04.0 -async 1 -t 00:01:00.0'},).run()print ":::: Pass 3"FFmpeg( inputs = {outfile1:'-y'}, outputs = {outfile2:'-vf crop=96:64:(in_w-96)/2:0'},).run()print ":::: Pass 4"FFmpeg( inputs = {outfile2:'-y'}, outputs = {outfilei:'-f image2 -r 30'},).run()print ":::: Beginning file conversion"flist = sorted(os.listdir(np(cwd+'/bin/img')))for f in flist: addframe(Image.open(np(cwd+'/bin/img/'+f)).convert('1').tobytes())addframe() #flush remaining buffer#compressed_arrays.append([buffers,len(outarray),outarray])with open(np(cwd+'/dat/filelist.z80'),'w') as flister: flister.write('#define getCurpage(x) x>>16\n') for i in compressed_arrays: tfn = 'I'+str(i[0]).zfill(3)+'.z80' flister.write('.dw '+tfn+' \\.db getCurpage('+tfn+')\n')with open(np(cwd+'/dat/numfiles.inc'),'w') as nfil: nfil.write("NUMBER_OF_FILES .EQU "+str(len(compressed_arrays)))#Sort by length of outarray, descendingcompressed_arrays = sorted(compressed_arrays,key=lambda i:i[1],reverse=True)slack = -1curfile = 0curpage = 0tslack = 0maxslack = 16383with open(np(cwd+'/dat/myinc.z80'),'w') as finc: while len(compressed_arrays)>0: curpage += 1 finc.write('defpage('+str(curpage)+') ;Prev page slack:'+str(slack)+'\n') slack = maxslack i=0 while i<len(compressed_arrays): if compressed_arrays[i][1]>slack: i+=1 else: a = compressed_arrays.pop(i) tfn = 'I'+str(a[0]).zfill(3)+'.z80' slack -= a[1] dumptofile(tfn,a) finc.write(tfn+': #include "dat/'+tfn+'" ;File length: '+str(a[1])+'\n') tslack += slack print "Page "+str(curpage).zfill(3)+" with slack "+str(slack).zfill(5)+" emitted." print "Compressed "+str(768*len(flist))+" bytes to "+str(total_len)+' with slack bytes '+str(tslack)print ":::: Completed."
def readFile(file): a = [] f = open(file,'rb') b = f.read(1) while b!=b'': a.append(ord(b)) b = f.read(1) f.close() return a def writeFile(file,a): f = open(file,'wb+') f.write(bytearray(a)) f.close()
def readFile(file): with open(file,'rb') as f: a = [] b = f.read(1) while b!=b'': a.append(ord(b)) b = f.read(1) return a def writeFile(file,a): with open(file,'wb+') as f; f.write(bytearray(a))