Advertisement
Guest User

4FFmpeg-crop-resize-calculator

a guest
Nov 8th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from gi.repository import Gtk
  4.  
  5.        
  6. class reuse_init(object):
  7.  
  8.  
  9.     def make_label(self, text):
  10.         label = Gtk.Label(text)
  11.         label.set_use_underline(True)
  12.         label.set_alignment(1.0, 0.5)
  13.         label.show()
  14.         return label
  15.  
  16.     def make_slider_and_spinner(self, init, min, max, step, page, digits):
  17.         controls = {'adj':Gtk.Adjustment(init, min, max, step, page), 'slider':Gtk.HScale(), 'spinner':Gtk.SpinButton()}
  18.         controls['slider'].set_adjustment(controls['adj'])
  19.         controls['slider'].set_draw_value(False)
  20.         controls['spinner'].set_adjustment(controls['adj'])
  21.         controls['spinner'].set_digits(digits)
  22.         controls['slider'].show()
  23.         controls['spinner'].show()
  24.         return controls
  25.    
  26.     def make_frame_ag(self, f_shadow_type, f_label, alig_pad, gri_row_sp, gri_colu_sp, gri_homog):
  27.         controls =  {'frame':Gtk.Frame(), 'ali':Gtk.Alignment(), 'grid':Gtk.Grid()}
  28.         controls['frame'].set_shadow_type(f_shadow_type) # 3 GTK_SHADOW_ETCHED_IN , 1 GTK_SHADOW_IN
  29.         controls['frame'].set_label(f_label)
  30.         controls['ali'].set_padding(alig_pad, alig_pad, alig_pad, alig_pad)
  31.         controls['frame'].add(controls['ali'])
  32.         controls['grid'].set_row_spacing(gri_row_sp)
  33.         controls['grid'].set_column_spacing(gri_colu_sp)
  34.         controls['grid'].set_column_homogeneous(gri_homog) # True
  35.         controls['ali'].add(controls['grid'])
  36.         controls['frame'].show()
  37.         controls['ali'].show()
  38.         controls['grid'].show()
  39.         return controls
  40.    
  41.     def make_spinbut(self, init, min, max, step, page, digits, numeric):
  42.         controls = {'adj':Gtk.Adjustment(init, min, max, step, page), 'spinner':Gtk.SpinButton()}
  43.         controls['spinner'].set_adjustment(controls['adj'])
  44.         controls['spinner'].set_digits(digits)
  45.         controls['spinner'].set_numeric(numeric)
  46.         controls['spinner'].show()
  47.         return controls
  48.    
  49.     def make_combobox(self, *vals):
  50.         controls = {'list':Gtk.ListStore(str), 'cbox':Gtk.ComboBox(), 'cellr':Gtk.CellRendererText()}
  51.         for i, v in enumerate(vals):
  52.               controls['list'].append([v])
  53.         controls['cbox'].set_model(controls['list'])      
  54.         controls['cbox'].pack_start(controls['cellr'], True)
  55.         controls['cbox'].add_attribute(controls['cellr'], "text", 0)
  56.         controls['cbox'].set_active(0)
  57.         controls['cbox'].show()
  58.         return controls
  59.    
  60. class ProgrammaGTK(reuse_init):
  61.    
  62.  
  63.     def __init__(self):
  64.        
  65.         self.window =  Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
  66.         self.window.connect("destroy", self.on_window_destroy)
  67.         self.window.set_title("4FFmpeg video crop resize calculator")
  68.         self.window.set_position(1) # 1 center, 0 none,
  69.         self.window.set_border_width(20)
  70.         self.window.set_default_size(500,-1)
  71.         #----------------------------------
  72.        
  73.         # scale
  74.         self.make_scale()
  75.         #------------------------
  76.         #self.button1 = Gtk.Button(label="Calcola")
  77.         #self.button1.connect("clicked", self.on_button1_clicked)
  78.         #self.table.attach(self.button1, 2, 3, 1, 2)
  79.        
  80.         #----------------------------------------------
  81.         # crop ----------------------------------------
  82.         self.make_crop()   
  83.        
  84.         # -denoise ----------------------------
  85.         self.make_denoise()
  86.        
  87.        
  88.         #---output------------------------
  89.         self.frame_out = self.make_frame_ag(3, '  FFmpeg filter string (select and copy):  ' , 12, 8, 8, True)
  90.         self.labelout = Gtk.Label("")
  91.         self.labelout.set_selectable(1)
  92.         self.frame_out['grid'].attach(self.labelout, 0, 0, 1 ,1)
  93.        
  94.        
  95.         #-- main VBox -------------------------
  96.         self.vbox1 = Gtk.VBox()
  97.         self.vbox1.set_spacing(12)
  98.        
  99.         #----crop frame -------------------
  100.         self.vbox1.pack_start(self.frame_crop['frame'], 1, 0, 0)
  101.         #------------------------------
  102.        
  103.         #----scale frame------------------
  104.         #------------------------------------
  105.         self.vbox1.pack_start(self.frame_scale['frame'], 1, 0, 0)
  106.        
  107.         # ------denoise frame------------------------
  108.         #------------------------------------
  109.         self.vbox1.pack_start(self.frame_dn['frame'], 1, 0, 0)
  110.        
  111.         #test-------------------
  112.         #self.f_test = self.make_frame_ag(3, "TEST" , 6, 8, 8, True)
  113.         #self.label_test = Gtk.Label("TEST")
  114.         #self.f_test['grid'].attach(self.label_test, 0, 0, 1, 1)
  115.  
  116.        
  117.         #self.vbox1.pack_start(self.f_test['frame'], 1, 0, 0)
  118.        
  119.         #-----output frame ---------------------
  120.         #----------------------------------------
  121.         self.vbox1.pack_start(self.frame_out['frame'], 1, 0, 0)
  122.        
  123.         self.window.add (self.vbox1)
  124.         #---------------------------------
  125.         self.window.show_all()
  126.        
  127.         # inizializza variabili
  128.         self.crop_str = ""
  129.         self.scalestr = ""
  130.         self.dn_str = ""
  131.        
  132.     def on_window_destroy(self, *args):
  133.         Gtk.main_quit(*args)
  134.            
  135.            
  136.     def make_scale(self):
  137.         self.frame_scale = self.make_frame_ag(3, 'scale' , 6, 8, 8, True)
  138.         # ------------------------------
  139.         self.box1 = self.make_combobox(
  140.           "None",
  141.           "W",
  142.           "H"
  143.           )
  144.         self.frame_scale['grid'].attach(self.box1['cbox'], 0, 0, 1, 1)
  145.         self.box1['cbox'].connect("changed", self.on_scale_clicked)
  146.         #------------------------------------
  147.         self.box2 = self.make_combobox(
  148.           "16/9",
  149.           "4/3"
  150.           )
  151.         self.frame_scale['grid'].attach(self.box2['cbox'], 0, 1, 1, 1)
  152.         self.box2['cbox'].connect("changed", self.on_scale_clicked)
  153.         #-----------------------------------------
  154.         self.size_spinner = self.make_slider_and_spinner(720, 200, 2000, 1, 10, 0)
  155.         self.frame_scale['grid'].attach(self.size_spinner['slider'], 1, 0, 1, 1)
  156.         self.frame_scale['grid'].attach(self.size_spinner['spinner'], 2, 0, 1, 1)
  157.         self.size_spinner['slider'].set_size_request(150,-1)
  158.         self.size_spinner['adj'].set_value(720.001) #mettere decimali se no non si vede
  159.         self.size_spinner['adj'].connect("value-changed", self.on_scale_clicked)
  160.         #-------------------------------------------------------
  161.         self.check = Gtk.CheckButton("lanczos")
  162.         self.check.connect("toggled", self.on_scale_clicked)
  163.         self.frame_scale['grid'].attach(self.check, 2, 1, 1, 1)
  164.        
  165.     def make_crop(self):
  166.         self.frame_crop = self.make_frame_ag(3, '' , 6, 8, 8, True)
  167.         # -------
  168.         self.checkcr = Gtk.CheckButton("crop")
  169.         self.checkcr.connect("toggled", self.on_crop_clicked)
  170.         self.frame_crop['grid'].attach(self.checkcr, 0, 0, 1, 1)
  171.         #----------
  172.         self.labelcropinw = self.make_label("input W:")
  173.         self.frame_crop['grid'].attach(self.labelcropinw, 0, 1, 1, 1)
  174.         self.labelcropinh = self.make_label("input H:")
  175.         self.frame_crop['grid'].attach(self.labelcropinh, 0, 2, 1, 1)
  176.         #
  177.         self.spincw = self.make_spinbut(640 , 100 , 1920 , 10 , 1 , 0, True )
  178.         self.spincw["adj"].connect("value-changed", self.on_crop_clicked)
  179.         self.frame_crop['grid'].attach(self.spincw["spinner"], 1, 1, 1, 1)
  180.         #
  181.         self.spinch = self.make_spinbut(480 , 100 , 1280 , 10 , 1 , 0, True )
  182.         self.spinch["adj"].connect("value-changed", self.on_crop_clicked)
  183.         self.frame_crop['grid'].attach(self.spinch["spinner"], 1, 2, 1, 1)
  184.         #
  185.         self.labelcroptop = self.make_label("crop Top:")
  186.         self.frame_crop['grid'].attach(self.labelcroptop, 2, 1, 1, 1)
  187.         self.labelcropbot = self.make_label("crop Bottom:")
  188.         self.frame_crop['grid'].attach(self.labelcropbot, 2, 2, 1, 1)
  189.         #
  190.         self.spinct = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  191.         self.spinct["adj"].connect("value-changed", self.on_crop_clicked)
  192.         self.frame_crop['grid'].attach(self.spinct["spinner"], 3, 1, 1, 1)
  193.         #
  194.         self.spincb = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  195.         self.spincb["adj"].connect("value-changed", self.on_crop_clicked)
  196.         self.frame_crop['grid'].attach(self.spincb["spinner"], 3, 2, 1, 1)
  197.         #
  198.         self.labelcroplef = self.make_label("crop Left:")
  199.         self.frame_crop['grid'].attach(self.labelcroplef, 4, 1, 1, 1)
  200.         self.labelcroprig = self.make_label("crop Right:")
  201.         self.frame_crop['grid'].attach(self.labelcroprig, 4, 2, 1, 1)
  202.         #
  203.         self.spincl = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  204.         self.spincl["adj"].connect("value-changed", self.on_crop_clicked)
  205.         self.frame_crop['grid'].attach(self.spincl["spinner"], 5, 1, 1, 1)
  206.         #
  207.         self.spincr = self.make_spinbut(0 , 0 , 600 , 1 , 10 , 0, True )
  208.         self.spincr["adj"].connect("value-changed", self.on_crop_clicked)
  209.         self.frame_crop['grid'].attach(self.spincr["spinner"], 5, 2, 1, 1)
  210.        
  211.     def make_denoise(self):
  212.         self.frame_dn = self.make_frame_ag(3, '' , 6, 8, 8, True)
  213.         # ----------------------
  214.         self.checkdn = Gtk.CheckButton("denoise")
  215.         self.checkdn.connect("toggled", self.on_dn_clicked)
  216.         self.frame_dn['grid'].attach(self.checkdn, 0, 0, 1, 1)
  217.         # -------------------------
  218.         self.labeldn = Gtk.Label("hqdn3d:")
  219.         self.labeldn.set_alignment(1.0, 0.5)
  220.         self.frame_dn['grid'].attach(self.labeldn, 1, 0, 1, 1)
  221.         #----------------------------------------
  222.         self.spindn = self.make_spinbut(4 , 2 , 8 , 1 , 10 , 0, True )
  223.         self.spindn["adj"].connect("value-changed", self.on_dn_clicked)
  224.         self.frame_dn['grid'].attach(self.spindn["spinner"], 2, 0, 1, 1)
  225.            
  226.     def on_scale_clicked(self, button):
  227.         if (self.box1['cbox'].get_active() != 0):
  228.           self.calcola_scale()
  229.         else:
  230.           self.scalestr = ""
  231.           self.outfilter()
  232.              
  233.     def on_dn_clicked(self, button):
  234.         self.dn_str = ""
  235.         if self.checkdn.get_active():
  236.             dn_val = int( self.spindn['adj'].get_value() )
  237.             self.dn_str = "hqdn3d=" + str(dn_val)
  238.         self.outfilter()
  239.            
  240.     def on_crop_clicked(self, button):
  241.         ok = 0
  242.         self.crop_str = ""
  243.         if self.checkcr.get_active():
  244.             ok = 1
  245.         if ok:
  246.             inW = int(self.spincw['adj'].get_value())
  247.             inH = int(self.spinch['adj'].get_value())
  248.             crT = int(self.spinct['adj'].get_value())
  249.             crB = int(self.spincb['adj'].get_value())
  250.             crL = int(self.spincl['adj'].get_value())
  251.             crR = int(self.spincr['adj'].get_value())  
  252.             finW = inW - crL - crR
  253.             finH = inH - crT - crB
  254.             if ( (finW < 100) or (finH < 100) ):
  255.             self.crop_str = (" ** invalid crop ** ")
  256.             else:
  257.             self.crop_str = "crop=" + str(finW) + ":" \
  258.               + str(finH) + ":" \
  259.               + str(crL)  + ":" \
  260.               + str(crT)
  261.         self.outfilter()
  262.              
  263.            
  264.     def calcola_scale(self):
  265.         self.scalestr = ""
  266.         numero = self.size_spinner['adj'].get_value()
  267.         if ( (int(numero) % 2) != 0):
  268.           numero = int(numero) + 1
  269.         else:
  270.           numero = int(numero)    
  271.         dar = self.box2['cbox'].get_active()
  272.         lato = self.box1['cbox'].get_active()      
  273.         if (dar == 1):
  274.           if (lato == 1):
  275.             n2 = numero/4*3
  276.           else:
  277.             n2 = numero/3*4
  278.           setdar = ",setdar=4/3,setsar=1/1"
  279.         else:
  280.           if (lato == 1):
  281.             n2 = numero/16*9
  282.           else:
  283.             n2 = numero/9*16
  284.           setdar = ",setdar=16/9,setsar=1/1"         
  285.         if ( (int(n2) % 2) != 0):
  286.           n2 = int(n2) + 1
  287.         else:
  288.           n2 = int(n2)         
  289.         self.scalestr = "scale="
  290.         if (lato == 1):
  291.           self.scalestr = self.scalestr + str(numero) + ":" + str(n2)
  292.         else:
  293.           self.scalestr = self.scalestr + str(n2) + ":" + str(numero)            
  294.         lanc = self.check.get_active()  
  295.         if lanc:
  296.           self.scalestr = self.scalestr + ":flags=lanczos" + setdar  
  297.         else:
  298.           self.scalestr = self.scalestr + setdar
  299.         self.outfilter()
  300.              
  301.     def outfilter(self):
  302.         final_str = ""  
  303.         if (self.crop_str != ""):
  304.             final_str = final_str + self.crop_str      
  305.         if (self.scalestr != ""):
  306.             if (final_str == ""):
  307.             final_str = final_str + self.scalestr
  308.             else:
  309.             final_str = final_str + "," + self.scalestr
  310.         if (self.dn_str != ""):
  311.             if (final_str == ""):
  312.             final_str = final_str + self.dn_str
  313.             else:
  314.             final_str = final_str + "," + self.dn_str
  315.            
  316.         if (final_str != ""):
  317.             final_str = "-vf " + final_str
  318.            
  319.         self.labelout.set_text(final_str)
  320.        
  321.        
  322.     def main(self):
  323.         Gtk.main()
  324.  
  325.        
  326.    
  327.  
  328. if __name__ == "__main__":
  329.     hwg = ProgrammaGTK()
  330.     Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement